Delete Files Older than ‘x’ Days in Linux

Delete Files Older than ‘x’ Days in Linux

Today, we we'll 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. 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

Recommended Readings:
Five Useful Shell – Prompt Tips.
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 version of Linux like Ubuntu, Fedora, Red Hat, Suse, etc. If you are facing any problem which seems quiet impossible here then let us know in comments below.


Join Our Email Newsletter

Like what you read here in this blog post? Get free blog updates in your Email!

About Rajesh Namase

Rajesh Namase is a professional blogger and founder of TechLila. He has a great passion in blogging and SEO. If you’d like to connect with him, use our Contact Us page.

Rajesh has written 218 awesome articles for us at TechLila.

Comments

  1. Ben says

    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.

  2. says

    Good tip to delete files older that 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.

  3. Pawan says

    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.

 Comment Policy

Your words are your own, so be nice and helpful if you can. Please, only use your REAL NAME, not your business name or keywords. Using business name or keywords instead of your real name will lead to the comment being deleted. Anonymous commenting is not allowed either. Limit the amount of links submitted in your comment. We accept clean XHTML in comments, but don't overdo it please. You can wrap code in [lang-name][/lang-name] tags.


Tell us what you're thinking...

If you want a picture to show with your comment, then get Gravatar!

CommentLuv badge