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:

  1. ls -d */ - This lists only directories in the current directory, showing only their names.
  2. 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).
  3. find . -maxdepth 1 -type d - This uses the find command to search for directories at a maximum depth of 1 (meaning it only searches the current directory).
  4. 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.
  5. 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.

Tags:

ls