bash 如何`ls`只有一层深?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4923834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to `ls` only one level deep?
提问by Nick Vence
I have lots subdirectories containing data, and I want a short list of which jobs (subdirectories) I have. I'm not happy with the following command.
我有很多包含数据的子目录,我想要一个我有哪些工作(子目录)的简短列表。我对以下命令不满意。
$ ls H2*
H2a:
energy.dat overlap.dat
norm.dat zdip.dat ...
(much more)
H2b:
energy.dat overlap.dat
norm.dat zdip.dat ...
(much more)
This needless clutter defeats the purpose of the wildcard (limiting the output). How can I limit the output to one level deep? I'd like to see the following output
这种不必要的混乱违背了通配符的目的(限制输出)。如何将输出限制为一层深?我想看到以下输出
H2a/ H2b/ H2z/
Thanks for your help, Nick
谢谢你的帮助,尼克
回答by harpo
Try this
尝试这个
ls -d H2*/
The -d
option is supposed to list "directories only", but by itself just lists
该-d
选项应该列出“仅目录”,但它本身只是列出
.
which I personally find kind of strange. The wildcard is needed to get an actual list of directories.
我个人觉得有点奇怪。需要通配符来获取目录的实际列表。
UPDATE: As @Philipp points out, you can do this even more concisely and without leaving bash by saying
更新:正如@Philipp 指出的,你可以更简洁地做到这一点,而无需离开 bash 说
echo H2*/
The difference is that ls
will print the items on separate lines, which is often useful for piping to other functions.
不同之处在于ls
将在单独的行上打印项目,这对于管道到其他功能通常很有用。
回答by ajreal
you should consider use find, like
你应该考虑使用 find,比如
find . -type d -maxdepth 1 -name "H2*"
回答by Philipp
echo H2*
It's Bash who does the expansion, so you don't even need ls
.
进行扩展的是 Bash,因此您甚至不需要ls
.
Should you have both files and directories starting with H2
, you can append a slash to restrict the glob to directories:
如果文件和目录都以 开头H2
,则可以附加斜杠以将 glob 限制为目录:
echo H2*/
回答by Adam J. Forster
Perhaps this is what you are looking for?
也许这就是你要找的?
ls | grep H2*
回答by user1388236
Use tree
by Steve Baker at http://mama.indstate.edu/users/ice/tree/It fills in for a lot of things that are missing from ls
.
To list directories one layer deep:
使用tree
由史蒂夫·贝克在http://mama.indstate.edu/users/ice/tree/它填补了很多东西,从失踪ls
。列出一层深的目录:
tree -adi -L 1 H2*