Deleting files based on CreationTime | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Deleting files based on CreationTime

UNIX for Dummies Questions & Answers


Tags
linux, unix, unix commands

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-16-2012
manushi88 manushi88 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 23 April 2012, 4:42 AM EDT
Posts: 7
Thanks: 8
Thanked 0 Times in 0 Posts
Deleting files based on CreationTime

In a directory there are files which are generated daily.
Format of files, if its generated on 16th Apr 2012 is TEST_20120416.txt.

So I need to delete all the files which are older than 7 days. I tried doing this


Code:
#!/bin/ksh
find /data/Test/*.* -mtime +7 -exec rm -rf {} \;
exit 0

Now the problem is above code is deleting based on modification time but according to requirement file should delete based on creation time.Kindly help me out in deleting files based on filename(filename has timestamp).

Last edited by manushi88; 04-16-2012 at 09:10 AM.. Reason: code tags
Sponsored Links
    #2  
Old 04-16-2012
hroptatyr hroptatyr is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 28 May 2014, 12:43 AM EDT
Location: Berlin
Posts: 13
Thanks: 0
Thanked 2 Times in 2 Posts
That will be a problem as most unixes don't store the file creation time, and it's not specified in POSIX either.

Use

Code:
stat <FILE>

and check the Birth row to see if your unix supports it.

If so, use

Code:
find -newerBt <REFERENCE_TIME_STAMP> -exec ...

there's no -Btime in most finds, and the +/-N days syntax doesn't work with -newerXY.

Last edited by hroptatyr; 04-16-2012 at 05:10 AM.. Reason: provide find-based solution
The Following User Says Thank You to hroptatyr For This Useful Post:
manushi88 (04-16-2012)
Sponsored Links
    #3  
Old 04-16-2012
manushi88 manushi88 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 23 April 2012, 4:42 AM EDT
Posts: 7
Thanks: 8
Thanked 0 Times in 0 Posts
Hi,

Unix doesnt support birth row.
    #4  
Old 04-16-2012
hroptatyr hroptatyr is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 28 May 2014, 12:43 AM EDT
Location: Berlin
Posts: 13
Thanks: 0
Thanked 2 Times in 2 Posts
Yes, that's exactly what I'm saying. POSIX enforces ctime, mtime and atime, but birth time is not mentioned anywhere. Some versions of the stat(1) command will give you this row though, and many versions of find can deal with it too, but if the birth time cannot be obtained all checks will always fail.

AFAIK, the only unix that does support birth time is FreeBSD.
Sponsored Links
    #5  
Old 04-16-2012
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 25 November 2014, 5:09 PM EST
Posts: 6,396
Thanks: 287
Thanked 673 Times in 642 Posts
Please post what Operating System and version you are running.


Quote:
Now the problem is above code is deleting based on modification time
Did the command delete the wrong files? Did the command delete any files? As presented the pattern *.* does not match your filenames because there is no period in the name of the file. This is unix not MSDOS and there is no special meaning to the period character in filenames and no special meaning to file extensions (like .txt) beyond accepted convention.
Also surely you mean +7 (older) not -7 (newer) !

Try experimenting with command structure like this to see what might be deleted:

Code:
#!/bin/ksh
find /data/Test -type f -name 'TEST_*' -mtime +7 -print | while read filename
do
        # Remove echo when tested
        echo rm -f "${filename}"
done


Last edited by methyl; 04-16-2012 at 06:49 AM..
Sponsored Links
    #6  
Old 04-16-2012
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 22 December 2014, 4:23 PM EST
Location: Paris
Posts: 4,471
Thanks: 17
Thanked 482 Times in 434 Posts
Quote:
Originally Posted by hroptatyr View Post
AFAIK, the only unix that does support birth time is FreeBSD.
btrfs, zfs, ext4 and hfs+ also store file creation time.
The Following User Says Thank You to jlliagre For This Useful Post:
jim mcnamara (04-16-2012)
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
deleting files based on the suffix saggiboy10 UNIX for Dummies Questions & Answers 1 09-10-2011 09:03 AM
Deleting files based on their size Xterra Shell Programming and Scripting 4 06-25-2010 03:15 AM
Deleting the files based on the date's kandi.reddy Shell Programming and Scripting 1 02-17-2010 08:10 AM
deleting lines from a delimited files based on a 2nd file goldie363 UNIX for Dummies Questions & Answers 4 03-30-2009 03:10 PM
command for deleting log files based on some condition pulkit Shell Programming and Scripting 4 01-09-2008 06:17 AM



All times are GMT -4. The time now is 06:37 PM.