Deleting old files in Linux is often necessary. Often logs need to be periodically removed for example. Accomplishing this through bash scripts is a nuisance. Luckily there is the Find utility, it allows a few very interesting arguments, one of them is executing a command when a file is found. This argument can be used to call rm, thus, enabling you to remove what you find. Another argument Find allows can specify a time in which should be searched. This way you can delete files older than 10 days, or older than 30 minutes. A combination of these arguments can be used to do what we want.

First off, we need to find files older than, for example, 10 days.
find /var/log -mtime +10

You can also find files older than, say, 30 minutes:
find /tmp -mmin +30

Another argument Find accepts is executing commands when it finds something. You can remove files older than x days like this:
find /path/* -mtime +5 -exec rm {} \;


The " {} " represents the file found by Find, so you can feed it to rm. The " \; " ends the command that needs to be executed, which you need to unless you want errors like:
find: missing argument to `-exec`

Like i said, {} represents the file. You can do anything with a syntax like this. You can also move files around with Find:
find ~/projects/* -mtime +14 -exec mv {} ~/old_projects/ \;


Which effectively moves the files in ~/projects to ~/old_projects when their older than 14 days.



antonio on Tuesday 20-03-2012

excellent explanation
Mark on Tuesday 24-04-2012

Another tutorial I read says "find /path/to/files* -mtime +5 -exec rm {} \;" will delete files older than 5 days. But according to your tutorial, +5 would mean 5 minutes not 5 days.
Vijay on Tuesday 24-04-2012

Mark,

Observe the difference in the flags: -mtime vs -mmin
arix on Tuesday 08-05-2012

good explanation
strider on Thursday 17-05-2012

Nice sharing. Thanks.
Tri on Thursday 24-05-2012

using CentOS 5, whats the difference between -delete and -exec rm {}? Thanks
Tim Quax on Friday 25-05-2012

@Tri, I guess the result would be the same :)
Vladimir on Saturday 09-06-2012

man find
...

ACTIONS
-delete
Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find’s exit status will be nonzero (when it eventually exits). Use of -delete automatically turns on the -depth option.

Warnings: bla-bla-bla
Tim Quax on Monday 11-06-2012

Thanks Vladimir, must've missed that in the man page. The usage is pretty much straight forward.
ajz on Friday 15-02-2013

hi everybody


i want command To delete the files which exceeds 50 kb and older than one day
rajkumar on Friday 15-02-2013

hi ajz

find /pathto/ -name filename -size +50k -mtime +1 -delete

check this command To delete the files which exceeds 50 kb and older than one day
rajkumar on Friday 15-02-2013


come on give queries? raj is there to solve
sanju on Thursday 21-02-2013

getting this error:-

-bash: /usr/bin/find: Argument list too long
jazz on Friday 30-05-2014

how to append delete files in "output.log"

"find /home/gauravsinghp/dir1/dir2/dir3/* -mmin -120 -exec rm {} ; >> output2.log"

does not work for me
Kingshuk on Monday 14-07-2014

How do i delete logs when the log space hits say 75% of the disk space?
Roel on Monday 17-11-2014

This is i what I wanted to do: find all files by month and zip it in one file. Please help

React on this article







Enter the code here: