• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

how do i delete old files based on the date they were created in linux redhat

I need to remove some old trace files. there are over 6,000 files and i only want to keep the files that were created in the last 3 months. how do i delete old files based on the date they were created in linux redhat?
0
sikyala
Asked:
sikyala
  • 2
  • 2
2 Solutions
 
edster9999Commented:
I use aline like this :

find /backup/weekly* -ctime +90 -print | xargs /bin/rm -f

This will find files inside /backup
which are called 'weekly*'
-ctime 90 will only pick files that have not been updated (note updated not created) more than 90 days ago.
-print then passes the name to the next command which is rm -f to delete it.

To test it, change the rm -f to ls -l
0
 
sikyalaSenior Database AdministratorAuthor Commented:
the files are never updated they are just created is there a command for records that have been created
0
 
TintinCommented:
Unix/Linux filesystems don't store the creation date of files.

For a lot of purposes, the last modification date is effectively the same as the creation date.

Assuming the trace files have a standard naming format, then do

find /dir/with/files -name "*.trc" -mtime +90 | xargs rm -f
0
 
edster9999Commented:
Agree - If the file is not updated it is just created and then left alone then the 'update' time is the 'creation' time.
The commands above will do what you want.

There is a minor difference between my command and the one from Tintin and that is the measure used to see when the file was changed.
he uses mtime and I use ctime :

       -mtime n
              File's data was last modified n*24 hours ago.  See the  comments
              for -atime to understand how rounding affects the interpretation
              of file modification times.

       -ctime n
              File's status was last changed n*24 hours ago.  See the comments
              for -atime to understand how rounding affects the interpretation
              of file status change times.

The mtime one will only spot if the data inside the file is changed.  The ctime one will spot any change to the file (like the date changing on it - if someone did 'touch filename')
0
 
sikyalaSenior Database AdministratorAuthor Commented:
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now