Reputation: 1356
I used the below command to delete files older than a year.
find /path/* -mtime +365 -exec rm -rf {} \;
But now I want to delete all files whose modified time is older than 01 Jan 2014. How do I do this in Linux?
Upvotes: 90
Views: 172383
Reputation: 81
First I execute this
touch -t 202310080000 /tmp/olddate ---> this creates a file with the specified date
find "path to which you want to apply search" -type f ! -newer /tmp/olddate -delete -----> this deletes files from the path where you want with reference to the file created at the certain date.
Upvotes: 0
Reputation: 860
This other answer pollutes the file system and find
itself offers a "delete" option. So, we don't have to pipe the results to xargs and then issue an rm.
This answer is more efficient:
find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete
Upvotes: 44
Reputation: 71
find ~ -type f ! -atime 4|xargs ls -lrt
This will list files accessed older than 4 days, searching from home directory.
Upvotes: 7
Reputation: 1411
This works for me:
find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
Upvotes: 93
Reputation:
You can touch your timestamp as a file and use that as a reference point:
e.g. for 01-Jan-2014:
touch -t 201401010000 /tmp/2014-Jan-01-0000
find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs rm -rf
this works because find
has a -newer
switch that we're using.
From man find
:
-newer file
File was modified more recently than file. If file is a symbolic
link and the -H option or the -L option is in effect, the modification time of the
file it points to is always used.
Upvotes: 41