# How do I delete files greater than a certain date on linux

Occasionally I extract an an archive into the wrong folder and would like to move or delete the newly extracted files.

What is the easiest way to do this via the command line? Can I somehow search for all files that are newer than the extraction time and pipe that into rm?

Thanks

-

Edit: As noted in the comments, tar modifies the mtime and ctime of the extracted files to match the dates in the archive, so this first method won't work unless the -m flag was used during extraction. The last method is optimal, but may result in deleting files you want if filenames collide.

find supports a -newer  file flag, specifying it should find files newer than file. touch has a -t argument to modify the access/modify time on a file. So to fix an oops that occurred around 7:25:30 PM:

$tar xzf whoops.tar.gz$ touch -t 200909261925.30 whoops-timestamp
$find . -newer whoops-timestamp  And if you're confident that displayed the correct files: $ find . -newer whoops-timestamp -print0 | xargs -0 rm -v


An alternative is to delete all the files listed in the archive you just extracted:

$tar tfz whoops.tar.gz | xargs rm -v  - (+1) The last option is the best. It's safer and nicer than what the OP had in mind. – nagul Sep 26 '09 at 23:54 question: wouldn't tar set the modified time to that of the original files? Wouldn't find use that modified time (vs change time?) during it's comparison? – ericslaw Sep 27 '09 at 4:05 Thanks! I used the last way of doing it. The archive was actually a zip so I did zipinfo -1 blah.zip | xargs rm -v – Tarski Sep 27 '09 at 9:23 @ericslaw Yup, that does seem to be the case. tar will preserve the dates from the archives unless you use the -m flag. So, the first method won't work as a general rule. – Annika Backstrom Sep 27 '09 at 13:58 Another alternative with find: $ find "/path/to_clean/" -type f -mtime +30 -print0 | xargs -0 rm -f


where the +30 is the number of days you wish to keep.

-