• Skip to main content
  • Skip to primary navigation
  • Skip to footer

We use Google Analytics and Microsoft Clarity to see which articles help readers and where pages need work. They set cookies only if you agree. The site works the same either way. Privacy policy

TechLila Logo

TechLila

Bleeding Edge, Always

  • Android on PC

    Android on PC

    Full Android builds, emulators and what runs well on the hardware you have.

    All Android on PC guides

    Start with

    • Best Android OS for PC
    • Android OS for low-end PCs
    • Android OS for laptops
    • Emulators for Android games on PC
    • Run Android apps on a PC

    Also on Android

    • Android versions, with their names
    • Best Android dialer apps
    • Android launchers
    • Best Android ROMs
    • LineageOS review
  • PC Customization

    PC Customization

    Skins, themes, dark modes and sound tweaks that make Windows and your media players your own.

    All customization guides

    Start with

    • VLC skins
    • Windows 11 dark themes
    • Kodi skins
    • Windows 10 sound equalizers
    • Change folder colors in Windows

    Windows and VLC tweaks

    • Windows 11 debloat
    • Windows 10 tips and tricks
    • Free Windows tools
    • Install Windows 11 26H2
    • VLC shortcuts
  • Mac Apps

    Mac Apps

    Launchers, Setapp and the apps worth paying for on a Mac.

    All Mac app guides

    Start with

    • Best Mac apps
    • Raycast vs Spotlight
    • Raycast alternatives
    • Setapp review
    • Best Setapp apps
    • Run Windows apps on a Mac

    Apple in numbers

    • macOS market share
    • Mac gaming
    • Apple services revenue
    • Mac sales
    • macOS developers
  • Topics

    Topics

    Every TechLila guide, sorted by subject.

    All articles, newest first

    Computers

    • Windows
    • Mac
    • Linux
    • All computer guides

    Phones and the web

    • Android
    • iPhone
    • Internet
    • Technology
  • Deals and Offers
  • Contact
TechLila Logo
FacebookPostLinkedInPinShares221
Linux terminal showing ls, grep and chmod commands next to Tux the Linux penguin
Up Next

Linux Commands Cheat Sheet: Essential Commands Grouped by Task

Delete Files Older than ‘x’ Days in Linux

TechLila

Delete Files Older than ‘x’ Days in Linux

Last updated on November 30, 2020 UTC
Avatar of Rajesh Namase Written by Rajesh Namase

Today, we will show a way to you by which you can delete files which are older than ‘X’ days. Suppose you want to delete files older than 7 days then this article will help you to do that. The find utility permits you to pass in a couple of interesting arguments, including one to execute another command on each file. In order to figure out what files are older than a certain number of days, we’ll use this find utility and then use the rm command to delete them.

Warning: rm deletes files permanently, with no Trash or undo, and find also searches subfolders. Before you delete anything, run the list command shown below and check its output.

The command syntax is as follows:

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

We’re not liable for any data loss that may occur. We recommended you to the list the files and check before deleting them, by running the following command:

find /path/to/files* -mtime +5 -exec ls {} \;

Note: There are spaces between rm, {}, and \;

Delete Files Older than 5 Days
See also
How to Create Repository in Linux

Command Explanation:

The first argument in the above command is the path to the files. The second argument is -mtime is used to specify how many days old the file is. If you enter +5, it will find files older than five days. The last argument is -exec allows you to pass in a command such as rm. The {} \; at the end is required to terminate the command.

This should work on almost all versions of Linux like Ubuntu, Fedora, Red Hat, Suse, etc. If you are facing any problem which seems quite impossible here then let us know in the comments below.

More on TechLila

  • Linux Hands On: The Advanced Terminal Commands
  • Linux for Kids – A List of Best Linux Distro for Kids
  • Linux Mint vs Ubuntu – Which Distro Should You Choose?

Sharing is Caring

FacebookPostLinkedInPinShares221
Avatar of Rajesh Namase

Rajesh Namase

Rajesh Namase is an entrepreneur from Pune, India, and the founder of TechLila, where he writes about technology, SEO and the tools he builds. He started blogging in September 2010 on Namase.com, the blog TechLila grew out of, and is counted among India's pioneering tech bloggers. He is also the co-founder and CEO of WebGeeksMedia. Away from work, it's badminton and yoga.

