221

How do I write a bash script that goes through each directory inside a parent_directory and executes a command in each directory.

The directory structure is as follows:

parent_directory (name could be anything - doesnt follow a pattern)

  • 001 (directory names follow this pattern)
    • 0001.txt (filenames follow this pattern)
    • 0002.txt
    • 0003.txt
  • 002
    • 0001.txt
    • 0002.txt
    • 0003.txt
    • 0004.txt
  • 003
    • 0001.txt

the number of directories is unknown.

12 Answers 12

240

This answer posted by Todd helped me.

find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && pwd" \;

The \( ! -name . \) avoids executing the command in current directory.

7
  • 5
    And if you only want to run the command in certain folders: find FOLDER* -maxdepth 0 -type d \( ! -name . \) -exec bash -c "cd '{}' && pwd" \;
    – Martin K
    Dec 3, 2015 at 10:40
  • 4
    I had to do -exec bash -c 'cd "$0" && pwd' {} \; since some directories contained single quotes and some double quotes. Aug 18, 2016 at 8:46
  • 25
    You also could omit current directory by adding -mindepth 1
    – mdziob
    Jan 9, 2017 at 16:05
  • How to change this to a function accept a command like "git remote get-url origin` instead of pwd directly?
    – roachsinai
    Feb 14, 2019 at 3:51
  • disd(){find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c 'cd "{}" && "$@"' \;} not work.
    – roachsinai
    Feb 14, 2019 at 3:55
131

You can do the following, when your current directory is parent_directory:

for d in [0-9][0-9][0-9]
do
    ( cd "$d" && your-command-here )
done

The ( and ) create a subshell, so the current directory isn't changed in the main script.

4
  • 5
    It doesn't matter here because the wildcard doesn't match anything else than numbers, but in the general case, you basically always want to put the directory name in double quotes inside the loop. cd "$d" would be better in that it transfers to situations where the wildcard does match files whose names contain whitespace and/or shell metacharacters.
    – tripleee
    Apr 17, 2015 at 9:53
  • 1
    (All of the answers here seem to have the same flaw, but it matters the most in the top-voted answer.)
    – tripleee
    Apr 17, 2015 at 9:54
  • 1
    Also, the vast majority of commands don't actually care in which directory you execute them. for f in foo bar; do cat "$f"/*.txt; done >output is functionally equivalent to cat foo/*.txt bar/*.txt >output. However, ls is one command that does produce slightly different output depending on how you pass it arguments; similarly, a grep or wc which outputs a relative file name will be different if you run it from a subdirectory (but often, you want to avoid going into a subdirectory precisely for that reason).
    – tripleee
    Apr 17, 2015 at 9:56
  • Thanks for the answer I used it like so for d in ./packages/*-spa/; do (cd "$d" && cp test.env .env); done Mar 30, 2022 at 12:24
67

You can achieve this by piping and then using xargs. The catch is you need to use the -I flag which will replace the substring in your bash command with the substring passed by each of the xargs.

ls -d */ | xargs -I {} bash -c "cd '{}' && pwd"

You may want to replace pwd with whatever command you want to execute in each directory.

2
  • 5
    I don't know why this has not been upvoted, but the simplest script I found so far. Thanks
    – Linvi
    Feb 18, 2019 at 10:13
  • 1
    This one is the best one as any command you run with xargs with generate an exit code and follow up actions can be taken from the value of '$?'. Thanks for this. May 19, 2022 at 14:56
62

If you're using GNU find, you can try -execdir parameter, e.g.:

find . -type d -execdir realpath "{}" ';'

or (as per @gniourf_gniourf comment):

find . -type d -execdir sh -c 'printf "%s/%s\n" "$PWD" "$0"' {} \;

