Operating System - HP-UX
1830898 Members
3298 Online
110017 Solutions
New Discussion

Re: batch compressing dated files

 
SOLVED
Go to solution
Jeff Hagstrom
Regular Advisor

batch compressing dated files

I have reports that are put into directories and are named by only the date(20030324.txt). I have these files dating back to the begining of the year. I want to compress all the files older then today minus 30 or everything older then this month (compress < 200303*). I don't know awk or pearl.
11 REPLIES 11
Pete Randall
Outstanding Contributor
Solution

Re: batch compressing dated files

for i in `find /dir_name -mtime +30`
do
compress $i
done


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: batch compressing dated files

Hi Jeff:

# cd
# find . -type f -xdev -mtime +30 -exec gzip {} \;

Regards!

...JRF...
Jeff Schussele
Honored Contributor

Re: batch compressing dated files

Hi Jeff,

Try the following

find /start_dir -mtime +30 -a -name "2003*.*" -exec compress {} \;

Notes:

1) Replace /start_dir with a relevant dir root to ease the search burden

2) The -a is a logical AND operator in the command

3) Try the command first with ll in place of compress to verify just WHICH files will be compressed

4) I'd recommend gzip over compress - generally compresses better.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
A. Clay Stephenson
Acclaimed Contributor

Re: batch compressing dated files

Well, if the filename is all you can trust then you must only use the filename for date calculations. We need to create a filename 30 days ago and any filename less than that (since you have wisely chosen YYYYMMDD format) is a candidate for compression.

I'll assume that you have cd'ed to the target directory.

P30FNAME=$(caljd.sh -y -s $(caljd.sh -p 30))".txt"

ls | while read FNAME
do
if [[ -f ${FNAME} ]]
then
if [[ "${FNAME}" < "${P30FNAME}" ]]
then
echo "Compress this file"
fi
fi
done


That should do it and here's caljd.sh to do the date calculations.

If it ain't broke, I can fix that.
Jeff Hagstrom
Regular Advisor

Re: batch compressing dated files

Does -mtime look at the date on the file or the filename?
Pete Randall
Outstanding Contributor

Re: batch compressing dated files

-mtime looks at mod time.


Pete

Pete
Pete Randall
Outstanding Contributor

Re: batch compressing dated files

If you need to depend on the file's name, then you're better off with Clay's method.

Pete

Pete
Jeff Schussele
Honored Contributor

Re: batch compressing dated files

Hi,

-mtime +X where X=# days is the time of last modification.

atime = access time

ctime = change of status time

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Jeff Hagstrom
Regular Advisor

Re: batch compressing dated files

[NIKE-1 find /u/online/rcpt_aud -mtime +30 -a -name "2003*.*" -exec ll {} \;
find: cannot stat
/u/online/recpt_aud

????????
James R. Ferguson
Acclaimed Contributor

Re: batch compressing dated files

Hi (again) Jeff:

The error, "cannot stat", means that there is no such directory in this case.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: batch compressing dated files

Jeff, the fact that you've spelled rcpt_aud both with and without the extra "e" (recpt_aud) makes me wonder what you actually tried to find on.


Pete

Pete