Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 06-17-2005, 01:27 AM   #1
Registered: Jun 2005
Distribution: Centos
Posts: 215

Rep: Reputation: 30
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.

Old 06-17-2005, 01:44 AM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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.

Last edited by jschiwal; 06-17-2005 at 01:48 AM.
Old 06-17-2005, 01:51 AM   #3
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
Cheers mate, I'll give that a go!
Old 06-17-2005, 02:20 AM   #4
Registered: Sep 2004
Location: Des Moines, Iowa
Distribution: Slackware, Mandriva, Debian derivatives, +BSD/ Solaris/ Minix/ plan9/ GNU/HURD...
Posts: 185

Rep: Reputation: 30
I can tell you this much:

This line will print only those files in a directory specified by the date argument to sed:
 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:
  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:
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!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
Delete files for a particular changed date recursively? qwertyme Linux - Newbie 5 01-23-2009 10:41 AM
delete files date wise hardeep_ubhi Linux - General 1 11-16-2004 02:07 AM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 07:54 PM
Listing Files based on date axero Linux - Newbie 2 10-19-2003 04:58 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:37 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration