Deleting the files between particular dates

Hi

Please help me for the below task.

In my home directory if I run ls -l command, it lists all the files, here I want to delete files created from January 2014 to Aug 2014...but I need to keep the files which are created after September 01 2014.

Thanks
Siva

Hi,

Here you would use the find command, below is an example.

find ./ -mtime +30 -type f -print

The above command will show all files modified 30 days ago or more - use the print statement first to be sure that you find the correct files and do check it's what you want. Then you can run the command again with the delete function added like this;

find ./ -mtime +30 -type f -exec rm {} \;

You will have to be sure that the list is correct from the first example and the "mtime" value will have to be set to the required vale.

Regards

Dave

Hi Shiv

i am assuming you are running below code today.
So it will list 10 days older files but not 253 days older.
243 means 01 jan 14 to 31 Aug 14. and 1 sep to 10 sep (10 days)
The files before 01 jan 14 will not be listed.
first list the files . verify the files then run the rm command
to list the files

find . -type f -mtime +10 -mtime -253 -exec ls {} +

to delete the files

find . -type f -mtime +10 -mtime -253 -exec rm {} +

If you have specific dates rather than having to calculate the number of days to look back, you can use touch to create files with the timestamps at the extremes that you want to work with and then use a find command to reference them.

For example, if you want to delete files between 5th April 2013 00:00 and 9th June 2014 23:59, then you can do this:-

touch -mt 201304050000 /tmp/start_marker
touch -mt 201406100000 /tmp/end_marker

find . -newer /tmp/start_marker ! -newer /tmp/end_marker -exec rm {} +

.... or if it doesn't understand the + , replace it with \; . The difference is that that \; will cause find to fire up the command given for each individual file and that takes more time than + which runs the command once for as many files as it can in one go. This really can make a difference when you you have many hundreds of files.

I've coded the end_marker file to be actually dated as 10th June at 00:00 because we want files that are older than that time.

I hope that this helps,
Robin