Do something in all first level subdirectories linux command shell
https://gist.github.com/3932467
for dir in *; do echo $dir ; done;
Written by Leandro Moreira
Related protips
4 Responses
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
·
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Shell
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#