深度阅读

How do I list only directories using the ls command in Linux?

作者
作者
2023年08月22日
更新时间
9.61 分钟
阅读时间
0
阅读量

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.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。