Note: You can use ${0#./} instead of $0 to fix ./ in the front.

or more practical example:

find . -name .git -type d -execdir git pull -v ';'

If you want to include the current directory, it's even simpler by using -exec:

find . -type d -exec sh -c 'cd -P -- "{}" && pwd -P' \;

or using xargs:

find . -type d -print0 | xargs -0 -L1 sh -c 'cd "$0" && pwd && echo Do stuff'

Or similar example suggested by @gniourf_gniourf:

find . -type d -print0 | while IFS= read -r -d '' file; do
# ...
done

The above examples support directories with spaces in their name.


Or by assigning into bash array:

dirs=($(find . -type d))
for dir in "${dirs[@]}"; do
  cd "$dir"
  echo $PWD
done

Change . to your specific folder name. If you don't need to run recursively, you can use: dirs=(*) instead. The above example doesn't support directories with spaces in the name.

So as @gniourf_gniourf suggested, the only proper way to put the output of find in an array without using an explicit loop will be available in Bash 4.4 with:

mapfile -t -d '' dirs < <(find . -type d -print0)

Or not a recommended way (which involves parsing of ls):

ls -d */ | awk '{print $NF}' | xargs -n1 sh -c 'cd $0 && pwd && echo Do stuff'

The above example would ignore the current dir (as requested by OP), but it'll break on names with the spaces.

See also:

6
  • 1
    The only proper way to put the output of find in an array without using an explicit loop will be available in Bash 4.4 with mapfile -t -d '' dirs < <(find . -type d -print0). Until then, your only option is to use a loop (or defer the operation to find). Oct 4, 2015 at 14:03
  • 1
    In fact, you don't really need the dirs array; you could loop on find's output (safely) like so: find . -type d -print0 | while IFS= read -r -d '' file; do ...; done. Oct 4, 2015 at 14:04
  • @gniourf_gniourf I'm visual and always trying to find/make the things which look simple. E.g. I've this script and by using bash array is easy and readable for me (by separating the logic into smaller pieces, instead of using pipes). Introducing additional pipes, IFS, read, it gives the impression of additional complexity. I'll have to think about it, maybe there is some easier way of doing it.
    – kenorb
    Oct 4, 2015 at 14:11
  • If by easier you mean broken then yes, there are easier ways of doing. Now don't worry, all these IFS and read (as you say) become a second nature as you get used to them… they are part of the canonical and idiomatic ways of writing scripts. Oct 4, 2015 at 14:20
  • By the way, your first command find . -type d -execdir echo $(pwd)/{} ';' doesn't do what you want (the $(pwd) is expanded before find is even executed)… Oct 4, 2015 at 14:21
26

If the toplevel folder is known you can just write something like this:

for dir in `ls $YOUR_TOP_LEVEL_FOLDER`;
do
    for subdir in `ls $YOUR_TOP_LEVEL_FOLDER/$dir`;
    do
      $(PLAY AS MUCH AS YOU WANT);
    done
done

On the $(PLAY AS MUCH AS YOU WANT); you can put as much code as you want.

Note that I didn't "cd" on any directory.

Cheers,

3
  • 3
    There are a couple of instances of "Useless Use of ls" here - you could just do for dir in $YOUR_TOP_LEVEL_FOLDER/* instead. Sep 19, 2011 at 11:40
  • 1
    Well, maybe it's useless in a general sense but it allows to do filtering directly in the ls (i.e all directories ended with .tmp). That's why I used the ls $dir expression
    – gforcada
    Sep 19, 2011 at 13:39
  • Will for subdir in 'ls $YOUR_TOP_LEVEL_FOLDER/$dir'; traverse multiple directories nesting like parent/child1/child1_1/child1_1_1/? Or just one directory deep into the parent?
    – ruslaniv
    Feb 3, 2021 at 6:21
23
for dir in PARENT/*
do
  test -d "$dir" || continue
  # Do something with $dir...
done
1
  • 2
    And this is very easy to comprehend! Jul 19, 2018 at 12:26
6

While one liners are good for quick and dirty usage, I prefer below more verbose version for writing scripts. This is the template I use which takes care of many edge cases and allows you to write more complex code to execute on a folder. You can write your bash code in the function dir_command. Below, dir_coomand implements tagging each repository in git as an example. Rest of the script calls dir_command for each folder in directory. The example of iterating through only given set of folder is also include.

#!/bin/bash

#Use set -x if you want to echo each command while getting executed
#set -x

#Save current directory so we can restore it later
cur=$PWD
#Save command line arguments so functions can access it
args=("$@")

#Put your code in this function
#To access command line arguments use syntax ${args[1]} etc
function dir_command {
    #This example command implements doing git status for folder
    cd $1
    echo "$(tput setaf 2)$1$(tput sgr 0)"
    git tag -a ${args[0]} -m "${args[1]}"
    git push --tags
    cd ..
}

#This loop will go to each immediate child and execute dir_command
find . -maxdepth 1 -type d \( ! -name . \) | while read dir; do
   dir_command "$dir/"
done

#This example loop only loops through give set of folders    
declare -a dirs=("dir1" "dir2" "dir3")
for dir in "${dirs[@]}"; do
    dir_command "$dir/"
done

#Restore the folder
cd "$cur"
5

I don't get the point with the formating of the file, since you only want to iterate through folders... Are you looking for something like this?

cd parent
find . -type d | while read d; do
   ls $d/
done
1
  • 1
    you should add -maxdepth 1 -mindepth 1 in find as well else it would pick up all subdirectories too ! Oct 28, 2021 at 1:31
3

you can use

find .

to search all files/dirs in the current directory recurive

Than you can pipe the output the xargs command like so

find . | xargs 'command here'
1
  • 6
    This is not what was asked.
    – kenorb
    Oct 4, 2015 at 15:08
2
  #!/bin.bash
for folder_to_go in $(find . -mindepth 1 -maxdepth 1 -type d \( -name "*" \) ) ; 
                                    # you can add pattern insted of * , here it goes to any folder 
                                    #-mindepth / maxdepth 1 means one folder depth   
do
cd $folder_to_go
  echo $folder_to_go "########################################## "
  
  whatever you want to do is here

cd ../ # if maxdepth/mindepath = 2,  cd ../../
done

#you can try adding many internal for loops with many patterns, this will sneak anywhere you want
2

You could run sequence of commands in each folder in 1 line like:

for d in PARENT_FOLDER/*; do (cd "$d" && tar -cvzf $d.tar.gz *.*)); done
0
for p in [0-9][0-9][0-9];do
    (
        cd $p
        for f in [0-9][0-9][0-9][0-9]*.txt;do
            ls $f; # Your operands
        done
    )
done
2
  • You'd need to change back up the directory again, or do the cd in a subshell. Sep 19, 2011 at 11:37
  • Downvote: the edit lost the cd so this code doesn't actually do anything useful.
    – tripleee
    Apr 17, 2015 at 9:51

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.