How to delete files from a specific date?


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers How to delete files from a specific date?
# 1  
Old 05-15-2012
How to delete files from a specific date?

Guys, I am wondering how to remove files for a specific date in a directory?

for instance when I do ls -l , i see many files. And i want to delete files for date May 15:
Code:
58252015 May 10 03:45 my_05102012.log
58252015 May 15 06:45 my_05152012.log

Thanks
# 2  
Old 05-15-2012
based on your sample:

Code:
cd /directory
rm *05152012.log

I am assuming you mean to delete based on file name not UNIX file time/date
# 3  
Old 05-15-2012
Not the file name.

I am looking for the Unix file time and date.
# 4  
Old 05-16-2012
One way:

Code:
touch -t 201205150000.00 /tmp/PID$$.marker1
touch -t 201205160000.00 /tmp/PID$$.marker2
find /your/directory -type f -newer /tmp/PID$$.marker1 ! -newer /tmp/PID$$.marker2 -ls
rm /tmp/PID$$.*



This code justs lists the regular files that were modified on 2012/05/15. You can drop the -ls from the find and pipe it to xargs rm -f once you are sure that the list of files produced is what you want to delete.
# 5  
Old 05-16-2012
Thanks agama.

Any simpler way to do?
# 6  
Old 05-16-2012
Maybe, but more room for error:

Code:
ls -l /your/directory |grep "^-.*May 15" #|xargs rm



Remove the hash when you're sure. It deletes all that appear to be regular files (leading dash in the ls output) and have the date (May 15) somewhere in the rest of the record. Realise that it will delete files from May 15th of previous years, as well as any files that have that string in the name. Like I said, not exact and lots of room for error.
# 7  
Old 05-16-2012
Code:
find /path/to/directory -ctime <integer-number-of-days-from-today> -type f |xargs rm

e.g. today is May 16 and May 12 is the target day (4 days ago) and my files are in folder mydir
Code:
cd mydir
find . -ctime -4 -type f |xargs rm

Please test it by making a prototype directory (or making a backup of your current directories) before you execute the command.

Update: it will delete all files made between now and 4 days ago (perhaps not what you're looking for - apologies).
There's something mentioned here: http://www.linuxquestions.org/questi...ate-245888/#15

Finally:
Code:
cd mydir
find  . -maxdepth 1 -ctime -4 -type f -not -ctime -3 |xargs rm

As mentioned earlier, please use with caution :-)

Last edited by eosbuddy; 05-16-2012 at 01:16 AM.. Reason: UPDATE
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

5. Shell Programming and Scripting

How to delete files in specific dir

I've got a problem how to delete files from specific directory. I don't want do it recursively but only in specific directory. So, command like find /home/dirname/ -mtime +8 -type f -exec rm {} \; is not for me because this command will remove ifs files in /home/dirname and all... (2 Replies)
Discussion started by: zukes
2 Replies

6. Shell Programming and Scripting

Delete files created before specific date.

There is a system logging a huge amount of data and we need to delete some of the older logs .I mean the files that are created before one week from today. Here is a listing of files that are sitting there: /usr/WebSphere/AppServer/logs # ls -l -rw-r--r-- 1 root system 3740694 May... (5 Replies)
Discussion started by: moustafashawky
5 Replies

7. Shell Programming and Scripting

Delete lines prior to a specific date in a log file.

Hi all. I have a database log file in which log data get appended to it daily. I want to do a automatic maintainence of this log by going through the log and deleting lines belonging to a certain date. How should i do it? Please help. Thanks. Example. To delete all lines prior to Jun... (4 Replies)
Discussion started by: ahSher
4 Replies

8. Shell Programming and Scripting

How do I delete all files that contain specific text?

Hi all, I want to remove the files from a folder whcih contain a specific text in it. eg: If i have the files like xxxx.NAME.adadd.ajfaj.kjfha agsg.NAME.dfshd.djsh.sdjhf asgd.NAME2.sdhj.djhf.sjdhf shdd.NAME2.dhse.tywe.eyio How to remove the files which contain the pattern "NAME"... (3 Replies)
Discussion started by: sparks
3 Replies

9. UNIX for Dummies Questions & Answers

How to delete specific files only

Hello, I have these files in my directory. ABC123 ABC12.sls.20080809111121 ABC233 ABC12.sls.20080403123212 ABC543 ABC12.sls.20080804231212 ABC323 ABC12.sls.20080809111232 ABC765 ABC12.sls.20080809112343 ABC654 ABC12.sls.20080809113133 I want to delete only files in first... (2 Replies)
Discussion started by: i.scientist
2 Replies

10. Shell Programming and Scripting

delete files in specific directory

i have a directory "ABC" with lots of old files and sub directories in it. the issue now is i want to delete away files which are older than 15 days in "ABC" without deleting the files in the sub directories and without deleting the sub directory. i tried using find command but it will drill down... (2 Replies)
Discussion started by: legato
2 Replies
Login or Register to Ask a Question