Operating System - HP-UX
1819695 Members
3543 Online
109605 Solutions
New Discussion юеВ

removing files based on date?

 
SOLVED
Go to solution
Eric_63
Occasional Contributor

removing files based on date?

I need to remove files in a directory based on the date of the file and not on the name.

What syntax could I use.

Like if I wanted to remove all files from May 1.
7 REPLIES 7
Pete Randall
Outstanding Contributor
Solution

Re: removing files based on date?

find /directory -mtime +30 -exec rm {} \;

would remove all files not accessed in the last 30 days.

HTH,
Pete

Pete
Sukant Naik
Trusted Contributor

Re: removing files based on date?

Hi,

You can use the find command with the -mtime, atime, -ctime option.

eg
find . -mtime 10 -name -exec rm {}\;

-Sukant
Who dares he wins
Robin Wakefield
Honored Contributor

Re: removing files based on date?

Hi Eric,

You can also reference an existing file:

find . -type f ! -newer filename | xargs rm

will remove any file older than "filename".

Rgds, Robin.
S.K. Chan
Honored Contributor

Re: removing files based on date?

You can even do range of date, for example ..

find . \( -newer refA -a ! -newer refB \) -exec rm -rf {} \;

refA=by touching a file with the starting timestamp
refB=by touching a file with the ending timetamp
V. V. Ravi Kumar_1
Respected Contributor

Re: removing files based on date?

hi,

for example, if u want delete all files in /tmp which were created/copied/accessed from May 1st, u try the following.

find /tmp -type f -atime -9 -exec rm {} \;

this will delete all the files which were accessed during last 9 days, that is from May 1st to today.

hope this helps
ravi
Never Say No
MANOJ SRIVASTAVA
Honored Contributor

Re: removing files based on date?

Hi Eric


I use awk for the same like this

rm -i `ls -l | awk '{if($6=="May" && $7=="1") print $NF}'`


this will delete files created on May 1 only , you can be creative with == && >> and <<


Manoj Srivastava
Eric_63
Occasional Contributor

Re: removing files based on date?

Thanks for all the replies.