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

Posted on 2012-09-05
Last Modified: 2012-09-20
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?
Question by:sikyala
    LVL 20

    Accepted Solution

    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

    Author Comment

    the files are never updated they are just created is there a command for records that have been created
    LVL 48

    Assisted Solution

    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
    LVL 20

    Expert Comment

    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')

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
    Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now