My Shell Workflow: Executing a command in lots of directories.

Updated on Jan 06, 2018


You want to run a shell command in many directories. For example, in my case, I wanted to execute a shell command in lots of directories(approx. 200) to clean up build files.


Writing a for-loop and running it on the shell did the trick for me.

for d in ./*/ ; do (cd "$d" && mvn clean); done


The above for-loop would run cd "$d" && mvn clean in each item of the directory list defined by the path ./*/. $d holds the directory name. You can be creative in defining the list of directories using a complex glob as well.

I hope this helps you as well.

Did you find this post useful? Show some love!

find . -name pom.xml -exec mvn clean .... \;

Thanks! find is something I wish to use more.

Very nearly the same, here's my "inall" script:

$ cat ~/bin/inall
# runs given command (all arugments) in each directory of pwd
# example: in a workspace directory with many subfolders:
# $ inall git status

echo $@
for dir in ${PWD}/*
    if [ -d $dir ]; then
        echo ""
        echo "$dir"
        cd $dir && $@
        cd - > /dev/null

This lil snippet of code is golden. Thanks for sharing!

Classic DEV Post from Dec 22 '17

How we can make a difference as software engineers

As the holiday season rolls around again the idea of doing good in the world ...

Follow @amandasopkin to see more of their posts in your feed.
Member since Mar 17, 2017
More from @shriharshmishra
"Behind the Corporate Proxy"
#productivity #httpproxy #vagrant #docker
"My Shell Workflow: Using bash history"
#shell #bash #productivity
Trending on
Fast way to build CRUD app?
#discuss #programming
Smells like Team spirit
#discuss #career #productivity #workplace
Bash Brackets Quick Reference
#bash #shell #cheatsheet
Resources for Getting into DevOps?
#help #discuss #devops
I'm visiting more & more every day ๐Ÿ˜
#discuss #learning
What are the alternatives to unit tests?
#coding #discuss
My commit message workflow
#git #productivity #workflow
Journey to fullstack engineering.
#beginners #programming #career