user2742389
user2742389

Reputation: 3

Write shell script for finding "differently named" files not older than 7 days old

I have a list of files something like :

NVNTR.INTERNET.20150721.ASP.zip
NVNTR.INTERNET.20150721.BGRNB.zip
NVNTR.INTERNET.20150721.FRNB.zip
NVNTR.INTERNET.20150721.GNORNB.zip
NVNTR.INTERNET.20150721.OZRNB.zip
NVNTR.INTERNET.20150721.UGRNB.zip
NVNTR.INTERNET.20150721.URNB.zip
NVONB.INTERNET.20091126.WEBDE_1UND1.URNB.zip
NVONB.INTERNET.20100217.O2_2.URNB.zip
NVONB.INTERNET.20110214.OUTBOX.URNB.zip
NVONB.INTERNET.20111111.ONB.zip
NVONB.INTERNET.20111130.NTR.URNB.zip
NVONB.INTERNET.20150810.AFRNB.zip
NVONB.INTERNET.20150810.ASP.zip
NVONB.INTERNET.20150810.AZRNB.zip
NVONB.INTERNET.20150810.BGRNB.zip
NVONB.INTERNET.20150810.FRNB.zip
NVONB.INTERNET.20150810.GNORNB.zip
NVONB.INTERNET.20150810.LGV.zip
NVONB.INTERNET.20150810.ONB.zip
NVONB.INTERNET.20150810.OZRNBDELTA.zip
NVONB.INTERNET.20150810.OZRNB.zip
NVONB.INTERNET.20150810.SFRNB.zip
NVONB.INTERNET.20150810.UGRNB.zip
NVONB.INTERNET.20150810.URNB.zip
NVONB.INTERNET.20150826.AFRNB.zip
NVONB.INTERNET.20150826.ASP.zip
NVONB.INTERNET.20150826.AZRNB.zip
NVONB.INTERNET.20150826.BGRNB.zip
NVONB.INTERNET.20150826.FRNB.zip
NVONB.INTERNET.20150826.GNORNB.zip
NVONB.INTERNET.20150826.LGV.zip
NVONB.INTERNET.20150826.ONB.zip
NVONB.INTERNET.20150826.OZRNBDELTA.zip
NVONB.INTERNET.20150826.OZRNB.zip
NVONB.INTERNET.20150826.SFRNB.zip
NVONB.INTERNET.20150826.UGRNB.zip
NVONB.INTERNET.20150826.URNB.zip
NVONB.PKID.20150819.AEND.zip
NVONB.PKID.20150819.KONS.zip

From the above list, i need to filter the files which are not more than 7 days old and when i say old, i consider the date from the date command. But i cannot use the find command as the Access, Modify and Change timestamps are same for all the files. lets say in this file NVONB.PKID.20150819.AEND.zip

I will have to consider 20150819 for the file age and not any other parameter. So if my system date is 20150828 means, all the files from the above list "not more than" 7days old should list out like :

NVONB.INTERNET.20150826.AFRNB.zip
NVONB.INTERNET.20150826.ASP.zip
NVONB.INTERNET.20150826.AZRNB.zip
NVONB.INTERNET.20150826.BGRNB.zip
NVONB.INTERNET.20150826.FRNB.zip
NVONB.INTERNET.20150826.GNORNB.zip
NVONB.INTERNET.20150826.LGV.zip
NVONB.INTERNET.20150826.ONB.zip
NVONB.INTERNET.20150826.OZRNBDELTA.zip
NVONB.INTERNET.20150826.OZRNB.zip
NVONB.INTERNET.20150826.SFRNB.zip
NVONB.INTERNET.20150826.UGRNB.zip
NVONB.INTERNET.20150826.URNB.zip

This will be the part of cron and will be executed every week on a particular day.

How can I achieve this?

Upvotes: 0

Views: 84

Answers (3)

user2742389
user2742389

Reputation: 3

Thanks for the responses, I finally completed the script myself something like this :

datelist () {
count=1
now=`date +"%Y%m%d"`
echo "$now"
while [ $count -le 9 ];
do
now=`date +"%Y%m%d" -d "$now - 1 day"`;
echo "$now";
count=`expr $count + 1`;
done
}

echo "Date Range considered:" `datelist`
testdata="`pwd`/"Test_Data""
matcheddata="`pwd`/"Matched_Files""
if [ ! -d "$matcheddata" ]; then
mkdir -p $matcheddata
fi
logfile=`pwd`/log.txt
for i in `datelist`;
do
#find $testdata -name "*$i*" -print -exec cp -f {} $testdata \; | tee -a log.txt
find $testdata -name "*$i*" -print -exec mv {} $matcheddata \; |  tee -a log.txt
done

Using datelist function, I calculated it for 9 days as of now and then used the generated list to get the files moving with find command.

Few snippets with respect to above solutions:

  1. I do not want to change the datestamp because these files needs to be moved as it is to different application
  2. Solutions above did not seem to work as per my requirements.

I really appreciate the time you all have put in and if my script needs some further improvement then please suggest. I am open for comments.

Above script can be standardized with respect to arguments passed and traps too. Also a condensed Python form from Pythonistas will be much appreciated.

Upvotes: 0

Walter A
Walter A

Reputation: 20012

You can "fix" the file timestamp:

find . -name \*zip | while read file; do
filedate=$(echo ${file##*/}|cut -d"." -f3)
   echo "${file} ${filedate}"
   touch -t ${filedate}1200 ${file}
done
find . -name \*zip -mtime +7 -exec rm {} \;
ls -l

Upvotes: 0

Joe
Joe

Reputation: 28336

You should be able to get 7 days ago using date. Exactly how will depend on which version you have, a couple of possibilities are below. Check date --help to see what your version supports.

If your version of date supports -v offset:

date -v-7d +%Y+m+d

If it supports -r for converting epoch:

date -r $(($(date +%s) - 604800)) +%Y%m%d
#(604800 == number of seconds in 7 days)

Once you have the date command, use awk to filter your list. This one splits the name on the periods, compares the third element with target date, and prints the entire filename for any that are newer.

ls /path | awk -F. '$3 > '`date -v-7d +%Y%m%d`'{print $0}'

Upvotes: 1

Related Questions