Brad Lucas

Programming, Clojure and other interests
August 23, 2017

Run A Command In Each Sub-Directory of a Directory

If you ever want to run the same command in each sub-directory of a directory consider the following example.

Replace the value for COMMAND with your own command.

$ COMMAND=pwd
$ for d in `find . -maxdepth 1 -type d`; do pushd > /dev/null $d; $COMMAND; popd > /dev/null; done

Or setup your own command by putting the following in your .bashrc file.

function run-command-in-subs {
  for d in `find . -maxdepth 1 -type d`; 
  do 
     pushd > /dev/null $d;
     $1;
     popd > /dev/null; 
  done
}
Tags: bash