![]() 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 maxdepth 1 -type dĪs you can see in the output above, it also shows the hidden directory. With maxdepth 1 you ask the find command to keep the search at the current level only (and not go inside the subdirectories). With type d, you ask the find command to only look for directories. I hope you are familiar with the find command. #DISPLAY DIRECTORY TREE LINUX MAC OS HOW TO#Here's how to use the find command to list only the subdirectories: find directory_path -maxdepth 1 -type d Use find 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 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. This way, I'll get notified and respond back to you as soon as I can.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 that? ![]() If you have additional questions, or if the issue persists after the performing the steps above, please let me know by replying to this thread.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |