Removing files from a particular month

Hi All

I am trying to remove files from february, only using the following commands:

find . -mtime 70 -exec rm {} \;

, but I dont seem to get them deleted.

But I am confused, now, because I have been told to use

-atime

, like

find . -atime 75 -exec rm {} \;

Please can you help!

FR

This sentence is ambiguous and I can think of at least four different meanings:
1) Delete every file created since from 1 February 2012 to today (inclusive).
2) Delete every file which was created on or before 1 February 2012
3) Delete every file which was created between 1 February 2012 and 29 February 2012 (inclusive).
4) Delete every file which was created in February in any year.

The syntax you post will not work and it will probably be a much more complicated syntax of find on HP-UX. But let's find out what you want to delete first!

Please carefully describe the range of dates (inclusive) of the files you want to delete. I would suggest that you think in terms of -mtime because -atime is useless for this purpose because accessing the file changes the atime stamp.

Hi

Thank you for your response.

What I really want is to remove files from 1st of february 2012, to 29th of february 2012 ONLY.
Sorry if I mislead you.

FR

From today's date (03 May), this should work:

find . -type f  -mtime -$(( 3+30+31+29 )) -mtime +$(( 3+30+30 )) -exec ls -lv '{}' \;

Check the output first, confirm the dates before proceeding with delete.

@admin_xor
Not checked the numbers (noted that the above post has been revised) but if we adopt that approach, the syntax should be:

find . -type f \( -mtime -98 -a -mtime +69 \) -exec ls -lad '{}' \;

However the results from that command depend on exactly what time-of-day and on what date the command is run.

The quick and dirty approach is to use the month name from ls -la but this is frought with problems if there are old files (relevant or not) in any directory. With Linux the stat command is useful for this sort of script.

My approach which is pretty much 100% accurate on most unix Operating Systems.

touch -t 201202010000 /tmp/start_date  # File dated First day of February
touch -t 201203010000 /tmp/end_date    # File dated First day of March
find . -type f \( -newer /tmp/start_date -a ! -newer /tmp/end_date \) -exec ls -lad {} \;

Scripts to automate this sort of process can get a bit long when you need to calculate the restrospective dates but they are essentially easy to write.

2 Likes

Awesome!! :b:

Regarding the syntax, for "and" operation, I usually do not bother about the -a operator. That syntax I use for "or" operation with -o. Here's what GNU find's man page has to say about it:

The  expression  is  made up of options (which affect overall operation
       rather than the processing of a specific file, and always return true),
       tests  (which  return  a  true or false value), and actions (which have
       side effects and return a true or false value), all separated by opera-
       tors.  -and is assumed where the operator is omitted.

And here's the proof:

# ls -la /tmp/file*
-rw-r--r--. 1 root root 0 Feb  1 08:33 /tmp/file1
-rw-r--r--. 1 root root 0 Feb 28 08:33 /tmp/file2
-rw-r--r--. 1 root root 0 Feb 14 08:33 /tmp/file3
-rw-r--r--. 1 root root 0 Jan 14 08:33 /tmp/file4
# find /tmp -mtime -$(( 3+30+31+29 )) -mtime +$(( 3+30+30 )) -exec ls -lv '{}>
-rw-r--r--. 1 root root 0 Feb 28 08:33 /tmp/file2
-rw-r--r--. 1 root root 0 Feb  1 08:33 /tmp/file1
-rw-r--r--. 1 root root 0 Feb 14 08:33 /tmp/file3

But, again, HP-UX find may differ. This is tested with GNU find :slight_smile:

Thanks Methyl.

@admin_xor
The GNU find is indeed quite different from the find shipped with HP-UX (and other standard unix systems). The implicit "and" is a welcome addition to the find command.
Still beware of the time-of-day with either version using the syntax posted (-mtime is relative to the current date-and-time). The GNU find has the addition of start-of-day and end-of-day parameters which would be so useful for this question ... but we have to do it the old way on HP-UX.

Hi all, thank you very much for your contribution.
Both of the following worked well, in HP-UX although I rather use the script:

find . -type f \( -mtime -98 -a -mtime +69 \) -exec ls -lad '{}' \; | more

and

#!/bin/sh
touch -mt 201202110000 /tmp/start_date
touch -mt 201202292359 /tmp/end_date
find . -type f \( -newer /tmp/start_date -a ! -newer /tmp/end_date \) -exec ls -lad {} \;

Now I only need to replace the

-exec ls -lad {} \;

with

-exec rm {} \;

or

-exec rm -i {} \;

if I want to prompt for confirmation right?

FR

Obviously ensure that your files appear on a backup before deleting anything and test the "ls" version of the command first to be sure that you will be deleting the correct files.
If you want to be prompted for each file deletion, there is the -ok switch in HP-UX find - see man find .

-ok rm {} \;