bash - How to loop over directories in Linux? -


i writing script in bash on linux , need go through subdirectory names in given directory. how can loop through these directories (and skip regular files)?

for example:
given directory /tmp/
has following subdirectories: /tmp/a, /tmp/b, /tmp/c

i want retrieve a, b, c.

cd /tmp find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n' 

a short explanation: find finds files (quite obviously)

  • . current directory, after cd it's /tmp (imho more flexible having /tmp directly in find command. have 1 place, cd, change, if want more actions take place in folder)

  • -maxdepth 1 , -mindepth 1 make sure, find really, looks in current dir , doesn't include '.' in result

  • -type d looks directories

  • -printf '%f\n prints found folder's name (plus newline) each hit.

e voila!


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -