How to delete files by age

Posted on 2007-10-17
Last Modified: 2013-12-16
I am running a Linux system (but I don't know much Linux) and I would like to be able to have a script that will delete all files in a directory that are older than 5 days.  Can anyone suggest a way to do this?  Thanks in advance!
Question by:dkreines
    LVL 12

    Accepted Solution

    find /path/to/files -mtime +5 -exec rm -f {} \;
    LVL 12

    Expert Comment

    oh, I should note that will delete anything not modified for the past 5 days. To do when it was created, change mtime to ctime

    LVL 40

    Expert Comment


    If you are not sure about the files to be deleted, you may add the -i option to the rm command to confirm before deleting (but this wii prompt for each file, and if the number of files to be deleted is large, then you spend some time doing so).

    LVL 48

    Expert Comment

    find /path/to/files -mtime +5 -exec rm -f {} \;

    should really be

    find /path/to/files -mtime +5 -type f -exec rm -f {} \;


    -type f

    specifies to only match files.


    ctime is not creation time, it's change time.  Unix/Linux filesystems don't store file creation time.
    LVL 34

    Assisted Solution

    by:Duncan Roe
    -exec invokes the rm program for every file. More efficient would be:

    find /path/to/files -mtime +5 -print | xargs rm

    Depending on your circumstances, you might want to consider using -atime (last access time) to avoid deleting older files that are still potentially useful. You need to use --atime-preserve in your tar backups for the access time to be possibly useful.
    LVL 3

    Expert Comment

    Before executing them look at 'man find' and search for -mtime, -ctime, -atime options. Read them and decide what files you want to delete. DON'T run in on any system files.

    Make sure that you will delete right files:

    find /path/to/files -ctime[mtime][atime] +5 -type f

    If you see right files in the output, only them add -exec rm -fv {} \;
    LVL 34

    Expert Comment

    by:Duncan Roe
    Or add | xargs rm -fv
    LVL 1

    Expert Comment

    I use tmpreaper package (in debian) to clear certain tmp-dirs automatically. It uses commands described above, It looks to subdirs and does not recursive delete dirs if they contain files modified/accessed desired days ago. It has also "blacklist" to prevent certain files to be deleted.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    12 Experts available now in Live!

    Get 1:1 Help Now