Operating System - HP-UX
1820076 Members
3200 Online
109608 Solutions
New Discussion юеВ

Shell Script :- Compress files older than 3 days

 
sinhass
Regular Advisor

Shell Script :- Compress files older than 3 days

Hi,

Could anyone pls help me to write a script which will run everyday and compress all the files which are 3 days old(created) in three different directories.

With thanks & regards

-sinhass
7 REPLIES 7
Con O'Kelly
Honored Contributor

Re: Shell Script :- Compress files older than 3 days

Hi

Use the find command in a script.

find / -path "//*" -prune -type f -mtime +3 -exec gzip {} \;

This will ensure you only find files in the directory specified not in any subdirectories.

Cheers
Con
Indira Aramandla
Honored Contributor

Re: Shell Script :- Compress files older than 3 days

Hi,

You could also do this to compress files older than 3 days.

cd to the directory
find . -name "file_name" -mtime +3 -exec compress {} \;


IA
Never give up, Keep Trying
sinhass
Regular Advisor

Re: Shell Script :- Compress files older than 3 days

I ran those scripts earlier but one of my colleague told that if someone modify any of the files then the script will not recognise that file. He wants to compress all the files created three days back irrespective of modification/access time(mtime/atime/ctime).
Michael Selvesteen_2
Trusted Contributor

Re: Shell Script :- Compress files older than 3 days

Hi Sinhass,

Use any of the suggestion to compress file older than 3 days by incorporating the command in the script. Use cron to schedule the script to run every 3 days.

For more about cron please refer the following link

http://www.unixgeeks.org/security/newbie/unix/cron-1.html

--
M
Kent Ostby
Honored Contributor

Re: Shell Script :- Compress files older than 3 days

sinhass --

The problem is that hp-ux does not track a "create" date.

It tracks an access, modify, and change date but not a creation date so if you have to use creation date, the script would have to keep track via diffed lls of the files that were now in the directory.

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Steve Steel
Honored Contributor

Re: Shell Script :- Compress files older than 3 days

Hi


http://www.shelldorado.com/


Will explain this to you that indeed you cannot find easily on a create date


If someone is using the file you do not want to compress it anyway


Find with -mtime +3 is the safest way to go


If I create a file and after 2 days zeroise it and put new contents it becomes to me new and not 2 days old


Steve Steel

If you want truly to understand something, try to change it. (Kurt Lewin)
sinhass
Regular Advisor

Re: Shell Script :- Compress files older than 3 days

Thanks everybody for your time and help specially Steve & Michael for referring these two fantastic sites.

-Sinhass