Remove Files created in the previous month

Posted on 2006-06-02
Medium Priority
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

Accepted Solution

bira earned 2000 total points
ID: 16820761
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 62

Expert Comment

ID: 16836936
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

ID: 16837201
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.

Author Comment

ID: 16838989

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

ID: 16839430

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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.:
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

809 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