38

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

Nick Vence
  • 754
  • 1
  • 9
  • 19

5 Answers5

51

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.

harpo
  • 41,820
  • 13
  • 96
  • 131
  • 5
    If mydir is the name of a directory and you execute 'ls mydir', ls shows the contents of mydir. If you execute 'ls -d mydir', then ls shows lists mydir without showing its contents. Now if no directory or wildcard is provided, then ls operates on '.' (i.e. the current directory) by default. So 'ls -d' is equivalent to 'ls -d .', which should list '.' without showing it's contents. I hope that clarifies a bit why a directory name or wildcard is required. – Mansoor Siddiqui Feb 07 '11 at 18:46
  • The -d option is superior to the echo solution when you want to combine with e.g. -l for a long listing, to see folder permissions. Thanks very much! – Graham Perks Feb 04 '15 at 01:54
21

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.

Alan
  • 1,889
  • 2
  • 18
  • 30
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • 5
    consider putting -maxdepth before -type, your command probably will run into a warning. Nevermind, not a big deal. – Michael Mao Mar 07 '13 at 06:10
  • @MichaelMao, following your suggestion, I proposed an edit in which "-maxdepth 1" occurs before "-type d". – Alan Dec 05 '22 at 20:46
12
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*/
Philipp
  • 48,066
  • 12
  • 84
  • 109
  • Interesting point. But this will include files starting with "H2", and the OP wants only subdirectories. – harpo Feb 07 '11 at 16:46
3

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*
user1388236
  • 131
  • 1
  • 3
3

Perhaps this is what you are looking for?

ls | grep H2*
Adam J. Forster
  • 17,789
  • 9
  • 25
  • 20