Visit Our UNIX and Linux User Community

Deleting the files between particular dates

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting the files between particular dates
# 1  
Old 09-10-2014
Linux Deleting the files between particular dates


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.


Last edited by rbatte1; 09-10-2014 at 09:22 AM.. Reason: Added ICODE tags and corrected capital letters
# 2  
Old 09-10-2014

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.


# 3  
Old 09-10-2014
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 {} +

# 4  
Old 09-10-2014
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,

Previous Thread | Next Thread
Test Your Knowledge in Computers #440
Difficulty: Medium
The term "GPU" was coined by Nvidia in 1994.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to Move files of different dates

Hi, Currently I'm moving the files based on date like below. "mv *20150901* backup_folder" - Limitation: can move only 1 day files to backup folder. I want to move the files of different dates like 20150901,02, 03, 04..... Is there any single command to do it. Thanks in advance!! (2 Replies)
Discussion started by: prakashs1218
2 Replies

2. Shell Programming and Scripting

Files between two dates in UNIX

Hi Team, I need to connect to a prod server and need to get the files which falls between two dates. I should not create ant files on that machine. I am using korn shell. Your help is very much appreciated. Vinay (13 Replies)
Discussion started by: gvkumar25
13 Replies

3. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

4. AIX

Copy files for particular dates

Hi, I need to copy particular date files from one directory to another. For example, I have thousands of files in /home/usr From this I need to copy only particular date files (each date contains thousand number of files) to some directory of another server. Could anyone please help me... (3 Replies)
Discussion started by: teddy2882
3 Replies

5. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

6. UNIX for Dummies Questions & Answers

Deleting the files comparing the creation dates

Hi Gurus, I am new to unix. I have a requirement where i need to delete some files in a folder twice a week. Suppose i have a folder AAA. In that i have files from 01/04/2008 to 10/04/2008 I want to remove all the files except last 3 days i.e., 10,9th & 8th. Every week twice we want to... (2 Replies)
Discussion started by: pssandeep
2 Replies

7. UNIX for Dummies Questions & Answers

files between any two given dates

Can any one help me in getting all the files between any two given dates.. (8 Replies)
Discussion started by: thanuman
8 Replies

8. UNIX for Dummies Questions & Answers

Files and dates

Hello all. I am trying to do a file listing on a particular directory by date. I need to list the files and their directories that have a timestamp between Dec-1-2006 and Jan-1-2007. Any help would be greatly appreciated. (1 Reply)
Discussion started by: mastachef
1 Replies

9. UNIX for Dummies Questions & Answers

deleting files with dates 3 months ago

please help me with this????? :confused: :confused: i need to create a program that will run in unix that will delete all files in a given directory that is at least 3 months old. first the program will need to automatically know what date it is right now to determine the files it will... (3 Replies)
Discussion started by: godalle
3 Replies

10. UNIX for Dummies Questions & Answers

Remove files by dates

I've tried every way possible to remove files by date and nothing seems to work. Does anyone have an idea how to remove files by dates? Thanks in advance.... (1 Reply)
Discussion started by: dman110168
1 Replies

Featured Tech Videos