CentOS Delete Files

Posted on 2012-09-12
Last Modified: 2012-09-19
Using the command line in CentOS how do i delete all files within a specific directory that have a last modified date older than June 1, 2012?
Question by:deklinm
    LVL 68

    Assisted Solution

    touch -t 201206010000 /tmp/from
    find /specific/directory -type f  ! -newer /tmp/from | xargs rm
    rm /tmp/from

    The above will recurse into subdirectories of /specific/directory.
    If this is not desired add "-maxdepth 1" to the arguments to "find":

    touch -t 201206010000 /tmp/from
    find /specific/directory -maxdepth 1 -type f  ! -newer /tmp/from | xargs rm
    rm /tmp/from
    LVL 76

    Assisted Solution

    using the -mtime +90 is another option, but this will adjust unlike the WMP example based on the date that you run it. -mtime +90 means a file older than 90 days
    Switching -mtime -90 will mean less than 90 days old.
    LVL 6

    Accepted Solution

    This should help:

    cd /path/to/your/folder; find -type f -mtime +`echo $(( ( $(date +%s) - $(date -d "06/01/2012" +%s) ) /(24 * 60 * 60 ) ))` -print;

    Open in new window

    I included the print so that you will be able to see what files are going to be deleted. When you have verified that they are the files that you actually want to delete you can change the print to delete and it will remove the files. Here a couple alternatives if you, like me, are a careful administrator/user who wants to be sure they are deleting the right files:

    This will tell you what files you are actually going to delete and their current modified dates, before you actually delete them (the head -20 means it will only list the first 20 files, you just need a few to verify the dates):

    cd /path/to/your/folder; find -type f -mtime +`echo $(( ( $(date +%s) - $(date -d "06/01/2012" +%s) ) /(24 * 60 * 60 ) ))` -exec ls -lh {} \; 2> /dev/null | head -20

    Open in new window

    You can also change the actually date you see in this one-liner in case you need to reuse it to delete some other files as you are doing here, i.e. $(date -d >>>"06/01/2012"<<< +%s)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    Join Greg Farro and Ethan Banks from Packet Pushers ( and Greg Ross from Paessler ( for a discussion about smart network …
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    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.

    729 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

    23 Experts available now in Live!

    Get 1:1 Help Now