How do I list only directories using the ls command in Linux?
Published on Aug. 22, 2023, 12:17 p.m.
To list only directories using the ls command in Linux, you can use the -d
option. Here are some examples:
ls -d */
- This lists only directories in the current directory, showing only their names.ls -l | grep '^d'
- This lists all files and directories in the current directory, and then filters out the lines that begin with “d” (which indicates a directory).find . -maxdepth 1 -type d
- This uses thefind
command to search for directories at a maximum depth of 1 (meaning it only searches the current directory).echo */
- This lists only directories in the current directory, showing only their names. This method uses shell expansion to expand*/
into a list of directories.tree -d -L 1
- This lists all directories in the current directory and the first level of subdirectories in a tree-like format. The-d
option lists directories only and the-L 1
option limits the search to the first level of subdirectories.