开发者

How to `ls` only one level deep?

开发者 https://www.devze.com 2023-02-08 20:34 出处:网络
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.

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 c开发者_JAVA百科an I limit the output to one level deep? I'd like to see the following output

H2a/ H2b/ H2z/

Thanks for your help, Nick


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.


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.


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*/


Perhaps this is what you are looking for?

ls | grep 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*
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号