Thursday, January 6, 2011

Unix command to list/delete files older than n days

During software development, we often end up in searching large number of log files. Sometimes there will be hundreds of log files.

The unix command to list/remove files that are older than n days is given below.

Command To  List :
find ./ –mtime +5 –exec ls –l '{}' \;

Command To  Delete :
find ./ –mtime +5 –exec rm –f '{}' \;


Section of command Explanation
find unix find command
./ current directory (can specify any directory)
-exec to execute other commands within find
-mtime +5 To find the files that are not modified atleast 5 days ago.
ls –l or rm -f To list or remove
‘{}’ for each iteration this will be replaced by the next filename
\; indicated the end of inner ls or rm command



Hope this help…
Cool…