shell script to traverse files recursively shell script to traverse files recursively shell shell

shell script to traverse files recursively


To apply a command (say, echo) to all files below the current path, use

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

for directories, use -type d


You should be looking at the find command.

For example, to change permissions all JPEG files under your /tmp directory:

find /tmp -name '*.jpg' -exec chmod 777 {} ';'

Although, if there are a lot of files, you can combine it with xargs to batch them up, something like:

find /tmp -name '*.jpg' | xargs chmod 777

And, on implementations of find and xargs that support null-separation:

find /tmp -name '*.jpg' -print0 | xargs -0 chmod 777


Bash 4.0

#!/bin/bashshopt -s globstarfor file in **/*.txtdo  echo "do something with $file"done