Last Updated: February 25, 2016
·
2.36K
· leandromoreira

Do something in all first level subdirectories linux command shell

https://gist.github.com/3932467

for dir in *; do echo $dir ; done;

4 Responses
Add your response

That doesn't get all subdirectories. I assume you mean for the '*' to be replaced with something else.

If you really want all subdirectories replace the '*' with a call to 'find':

for dir in `find . -type d`; do echo $dir ; done;
over 1 year ago ·

@jacaetevha

Hi @jacaetevha,

In what situations it won't work? I ran the command:
for dir in *; do echo $dir ; done;
Without replace anything and it printed all first level subdirectories in macosx.

over 1 year ago ·

1) Your command echos files, as well as directories.
2) Your command doesn't deal with recursive subdirectories.

Perhaps you didn't intend for #2 above. If that's the case just add a -maxdepth argument to the find command:

for dir in `find . -type d -maxdepth 1`; do echo $dir ; done;
over 1 year ago ·

@jacaetevha I got your point

over 1 year ago ·