![]() Here's the output which is identical to what you got with the ls -d command: Similar to the ls command, you can also use the */ pattern to list only the directories in the current working directory. The unlikely candidate? You'll be surprised to know that echo command in Linux can also be used for displaying the contents of a directory. Using echo command for listing directories Here's the output: Documents]$ tree -dai -L 1 i - remove the tree structure from the display.a - look for hidden files and directories as well.You can modify it to show only directories and only at the current level. If your aim is to list only the directories, you may also use the tree command.īy default, the tree command gives you the complete directory structure. Use tree command to list only directories You can use grep to filter the contents that start with d: ls -l | grep '^d'īut this gives you a lot more fields than just the directory names: Documents]$ ls -l | grep '^d' If you long list the contents, you can identify the directories because start with d. You can always rely on the good old grep command for filtering the output for specific content. */ to display hidden directories, but it only displays hidden directories. 1 abhishek abhishek 44 Nov 7 18:22 Documents/my_dir/ĭid you notice that it doesn't list the hidden directory? That's one shortcoming of this method. 1 abhishek abhishek 16 Nov 7 18:22 Documents/another_dir/ĭrwxrwxr-x. Here's an example where I move out of the Documents directory and then list only the directories inside Documents directory: ~]$ ls -ld Documents/*/ĭrwxrwxr-x. In this situation, you can use */ at the end of the path of the directory with ls -d: ls -d Path/To/Dir/*/ What if you are not in the same directory? ![]() ![]() The above command works in the current directory. List only subdirectories in a specific directory 1 abhishek abhishek 44 Nov 7 18:22 my_dir 1 abhishek abhishek 16 Nov 7 18:22 another_dirĭrwxrwxr-x. If you do not want the trailing slash (/) at the end of the directory names, you can use the cut command to cut it out: Documents]$ ls -ld */ | cut -f1 -d'/'ĭrwxrwxr-x. 1 abhishek abhishek 44 Nov 7 18:22 my_dir/ 1 abhishek abhishek 16 Nov 7 18:22 another_dir/ĭrwxrwxr-x. You may combine it with the long listing option -l and most other options: Documents]$ ls -ld */ĭrwxrwxr-x. This picture depicts the difference pretty well. With *, you list all the content (including contents of the subdirectories) and the / restricts the pattern to directories. The -d option list directories not its contents (which includes file, directories etc). ![]() Why */? Because without it, ls -d will only return the directory name. Here's the output it shows: Documents]$ ls -d */ To list only the subdirectories, use the -d option with ls command like this: ls -d */ It is always good to do it with the familiar ls command because this is the command you use for displaying the content of a directory. Here's the content of the directory I am going to use in the examples here: Use ls command to list directories only Listing only the directories is no different:ĭon't worry. Like anything in Linux, there are several ways to accomplish the same task. The ls command in Linux is used for listing the contents of any directory.īy default, it lists all the contents, be it a file or a directory or a link or a named pipe.īut what if you want to list only the directories? How do you do that?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |