如何“ls”仅一层深?

发布于 2024-10-16 01:02:52 字数 329 浏览 9 评论 0原文

我有很多包含数据的子目录,并且我想要一个我拥有的作业(子目录)的简短列表。我对以下命令不满意。

$ ls H2*
H2a:
energy.dat overlap.dat 
norm.dat zdip.dat ...
(much more)
H2b:
energy.dat overlap.dat
norm.dat zdip.dat ... 
(much more)

这种不必要的混乱违背了通配符的目的(限制输出)。如何将输出限制为一层深度?我想看到以下输出

H2a/ H2b/ H2z/

感谢您的帮助, 缺口

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晚风撩人 2024-10-23 01:02:52

试试这个

ls -d H2*/

-d 选项应该列出“仅目录”,但它本身只是列出

.

我个人觉得有点奇怪的。需要通配符才能获取实际的目录列表。

更新:正如@Philipp指出的,你可以更简洁地做到这一点,而无需离开bash,说

echo H2*/

差异在于ls将在单独的行上打印项目,这是通常对于通过管道传输到其他功能很有用。

Try this

ls -d H2*/

The -d option is supposed to list "directories only", but by itself just lists

.

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

echo H2*/

The difference is that ls will print the items on separate lines, which is often useful for piping to other functions.

怀念你的温柔 2024-10-23 01:02:52

您应该考虑使用 find,如下所示:

find . -maxdepth 1 -type d -name "H2*"

注意:在“-max深度 1”之前放置“-type d”会导致 Debian Linux 上出现警告(“find:警告:您已在参数后指定了全局选项 -maxdepth”) -type,但全局选项不是位置性的,即 -max深度 会影响在其之前指定的测试以及在其之后指定的测试。请在其他参数之前指定全局选项。”)Mac 上不会发出此类警告。

You should consider using find, like this:

find . -maxdepth 1 -type d -name "H2*"

NOTE: Putting "-type d" before "-maxdepth 1" results in a warning on Debian Linux ("find: warning: you have specified the global option -maxdepth after the argument -type, but global options are not positional, i.e., -maxdepth affects tests specified before it as well as those specified after it. Please specify global options before other arguments.") No such warning is issued on Mac.

长梦不多时 2024-10-23 01:02:52
echo H2*

由 Bash 进行扩展,因此您甚至不需要 ls

如果您同时拥有以 H2 开头的文件和目录,则可以附加斜杠以将 glob 限制为目录:

echo H2*/
echo H2*

It's Bash who does the expansion, so you don't even need ls.

Should you have both files and directories starting with H2, you can append a slash to restrict the glob to directories:

echo H2*/
怪异←思 2024-10-23 01:02:52

使用 Steve Baker 的 tree,网址为 http://mama.indstate.edu /用户/冰/树/
它填补了 ls 中缺少的许多内容。
要列出一层深的目录:

tree -adi -L 1 H2*

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 -adi -L 1 H2*
滿滿的愛 2024-10-23 01:02:52

也许这就是您正在寻找的?

ls | grep H2*

Perhaps this is what you are looking for?

ls | grep H2*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文