delete file older than N days | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

delete file older than N days

Shell Programming and Scripting


Tags
mtime

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-11-2007
ericaworld ericaworld is offline
Registered User
 
Join Date: May 2007
Last Activity: 11 July 2007, 12:40 PM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
delete file older than N days

Hi, All,
I'd like to delete files older than 1 day.

I thought the following command

find /your_directory -mtime +1-exec rm -f {} \;

will do the work, but not so, it seems like it won't delete files unless it is 2 days old or older. the files between 1 day and 2 days old does not get deleted, so does +1 actually means 2 days? if not so, why it won't delete files between 1 and 2 days old? can someone please help me?
Sponsored Links
    #2  
Old 07-11-2007
ericaworld ericaworld is offline
Registered User
 
Join Date: May 2007
Last Activity: 11 July 2007, 12:40 PM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
delete files older than 1 day

Hi, All,
I'd like to delete files older than 1 day.
I thought the following command :

find /your_directory -mtime +1-exec rm -f {} \;

will do the work, but not so, it seems like it won't delete files unless it is 2 days old or older. the files between 1 day and 2 days old won't get deleted, so does +1 actually means 2 days? if not so, why it won't delete files between 1 and 2 days old? can someone please help me?
Sponsored Links
    #3  
Old 07-11-2007
dangral dangral is offline Forum Advisor  
Registered User
 
Join Date: Oct 2002
Last Activity: 31 July 2013, 6:52 PM EDT
Posts: 723
Thanks: 3
Thanked 4 Times in 4 Posts
That command will delete anything that is older than 24 hours from when the command was run.
    #4  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
Erika,
As you said:
Quote:
I'd like to delete files older than 1 day.
the files between 1 day and 2 days old won't get deleted, so does +1 actually means 2 days?
Yes, the "find" has three options for number of days:
1) +n --> More than 'n' days ago.
2) -n --> Less than 'n' days ago.
3) n ---> Exactly 'n' days ago.

Thus, if you specify '+1', it means more than 1 day ago.
Sponsored Links
    #5  
Old 07-12-2007
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
Actually, date ranges in the find command do not work quite how you expect them to.

1 means 24 hours old
-1 means less than 24 hours old
+0 means more than 24 hours old
+1 means more than 48 hours old
Demonstration at the ksh prompt...
Code:
$ for i in 09 10 11 12;do touch -t 200707${i}0000  July_${i};done
$ ls -o
total 0
-rw-r--r--    1 Ygor          0 Jul  9 00:00 July_09
-rw-r--r--    1 Ygor          0 Jul 10 00:00 July_10
-rw-r--r--    1 Ygor          0 Jul 11 00:00 July_11
-rw-r--r--    1 Ygor          0 Jul 12 00:00 July_12
$ for j in 1 -1 +0 +1;do echo "[[ ${j} ]]";find . -type f -mtime ${j} -print;done
[[ 1 ]]
./July_11
[[ -1 ]]
./July_12
[[ +0 ]]
./July_09
./July_10
./July_11
[[ +1 ]]
./July_09
./July_10

The reason is that "+1" means 2 or more days old, i.e. more than 48 hours old.
Sponsored Links
    #6  
Old 07-12-2007
bigearsbilly bigearsbilly is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 21 October 2014, 9:54 AM EDT
Location: Southern England
Posts: 245
Thanks: 0
Thanked 10 Times in 10 Posts
if you need to be really precise I usually do summink like:


Code:
touch -t YYYYMMDDhhmmss 111
find . -older 111 | xargs rm

Sponsored Links
    #7  
Old 07-12-2007
psiva_arul's Avatar
psiva_arul psiva_arul is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 19 November 2009, 6:58 AM EST
Location: Bangalore, India
Posts: 97
Thanks: 0
Thanked 1 Time in 1 Post
How to find the Cron entrys path in UNIX server?

Actually, i wanted know about the cron entry path in my UNIX server?
if i issued the crontab -l it will listout the all cront entries in the current server, But i wanted to know about the PATH where all the cron entries is stored?


Please, any one help me.

Thanks,
Siva.P
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do i delete files older than 15 days in AIX? bluesteel AIX 4 03-31-2010 11:23 AM
delete files more than 15 days older ali560045 Shell Programming and Scripting 3 11-28-2008 04:43 AM
Delete user file(s) older then 'X' days ?? varungupta UNIX for Advanced & Expert Users 2 08-24-2007 05:01 AM
How can I delete files older than 7 days? odogbolu98 UNIX for Dummies Questions & Answers 3 02-26-2002 07:35 PM



All times are GMT -4. The time now is 02:24 AM.