Article

Find and delete old files in linux

I have a few of those directories that files tend to pile up in. I don’t need the files but I also don’t take the time to delete them. Pruning these old files is a good thing to keep your used disk space under control as well as your sanity. Find is a great tool to do this, its extremely flexible I recommend you read the man page next time your bored.

find /directory/with/old/files -mtime +120 -exec rm {} \;

Here we use find to find files that are older than 120 days. The “older” here being the key word. So they might actually be older than 120 days but we are using mtime and mtime is the time when the actual contents of a file was last modified.

Comments (3 comments)

Instead of “-exec rm {} \;” recent versions of find support “-delete”, which does exactly that and it’s faster (because it doesn’t spawn another process for every file). See find’s man page for more :)

Sotiris Tsimbonis / April 9th, 2008, 7:10 am

Ah thanks for the tip, -delete is much better

cmdln / April 9th, 2008, 9:05 am

[…] some lovely feedback from Sotiris Tsimbonis on my last post Find and delete old files in linux I have a better (faster) […]

Find and delete old files in linux redux | cmdln.org / April 9th, 2008, 9:21 am

What do you think?