Linux Shell – Iterate through file listings

This script is useful if you’d like to iterate through find results or say files in a directory and pick specific files and do operations on them. Specific to my case – I had to search for files with current timestamp in the filename and use those files for some operations. Here’s what I did in a bash script

TIMESTAMP=`date +%F`

cd /home/varun.verma/scripts/shell/getFiles
#find . -name "*.tar.gz" -type f -exec basename {} \; #Used this command to test the results. It finds files with a format and displays only the name of files, not the path (since I know where I am searching)

for i in `find . -name "*$TIMESTAMP*.tar.gz" -type f -exec basename {} \;`
  file=`echo -n $i`
  echo $file #Or do some operation here

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s