1836014 Members
2388 Online
110088 Solutions
New Discussion

grep and remove

 
SOLVED
Go to solution
Jade Bulante
Frequent Advisor

grep and remove

I'm trying to list files inside a directory that includes all of Jan - Dec 2000, after which I want to delete all of them since it's building up space.

Is there of syntax I should use to grab all files between jan through Dec and delete it also??

Need help..

Jade
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: grep and remove

Hi:

We can take advantage of JulianDays to do this:
Today 09/06/2001 is JD 2452159; 01/01/2000 is JD 2451545; 12/31/2000 is JD 2451910.
Today - 12/31/2000 = 249; Today - 01/01/2000 = 614.
So I want to get all regular files in the current directory files older than 248 days and younger than 613 days.

Here is a test version of the command:
find . -type f \( -mtime +248 -a -mtime -613 \) -exec ls -l {} \;

Here is a actual version of the command (MAKE CERTAIN YOU ARE IN THE DESIRED DIRECTORY):
find . -type f \( -mtime +248 -a -mtime -613 \) -exec rm {} \;

Man find for details. In case you are wondering about how I got those strange Julian Days, I've attached the script.

Regards, Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: grep and remove

Hi Jade:

Since you want to remove all files whose modification timestamp is for the year 2000, we could do this:

# cd
# ls -al | awk '$8~/2000/ {system("rm " $9)}'

This will remove all *files* (field-9 in the 'ls' output), while skipping directories (because 'rm' doesn't remove them), whose modification timestamp (field-8 in the 'ls') is "2000".

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: grep and remove

Hi Jade:

Here?s yet another way to meet your objective.

One of the values of using ?find? is that it recursively descends the directory you specify. Try this for a solution.

# cd mydir
# touch ?mt 200001010000 myref1
# touch ?mt 200012312359 myref2
# find . -type f -newer myref1 -a ! -newer myref2 | awk '{system("rm " $0)}'
# rm myref1 myref2

This will find and remove all files whose modification timestamp is more recent than ?myref1? (here, Jan 1, 2000 at 0000) and equal or less than ?myref2? (here, Dec 31, 2000 at 2359).

In reality, the file ?myref2? will be removed as a part of the ?find? process.

Regards!

...JRF...