Operating System - HP-UX
1827293 Members
2896 Online
109717 Solutions
New Discussion

How to rm files by extension and year?

 
SOLVED
Go to solution
Stacey Akerstrom
Frequent Advisor

How to rm files by extension and year?

I need to rm a ton of files for the sake of gaining back space.
The files I want to remove have an extension of .OLD, but I want to rm the ones prior to 2003 *only*.
I know it's possible, but I don't have scripting experience.

Thanks-
10 REPLIES 10
Pete Randall
Outstanding Contributor
Solution

Re: How to rm files by extension and year?

Try this:

find /dir -name '*.OLD' -mtime +285 |xargs rm


That should search from the starting directory "/dir" down, finding all files with a name ending in .OLD that haven't been touched since 285 days ago (roughly the beginning of the year, by my calculations), and removing them.


Pete


Pete
Vitek Pepas
Valued Contributor

Re: How to rm files by extension and year?

find . -name "*.OLD" -mtime +`date +%j` | xargs -i rm {}
It will remove all *.OLD files created before 01/01/03.
Pete Randall
Outstanding Contributor

Re: How to rm files by extension and year?

Stacey,

I like Vitek's solution much better - mostly because my manual date computation was off by about a month.


Pete


Pete
Stacey Akerstrom
Frequent Advisor

Re: How to rm files by extension and year?

Perfect, gentlemen!!!
Thanks so much.
Vitek Pepas
Valued Contributor

Re: How to rm files by extension and year?

Pete is right about using '/dir' instead '.', unless you will run it from directory where your files are. But I think he got the number of days wrong - it is 258 today.
Jeff Schussele
Honored Contributor

Re: How to rm files by extension and year?

Hi,

Use the find command to find these & delete them.
First use the touch command to create a file dated midnight 01/01/03 as follows:

touch -t 200301010000 /tmp/pre2003

Then use the find command using the and operator to find & remove the *.OLD files older than this.

find / \(-name "*.OLD" -a ! -newer /tmp/pre2003 \) -exec rm {} \;

HINT - Use ll in place of rm *first* to list the files before using rm JUST TO BE SURE.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Jeffrey W. Stewart
Frequent Advisor

Re: How to rm files by extension and year?

Vitek Pepas

find . -name "*.OLD" -mtime +`date +%j` | xargs -i rm {}
It will remove all *.OLD files created before 01/01/03.

Could someone explain this. I use the find and mtime option but the +`date +%j` I don't
understand. Thanks.
Jeffrey W. Stewart
Frequent Advisor

Re: How to rm files by extension and year?

Vitek Pepas

find . -name "*.OLD" -mtime +`date +%j` | xargs -i rm {}
It will remove all *.OLD files created before 01/01/03.

Could someone explain this. I use the find and mtime option but the +`date +%j` I don't
understand. Thanks.
Stacey Akerstrom
Frequent Advisor

Re: How to rm files by extension and year?

Jeffrey, I could be wrong here, but I believe it's implies using a Julian calendar...
Jeff Schussele
Honored Contributor

Re: How to rm files by extension and year?

Hi Jeff,

%j is the day of the year in a 3 digit number.
So Vitek is fuuneling the number of days to go *back* to the -mtime paramter using the command
date +%j

Try this command
date +%j - You should get 258 today & 259 tomorrow.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!