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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:10:18  来源:igfitidea点击:

How to `ls` only one level deep?

bashls

提问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 -doption 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 lswill 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 treeby 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*