delete file by date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete file by date
# 1  
Old 08-10-2009
delete file by date

Hello everyone,
I have a folder with many files.the script (csh) runs daily and adds a new file to the folder.I need to delete the first file added that month so I will have only 30 files left every day.
Thanks.
# 2  
Old 08-10-2009
you can use
HTML Code:
find <directory where you have file> -type f -mtime -30 -exec rm -i {} \;
The code looks for files modified in last 30 days, it asks for conformation before deleting it. If you dont want interactive remove -i option for the 'rm' command.

Sorry as smiling dragon said you have to use +30. I should be very carefull when i suggesting in forum..

Last edited by luckybalaji; 08-10-2009 at 07:49 AM..
# 3  
Old 08-10-2009
I think the O/P is looking to delete files older than 30 days, not newer. To do that, use -mtime +30 instead.

---------- Post updated at 07:11 PM ---------- Previous update was at 07:08 PM ----------

Alternatively, if you just want to keep the most recent 30 files (rather than files newer than 30 days), you can do the following:
Code:
rm `ls -t | head -30`

# 4  
Old 08-10-2009
thank you very much guys.
I'm new to unix and this forum is sooo helpful.
# 5  
Old 08-10-2009
Smilie Very happy to help, good luck in your adventures learning Unix!
# 6  
Old 08-10-2009
Alternatively, if you just want to keep the most recent 30 files (rather than files newer than 30 days), you can do the following:
Code:
rm `ls -t | head -30`

[/quote]

I have tried that and it deletes the most recent 30 files because the ls -t command sorts by date with newest first.
how can i sort by date when the oldest date is first?
# 7  
Old 08-10-2009
Lightbulb

Quote:
Originally Posted by offerbukra
Quote:
Originally Posted by Smiling Dragon
Alternatively, if you just want to keep the most recent 30 files (rather than files newer than 30 days), you can do the following:
Code:
rm `ls -t | head -30`

I have tried that and it deletes the most recent 30 files because the ls -t command sorts by date with newest first.
how can i sort by date when the oldest date is first?
Oh dear! Sorry about that! That's two erroneous pieces of advice in one thread, wow... Smilie Smilie Smilie

What I should have said was to do the following:
Code:
rm `ls -t | tail -30`

Or:
Code:
rm `ls -tr | head -30`

And (as I'm sure you know by now if you didn't already), test it someplace safe first Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and delete file previous to some date

Hello All, I have a directory containing of many .dat file, but with different naming conventions. I want to delete files which are created before and on 10th September 2013. I tried this command to see files which are are the files created before 10th September find path/to/file -type... (6 Replies)
Discussion started by: nnani
6 Replies

2. Red Hat

Logrotate to delete file which has date stamp

Hello, can someone please suggest how to create an logrotate for this scenario. Need to delete all log file which are created more than 30 days ago, and all the log file have date stamp on it. I dont want to create a cron job for this task. here is the example -rw-r--r-- 1 tomcat tomcat ... (2 Replies)
Discussion started by: bobby320
2 Replies

3. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

4. Shell Programming and Scripting

Delete file with specific date

let say i have list of file PermissionsDirectoriesGroupSizeDateDirectory or file drwx------2users4096Nov 2 19:51mailv drwxr-s---35www 32768Jan 20 22:39public_htmlt drwx------ 2 users 4096 Nov 2 19:51 mail drwxr-s--- 35 www 32768 Jan 20 22:39 public_html drwxr-s--- 35 www 32768 Jan... (3 Replies)
Discussion started by: Jewel
3 Replies

5. Shell Programming and Scripting

delete a row in csv file based on the date

Hi, I have a csv file with old data..i need to have only last 30 days from the current dateof data in the file.The fourth field in the file is a date field.i need to write a script to delete the old data by comparing the the fourth field with the (current date -30).I need to delete the rows in... (2 Replies)
Discussion started by: pals70423
2 Replies

6. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

7. Shell Programming and Scripting

Delete lines in a file by date on the line

Hi I have a file with lines ending with a date in format dd/mm/yyyy see example below: a|b|c|08/01/2011 d|a|e|31/11/2010 e|d|f|20/11/2010 f|s|r|18/01/2011 What I would like to do is delete all lines with a date older than 30 days. With above example I should be left with a file... (5 Replies)
Discussion started by: fas1
5 Replies

8. Shell Programming and Scripting

Delete old date entries from a file

Hello, I have following log file. alog.log Oct 8 xxxxx Oct 9 xxxxx Oct 10 xxxxx Nov 8 xxxxx Nov 8 xxxxx . . . Jan 8 xxxxx I need to delete all the lines which are having date entries older than 60 days. So If the current date is Jan 9 2011 then it should delete all the lines upto... (3 Replies)
Discussion started by: Ravin83
3 Replies

9. UNIX for Advanced & Expert Users

Delete File Based On Date

Hi Xpert Out There I have a lots of file in this path : -rw-r----- 1 oracle dba 3954176 Dec 21 2006 1_2008.dbf -rw-r----- 1 oracle dba 887808 Dec 21 2006 1_2009.dbf -rw-r----- 1 oracle dba 143872 Dec 21 2006 1_2010.dbf -rw-r----- 1 oracle dba ... (3 Replies)
Discussion started by: adzuanamir
3 Replies

10. Filesystems, Disks and Memory

Delete file by date or by size

Hi Everyone, This forum has been a great help to me as a new newbie in Unix. Thanks to you all. My ? now is I have a log file that help me keep track of errors within my program. But the size of the log file keeps growing and I am wondering if there is any command that will help me to... (1 Reply)
Discussion started by: odogbolu98
1 Replies
Login or Register to Ask a Question