Remove Files created in the previous month

Posted on 2006-06-02
Last Modified: 2010-04-21
On the first day of the month, I would like to issue a command that would remove all files in a directory that were created in the previous month.  Is there a command that will allow me to remove all files that were created in the previous month?  I am running out of space and need a quick command that will accomplish this.

Thank you,

Question by:bruingjt
    LVL 6

    Accepted Solution

    Use find:

    find /yourdirectory -mtime +30 -exec rm {} \;

    You might want to try it without the -exec rm {} \; part first to ensure that you're going to delete the files you think you are.

    this command will keep only the files that were created/modified in the last
    30 days, and delete the olders.
    LVL 61

    Expert Comment

    A bit more efficient:

    find /yourdirectory -type f -ctime +30 | xargs rm -if

    remove i in last argument to get rid of prompts.
    LVL 48

    Expert Comment

    gheist, why ctime?  mtime would give a better pointer at 'create' time.

    bruinqit.  How strict do you want to be on the 'previous month'?  The solutions above will work, but if the previous month is February, then you are going to get some January files deleted as well.
    LVL 1

    Author Comment


    I understand that if February is the current month some files created in January would be removed,  but the January files should get deleted when the command runs in February, correct?
    LVL 48

    Expert Comment


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
    FreeBSD on EC2 FreeBSD ( is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
    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…
    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.:

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now