Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

As in the topic: I would like to remove files in a directory that have been modified in a particular date range. How can I do this ?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

The command GNU find is the way to go. For example, to delete all files in the current directory between 1 and 5 august, you can use the following command

find . -maxdepth 1 -newermt 2011-08-01 ! -newermt 2011-08-06 -delete

It is better to execute the command without the -delete action, first, to see the listing of interested files (a good substitute could be -ls that produce an ls-like listing).

Removing the -maxdepth 1 specification will traverse all subdirectories, too.

You can also specify hours, for example

find . -maxdepth 1 -newermt '2011-08-01 10:01:59' \
                 ! -newermt '2011-08-06 23:01:00' -delete

Be warned to not remove single quotes, that protect spaces between date and time.

The character ! is a negation, it should be read: newer that this date but not newer that this other date.

share|improve this answer
    
Thanks for reply. Can I also use something to choose hours' range ? And what is the '!' used for ? –  Patryk Aug 14 '11 at 9:44
    
The ! is a not. In this example: Not newer than 2011-08-06. –  con-f-use Aug 14 '11 at 9:47
    
@lordmonkey: see my editing in the answer –  enzotib Aug 14 '11 at 9:50
    
Thanks a lot :) that was really useful. –  Patryk Aug 14 '11 at 9:56
1  
+1. I didn't know -delete predicate. Maybe it's not needed, but I will add -type f. –  Michał Šrajer Aug 14 '11 at 10:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.