So I need to loop through a tree of directories, currently I can print off all the files in a directory, but once that is done I need to be able to go into the subfolders of the starting directory and so on. My Program prints off the files and the folders as a start, but I need to check each one to make sure it is a directory and then enter it. Could someone provide some guidance on this?

migrated from stackoverflow.com Sep 29 '11 at 10:36

This question came from our site for professional and enthusiast programmers.


help test yields

test: test [expr]


-d FILE True if file is a directory.


Exit Status: Returns success if EXPR evaluates to true; fails if EXPR evaluates to false or an invalid argument is given.

test can be abbreviated with [, so you can do

if [ -d "$file" ]; then
    # do stuff
test -d myFileOrFolder

but what you describe looks like the behaviour of find.


From comments entered to some of the responses I am guessing you want to run some script in each of the directory. In that case one of the possible ways is to use find with exec options as follow:

find ./ -type d -exec sh -c "cd {} && <your_script_with_absolute_path>" \;

For example, in the current dir there is a script test.sh & you want to execute it in each of the subdir in the current dir then

find ./ -type d -exec sh -c "cd {} && `pwd`/test.sh" \;

To the find command -type d will ask to look for all directories in path ./ passed as first argument. -exec will execute command for each such find, in this case sh -c which is creating a shell & executing a command with quotes, {} indicates the argument found by find command.
Hope this helps!


Use the -d operator to test for the existence of a directory:

if [ -d "$DIR" ]; then
  • Ok, that makes sense, then how would I enter that directory and run the script again? – Eric Anderson Sep 28 '11 at 12:49

Pseudo code:

[ -d your_filename ] && it's a folder ...
  • then how would i enter the new directory and then call my script again? – Eric Anderson Sep 28 '11 at 12:52
find . -type d

this finds all subdirectories

  • then how would i enter the new directory and then call my script again? – Eric Anderson Sep 28 '11 at 12:52
  • there's an -execdir option and a few others, depending on your version of find. You could also just pipe this to a while loop which explicitly visited each directory. – Foo Bah Sep 28 '11 at 15:23

Based on some of the comments, I'm guessing that you don't actually want to print the directories, but rather process all of the regular files in a directory tree (including all of the subdirectories). If that's the case, you may want to take a look at the exec option to find.

From memory, you probably want something like this (using the cat command to stand in for your script):

find . -type f -exec cat {} \;

In this example, all of the non-directory files in the entire tree (recursively) will be processed by cat (printed to standard out).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.