DEV Community

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

Shriharsh on January 03, 2018

Problem 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 dir...
Collapse
 
msoedov profile image
Alex Miasoiedov

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

Collapse
 
shriharshmishra profile image
Shriharsh

Thanks! find is something I wish to use more.

Collapse
 
val_baca profile image
Valentin Baca • Edited

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

$ cat ~/bin/inall
#!/bin/sh
# 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}/*
do
    if [ -d $dir ]; then
        echo ""
        echo "$dir"
        cd $dir && $@
        cd - > /dev/null
    fi
done
Collapse
 
shriharshmishra profile image
Shriharsh

cool! neater than what I used!

Collapse
 
maestromac profile image
Mac Siri

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

Collapse
 
shriharshmishra profile image
Shriharsh

Glad you like it :)