简体   繁体   English

在 Linux 中使用 ls 仅显示目录

[英]Using ls in Linux to show directories only

I'm getting myself familiar with Linux (Debian 9).我正在熟悉 Linux (Debian 9)。 However I managed to get myself confused with the ls command manual.但是,我设法让自己对ls命令手册感到困惑。 I made up the following trivial setting for testing:我为测试做了以下简单的设置:

 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

I would like to see all the directory names and their subdirectories, but not the filenames.我想查看所有目录名称及其子目录,但不是文件名。 That is, I want something like ls --insert-options-here so that the output is this subset from above:也就是说,我想要类似ls --insert-options-here的东西,这样 output 就是上面的这个子集:

 .: 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

According to ls manual that should be achieved by ls -ldR .根据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

But this is what happens instead: only the '.'但这就是发生的事情:只有'.' is shown.显示。

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

What am I missing from the manual?我从手册中遗漏了什么?

I noticed from https://stackoverflow.com/a/29277454/11199684 that this can be achieved by find. -maxdepth 2 -type d -print我从https://stackoverflow.com/a/29277454/11199684注意到这可以通过find. -maxdepth 2 -type d -print find. -maxdepth 2 -type d -print which is impressive but it gives less information than the ls output above. find. -maxdepth 2 -type d -print令人印象深刻,但它提供的信息比上面的 ls output 少。 Besides, I might not know beforehand the correct maxdepth?此外,我可能事先不知道正确的最大深度? And above all, for future needs I'm actually interested in teaching myself from the manuals.最重要的是,为了未来的需要,我实际上有兴趣从手册中自学。 Advice are appreciated.建议表示赞赏。

The -d option apply to [FILE] given as input, in your case, as none are given, you fall back to the default one: the current directory. -d选项适用于作为输入给出的[FILE] ,在你的情况下,由于没有给出,你会回退到默认的:当前目录。 The recursivity doesn't apply as no directory are returned.递归性不适用,因为没有返回目录。

To get the same output as ls with the find command, you can combine them:要使用find命令获得与ls相同的 output ,可以将它们组合起来:

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

in some way you could use the grep tool to filter the results:在某种程度上,您可以使用grep工具过滤结果:

 ll -A | grep ^d

if you want to use it programatically, in a bash script you can have only the dir names (but find might be better for that):如果您想以编程方式使用它,在 bash 脚本中,您只能拥有目录名称(但find可能会更好):

 ll -A | grep / | awk '{print $9}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM