UNIX: Loop Through Files In A Directory

last updated in Categories

How do I loop through files in a directory under UNIX like operating systems?

The most simplest form is as follows:

for file in /path/to/file1.txt /path/to/file2.txt /path/to/file3.txt
 # do something on $file
 cat "$file"

You can directly process all command line args:

for file in $*
 # do something on $file
 [ -f "$file" ] && cat "$file"

OR simply process all *.css file in the current directory:

for file in *.css
 # do something on "$file"
 cat "$file" >> /var/www/cdn.example.com/cache/large.css

You can also read file names stored in a text file called delete.txt (note read with -r and IFS which will take care of file with spaces):

while IFS= read -r f <&3; 
      #do something with "$f"
      rm -f "$f"
done 3< delete.txt

Make sure you always put $f or $file in double quote. Here is another sample script it will go through /home/wwwdata/{example.com,example.net,nixcraft.com} and process all files using for loop:

# sync all domains to backup server at midnight 
domains="example.com example.net nixcraft.com cyberciti.biz"
now=$(date +"%d-%m-%Y_%S")
	local f="$1"
	local d="$2"
	[[ "$d" != "" ]] &&      echo "                            $d"
	[[ "$f" == "start" ]] && echo "--------------------------------------------------------------"
	[[ "$f" == "end" ]] &&   echo "=============================================================="
source /usr/local/nixcraft/mgmt/ssh/.keychain/$HOSTNAME-sh
for d in $domains
	logdata "start" "$d @ $(date)"
        [ -d "/home/wwwdata/$d/" ] && { 	cd "/home/wwwdata/$d/";  
 	/usr/bin/rsync  --exclude='cache/cache-*'\
			--exclude '.bash_history' \
			--exclude '.viminfo' \
			--exclude 'cache/*_mutex.lock' \
			--exclude 'broken-link-checker*' \
                        --exclude 'tmp/*'
			-a --delete . backup@nasbox.nixcraft.net.in:/raid6/$HOSTNAME/ ;
	logdata "end" "$d @ $(date)" 
done &> $log
[ -f $latest ] && /bin/rm -f $latest
ln -s $log $latest
mail -s "Backup $HOSTNAME" admin@clients.nixcraft.net.in < $latest

See also:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

3 comment

  1. For most cases the -exec option of find is very helpful:
    find . -name ‘*.css’ -exec cat {} >> large.css \;

    # delete certain files younger than 60 minutes:
    find /wherever/you/want -type f -name ‘*substring*’ -mmin -60 -delete

  2. Another way, using xargs
    find . -print0 -type f | xargs -0 -I {} cp -v {} /tmp

    Still, have a question? Get help on our forum!