Operating System - HP-UX
1828592 Members
2709 Online
109983 Solutions
New Discussion

Re: Script to delete files

 
SOLVED
Go to solution
Jeeshan
Honored Contributor

Script to delete files

Hi

I have some programs/scripts in HP-UX which generate logs by name/date series continuously.

I want to delete those files using script without deleting the last 2-3 files using creation date.

Can anyone help me regarding this?
a warrior never quits
7 REPLIES 7
Dennis Handly
Acclaimed Contributor
Solution

Re: Script to delete files

If the file is created with a sortable date (one with YYYYMMDDHHSS numbers only), you can just use:
$ echo rm -f $(ll -r prefix* | awk '{print $9}' | tail -n +4)

Remove the echo if you are happy with what you are removing.

If the files don't have a sortable name and you can assume the last modification date is ordered, you can use "ll -t".
A. Clay Stephenson
Acclaimed Contributor

Re: Script to delete files

Bear in mind that unless the creation date is carried in the name of the file itself (or you have those data stored in another file) that what you ask is not possible. UNIX has no notion of the creation date of a file. If one of the other timestamps (atime, ctime, or mtime) happens to match the creation time of a file it is nothing more and nothing less than coincidence.
If it ain't broke, I can fix that.
Arturo Galbiati
Esteemed Contributor

Re: Script to delete files

Hi,

find . -mtime 1 -type f -name *.log | xargs rm

this will rmove your file modified between 24 and 48 hours ago (about creation see previos post). Type the correct regular expression istead of *.log

HTH,
Art
Jeeshan
Honored Contributor

Re: Script to delete files

Thanks Dennis for your support

What i wanna do is, i have a directory which contains files like this manner

-rw-r--r-- 1 user group 24033 Dec 19 13:05 History_2007121913.log
-rw-r--r-- 1 user group 24110 Dec 19 14:04 History_2007121914.log
-rw-r--r-- 1 user group 23575 Dec 19 15:04 History_2007121915.log

Last log is running. If i want to delete the files keeping the last one what will be the procedure?

a warrior never quits
Dennis Handly
Acclaimed Contributor

Re: Script to delete files

>i have a directory which contains files like this manner
History_2007121913.log

These are ordered by name.

>If i want to delete the files keeping the last one what will be the procedure?

$ echo rm -f $(ll -r History_* | awk '{print $9}' | tail -n +2)

Remove the "echo" when you are sure it removes only the files you want.
Jeeshan
Honored Contributor

Re: Script to delete files

thanks all
a warrior never quits
Steve Post
Trusted Contributor

Re: Script to delete files

in know it's closed but here's one more way ...more to that find command.

log files older than 2 days get gzipped.
log files older than 5 days get deleted.
I run the find command from the spot where the files are so I do not accidentally delete stuff I care about. I also use the -name option in find for the same reason.

cd /directory/path
find . -type f -mtime 2 -name "log*" -exec gzip {} \;

find . -type f -mtime 5 -name "log*gz" -exec rm {} \;


And here's another way

cd /directory/path
LIST1=`find . -type f -mtime 2 -name "log*"`

for F in $LIST1
do
gzip $F
done