How to Delete all files older than X number of Days in Linux?

Updated on September 3, 2017

Question: I would like to know if there is a single command in Linux that allows me to delete all files that are older than say 10 days? – Ravi

Solution: Yes, you can use ‘find‘ command with few arguments – such as to search all files or files with specific extensions (e.g., *.tar), calculate file modification time and a command to execute on each file.

For example, the below command will delete all files that are older than 10 days.

find * -mtime +10 -exec rm {} \;

Linux command help

Here,

find – the command utility to search files.

* – specifies all files. If you want to search for files with specific extension then the command goes like this…

find *.tar -mtime +10 -exec rm {} \;

-mtime –  File’s  data was last modified n*24 hours ago.

+10 – means, files older than 10 days.

-exec – runs external command

rm – command to remove files

{} \; – terminates the command

Caution: Note the directory where the command is executed, as the files deleted cannot be recovered, until unless you use recovery software.

Was this article helpful?

Related Articles

Leave a Comment