Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively read folders and executes command on each of them

Tags:

bash

shell

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

like image 736
miro Avatar asked Aug 26 '09 10:08

miro


People also ask

How do I recursively search a folder?

An easy way to do this is to use find | egrep string . If there are too many hits, then use the -type d flag for find. Run the command at the start of the directory tree you want to search, or you will have to supply the directory as an argument to find as well. Another way to do this is to use ls -laR | egrep ^d .

What are recursive folders?

Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.

What does recursively mean in Linux?

Recursive means that Linux or Unix command works with the contains of directories, and if a directory has subdirectories and files, the command works on those files too (recursively).


3 Answers

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 :

  • man find
  • lots of tutorials found with google, like, for instance, Unix Find Command Tutorial
like image 101
Pascal MARTIN Avatar answered Oct 24 '22 05:10

Pascal MARTIN


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).

like image 21
Trey Blancher Avatar answered Oct 24 '22 07:10

Trey Blancher


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 *"
like image 9
Paolo Santos Avatar answered Oct 24 '22 06:10

Paolo Santos