LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Delete files based on date (https://www.linuxquestions.org/questions/linux-general-1/delete-files-based-on-date-334460/)

stefaandk 06-17-2005 01:27 AM

Delete files based on date
 
Got a directory full of files going back for months, I need to delete all files in there which are older then this month of June.

Can't find the syntax to delete files by date so any help would be appreciated.

Cheers

jschiwal 06-17-2005 01:44 AM

You could use the find command.

find <directory> -ctime +17 -daystart -exec rm "{}" \;

The plus sign before the 17 will select change times greater than 17*24 hours ago. The daystart options measures the time from midnight rather than the current time. The double quotes are needed when a file-name contains whitespace.

This command will also delete files from subdirectories. If that isn't what you want, use the -maxdepth 1 option.

stefaandk 06-17-2005 01:51 AM

Cheers mate, I'll give that a go!

Hosiah 06-17-2005 02:20 AM

I can tell you this much:

This line will print only those files in a directory specified by the date argument to sed:
Code:

ls -lh | awk '{print $6 " " $9}' | sed -n '/Jun/p'
Where "Jun" is the month you want to search for.
You could then pipe this output to a file:
Code:

  ls -lh | awk '{print $6 " " $9}' | sed -n '/May/p' | awk '{print $2}' >> file_list
And we have just the names of the files you specify by month in a file.
You then should be able to do:
Code:

rm `cat file_list`
NOTE: those are back quotes around `cat file_list`
I'd test it first on "dummy data" to make sure I didn't screw up!


All times are GMT -5. The time now is 03:50 PM.