50

I am trying to recurse into folders and then run commands on them, using bash script. Any suggestions?

fuzzy
  • 97
  • 6
miro
  • 673
  • 1
  • 6
  • 7

7 Answers7

70

If you want to recurse into directories, executing a command on each file found in those, I would use the find command, instead of writing anything using shell-script, I think.

That command can receive lots of parameters, like type to filter the types of files returned, or exec to execute a command on each result.


For instance, to find directories that are under the one I'm currently in :

find . -type d -exec echo "Hello, '{}'" \;

Which will get me somehthing like :

Hello, '.'
Hello, './.libs'
Hello, './include'
Hello, './autom4te.cache'
Hello, './build'
Hello, './modules'


Same to find the files under the current directory :

find . -type f -exec echo "Hello, '{}'" \;

which will get me something like this :

Hello, './config.guess'
Hello, './config.sub'
Hello, './.libs/memcache_session.o'
Hello, './.libs/memcache_standard_hash.o'
Hello, './.libs/memcache_consistent_hash.o'
Hello, './.libs/memcache.so'
Hello, './.libs/memcache.lai'
Hello, './.libs/memcache.o'
Hello, './.libs/memcache_queue.o'
Hello, './install-sh'
Hello, './config.h.in'
Hello, './php_memcache.h'
...


Some would say "it's not shell"... But why re-invent the wheel ?
(And, in a way, it is shell ^^ )


For more informations, you can take a look at :

Abizern
  • 146,289
  • 39
  • 203
  • 257
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • If you need to rename directories as well as files recursively check out this answer: http://stackoverflow.com/questions/4268591/unix-rename-files-directories-to-uppercase – JJones Dec 11 '13 at 08:59
  • thanks. how would i adjust this to look just for sub-directories of my current directory, and not look for sub-sub directories – Zach Smith Oct 18 '17 at 09:51
  • ah. like this: `find . -maxdepth 1 -type d -exec echo "Hello, '{}'" \;` – Zach Smith Oct 18 '17 at 09:52
  • How do I make it use a function? I have it defined with `wavogg() { stuff }` and `find` just says `'wavogg': No such file or directory` – Aaron Franke Mar 19 '19 at 23:26
  • Actually, it also doesn't seem to work with `cd`... `find . -type d -exec cd "'{}'" \;` – Aaron Franke Mar 19 '19 at 23:27
22

Bash 4.0 introduced the globstar option, so a construct like:

for f in mydir/**/*
do
  # operations here
done

...will act recursively on whatever lands in $f. Turn this on with "shopt -s globstar", otherwise the ** will be treated as a singular *.

Found this gem today at http://www.linuxjournal.com/content/globstar-new-bash-globbing-option, after being inspired by the zsh construct (which I have enabled by default).

Trey Blancher
  • 321
  • 2
  • 2
9

Some basic shells miss commands like 'find' and some of their commands don't support recursivity. In that case you can use this script to run the desired command in all subdirs in the tree:

CDIR=$(pwd)
for i in $(ls -R | grep :); do
    DIR=${i%:}                    # Strip ':'
    cd $DIR
    $1                            # Your command
    cd $CDIR
done

If you name the above "recurse.sh" then use:

./recurse.sh <command>

Example (change the owner/group to 'root' of all files in the tree):

./recurse.sh "chown 0:0 *"
Paolo Santos
  • 91
  • 1
  • 1
7

Something like this should achieve your goal:

function RecurseDirs
{
    oldIFS=$IFS
    IFS=$'\n'
    for f in "$@"
    do
    -----your activity here-----
        if [[ -d "${f}" ]]; then
            cd "${f}"
            RecurseDirs $(ls -1 ".")
            cd ..
        fi
    done
    IFS=$oldIFS
}
Zuul
  • 16,217
  • 6
  • 61
  • 88
dreynold
  • 865
  • 8
  • 17
1

Have a look at the find command and check the switches -type (use d to specify directory) and -exec (to specify a command to execute).

Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
1

Sorry I don't understand what you are asking. The best I can guess with your question is

find -type d -exec scriptname.sh \{\} \;
Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
0

For most recursive file operations, you want to use find, as the other answers explain.

There is an example of a recursive bash script included in the bash-doc package. If you've got those examples installed, it will be at /usr/share/doc/bash/examples/functions/recurse (on Debian).

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141