在Linux中使用ls仅显示目录

时间:2019-03-27 14:50:44

标签: linux ls manual

我逐渐熟悉Linux(Debian 9)。但是我设法使自己对ls命令手册感到困惑。我对测试进行了以下简单设置:

meouser:~/triv_dir$ ls -laR
.:
total 24
drwxr-xr-x  4 meouser meouser 4096 Mar 27 15:42 .
drwxr-xr-x 19 meouser meouser 4096 Mar 27 15:41 ..
drwxr-xr-x  4 meouser meouser 4096 Mar 27 15:55 a
drwxr-xr-x  2 meouser meouser 4096 Mar 27 15:42 b
-rw-r--r--  1 meouser meouser    6 Mar 27 15:44 c
-rw-r--r--  1 meouser meouser    6 Mar 27 15:44 d

./a:
total 24
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:55 .
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:42 ..
-rw-r--r-- 1 meouser meouser    4 Mar 27 15:50 aa
-rw-r--r-- 1 meouser meouser    4 Mar 27 15:50 ab
drwxr-xr-x 2 meouser meouser 4096 Mar 27 15:55 a_sub_1
drwxr-xr-x 2 meouser meouser 4096 Mar 27 15:55 a_sub_2

./a/a_sub_1:
total 12
drwxr-xr-x 2 meouser meouser 4096 Mar 27 15:55 .
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:55 ..
-rw-r--r-- 1 meouser meouser    4 Mar 27 15:55 aaa

./a/a_sub_2:
total 8
drwxr-xr-x 2 meouser meouser 4096 Mar 27 15:55 .
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:55 ..

./b:
total 16
drwxr-xr-x 2 meouser meouser 4096 Mar 27 15:42 .
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:42 ..
-rw-r--r-- 1 meouser meouser    4 Mar 27 15:50 ba
-rw-r--r-- 1 meouser meouser    4 Mar 27 15:50 bb

我想查看所有目录名称及其子目录,但看不到文件名。也就是说,我想要类似ls --insert-options-here的东西,以便输出是上面的这个子集:

.:
total 24
drwxr-xr-x  4 meouser meouser 4096 Mar 27 15:42 .
drwxr-xr-x 19 meouser meouser 4096 Mar 27 15:41 ..
drwxr-xr-x  4 meouser meouser 4096 Mar 27 15:55 a
drwxr-xr-x  2 meouser meouser 4096 Mar 27 15:42 b

./a:
total 24
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:55 .
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:42 ..
drwxr-xr-x 2 meouser meouser 4096 Mar 27 15:55 a_sub_1
drwxr-xr-x 2 meouser meouser 4096 Mar 27 15:55 a_sub_2

根据ls手册,该手册应由ls -ldR完成。

meouser:~/triv_dir$ man ls

   -a, --all
          do not ignore entries starting with .

   -d, --directory
          list directories themselves, not their contents

   -l     use a long listing format

   -R, --recursive
          list subdirectories recursively

但这是相反的情况:仅显示'.'

meouser:~/triv_dir$ ls -ldR
drwxr-xr-x 4 meouser meouser 4096 Mar 27 15:42 .

手册中我缺少什么?

我从https://stackoverflow.com/a/29277454/11199684注意到,这可以通过以下方法实现 find . -maxdepth 2 -type d -print 这令人印象深刻,但是它提供的信息少于上面的ls输出。此外,我可能事先不知道正确的最大深度?最重要的是,对于将来的需求,我实际上对从手册中自我介绍很有兴趣。意见表示赞赏。

1 个答案:

答案 0 :(得分:2)

-d选项适用于作为输入提供的[FILE],在您的情况下,由于未给出输入,您将退回到默认目录:当前目录。递归不适用,因为没有目录返回。

要使用ls命令获得与find相同的输出,可以将它们组合在一起:

find . -type d -exec ls -ld {} \;