Category

  • Linux

Tags

Linux Commands, Linux Tutorials

Reader Interactions

What people are saying

  1. Suraj Salunkhe

    I used this trick! Awesome trick! Thanks for sharing such a good trick :)

    Reply
  2. Amruta Patil

    Ya I used this cammand during practice session of RHCE.

    Reply
  3. Ben

    find ./* -mtime +5 -exec ls {} \; >> ./5DaysOld

    alias 5DayLs="find ./* -mtime +5 -exec ls {} \; >> ./5DaysOld"

    Crufted something together out of your suggestion here. A method for making a plain text file listing of files five days old. Easier to keep a record for viewing prior to removing files. Users can then use cat, vi, vim, gedit, emacs to see what they may toss out.

    find ./* -mtime +5 -exec ls {} \; >> ./5DaysOld

    Breaking it down ./ is the current directory. Find using a current directory often safer than using the / cannon. >> appends to a file, if it does not exist, it gets created.

    Even thought it may useful as alias. Probably could / should port it to a shell script first. Being lazy though, late here.

    Reply
  4. Ben Piper

    Thanks! I used this syntax in a cron job.

    Reply
  5. Vivian

    The good tip to delete files older than certain days. However, if your argument list is long (I think the max is 65535 or something along those lines) this will not work.

    I have a custom script that does the job:


    #!/bin/bash

    cd /directory-to-be-checked
    allfiles=`ls`
    NOW=`date +%s`
    for filename in $allfiles
    do
    OLD=`stat -c %Z $filename`
    if [ "$2" == "" ]
    then
    DELAY=86400
    else
    let "DELAY=$2 * 86400"
    fi
    AGE=`expr $NOW - $OLD`
    #echo "Delay is $DELAY and Age is $AGE"
    #exit 0
    if [ $AGE -gt $DELAY ]
    then
    #echo "File $filename is $AGE"
    fstamp=`ls -l $filename`
    if [ "$1" == "DEL" ]
    then
    echo "Deleting file $filename"
    `rm -f $filename`
    else
    echo "$fstamp"
    fi
    fi

    Done.

    Reply
    • Rajesh Namase

      Thanks for giving the script.

      Reply
      • Vivian

        Your welcome Namase.

        Call the script like so:

        ./scriptname.sh DEL numDays

        If numDays is not specified it will use the minimum of 1 so make sure you specify numDays. I’m not liable for how you use this script and for any data loss that may occur.

        Reply
  6. Pawan

    if I want Delete Dirs and files from 30 days old to my remote machine and one thing excluding 1 and 2 or 3 dirs and so how can i done this ..?? Please share your thoughts.

    Reply
  7. Ali

    I use this technique it is very useful for deleting trc file in oracle

    Reply
  8. Ethan

    Thanks for sharing this useful command for all users. We can easily delete older files in Linux. I am also looking for it and finally got the solution here.

    Reply
  9. Greg

    Does this get files in sub-directories as well? If so, how would I do it so it doesn’t?

    find /home/greg/logs -type f -mtime +5 -exec rm {} ;

    Reply
  10. Suraj Salunkhe

    I was looking for such command. Really helpful. And thanks for sharing the script, Vivian.

    Reply
  11. Suresh Musale

    Dear Team,
    I need to remove directories which are of 14 days old and it should remove Directories if it has only numbers
    For Example :
    Direcory name 20180424 should delete
    Direcory name 20180424backup should delete

    Actually Find command with regex and mtime is not working.

    find path regex mtime -14 | xargs rm -rf {}\;
    is not working

    Reply

Add Your Comment Cancel reply

Comment (required)

TechLila Footer Logo TechLila Footer Text Logo

Footer

About

TechLila’s mission is to help you get more out of the computer and phone in front of you. Since September 2010 we have published practical guides, app comparisons and technology statistics for people who use Windows, Mac, Linux, Android and iPhone.

Site Links

  • About
  • Contact Us
  • Brand Assets
  • Feedback Policy

Standards

  • Editorial Policy
  • Fact-Check Policy
  • Corrections Policy
  • Ethics Policy
  • Stats Methodology
  • How We Test

Legal

  • Privacy Policy
  • Terms
  • Disclaimer

Copyright © 2010–2026 TechLila. All Rights Reserved. · Cookie settings

Back to top