remove files and directories with acces time older than a given date

find <dir> -printf '%p : %A@\n' | awk '{FS=" : " ; if($2 < <time in epoc> ) print $1 ;}' | xargs rm --verbose -fr ;
remove files with access time older than a given date. If you want to remove files with a given modification time replace %A@ with %T@. Use %C@ for the modification time. The time is expressed in epoc but is easy to use any other format.
Sample Output
~/tmp$ find . -printf '%p : %A@\n' | awk '{FS=" : " ; if($2 < 1258734167 ) print $1 ;}' | xargs rm --verbose -fr ;
removed `./file_1'
removed `./file_2'
removed `./file_3'
removed `./file_4'
removed `./file_5'
removed `./file_6'
removed `./file_7'
removed `./file_8'
removed `./file_9'

By: angleto
2009-11-20 16:31:58

3 Alternatives + Submit Alt

What Others Think

find -atime -delete
TeacherTiger · 598 weeks and 2 days ago
@TeacherTiger is not the same, atime allow to specify a delta expressed in days The command I wrote allow to specify an exact date and time.
angleto · 598 weeks and 2 days ago
Then you can use "find -amin -n -delete", where n is the number of minutes in the past that the file was accessed. Note the minus sign before n is important (meaning "for less than n"). It's still not down to the second level, but usually you don't care about that, and this is *much* shorter and simpler.
TeacherTiger · 598 weeks and 2 days ago
again, the options -amin, -atime, -mtime, -mmin ... etc. allows only to specify a delta, you can ever calculate the delta, but with the command I wrote you don't need to do it. For instance, if you want delete the files with access time older than 2009 10 21, you don't need to calculate how much is the delta between the access time of the files and the time of command execution.
angleto · 598 weeks and 2 days ago
Hi angleto, I think your command is exactly whar I have been looking for. But unfortunately it is giving me an error on AIX find: 0652-017 -printf is not a valid option. Any suggestion? Thanks
rubenryhan · 589 weeks and 5 days ago
Hi rubenryhan, try to replace: find -printf '%p : %A@\n' | ... with: find | xargs stat -c '%n : %Y' | ...
angleto · 588 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: