Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: delete based on file date stamp

Tags:

linux

bash

I have a folder with a bunch of files. I need to delete all the files created before July 1st. How do I do that in a bash script?

like image 707
David Oneill Avatar asked Sep 09 '10 13:09

David Oneill


1 Answers

I think the following should do what you want:

touch -t 201007010000 dummyfile
find /path/to/files -type f ! -newer dummyfile -delete

The first line creates a file which was last modified on the 1st July 2010. The second line finds all files in /path/to/file which has a date not newer than the dummyfile, and then deletes them.

If you want to double check it is working correctly, then drop the -delete argument and it should just list the files which would be deleted.

like image 91
bramp Avatar answered Oct 01 '22 07:10

bramp