Home Linux Commands How to Delete Files Older Than Specified Days in Linux

How to Delete Files Older Than Specified Days in Linux

As you might already know, we use the rm command in Linux to delete files and folders. The filenames to be deleted have to be passed as arguments to rm. However, rm does not offer other options by itself, like deleting files based on timestamps.

That’s the reason, we use the find command in Linux, which is used to search for files and folders based on different parameters. It is a complex command which can be used to search with parameters like the filename, size, type of file, etc.

There is an option in the find command to search for files based on how old they are and today we will see how to use find and rm together to delete files older than the specified number of days.

Find Files Older Than N Days

We use the argument '-atime' of find command to find files older than N days, i.e. last accessed before at least N days.

$ find <directory_path> -atime +<N>
$ find . -atime +3
Find Files Older Than Specified Days
Find Files Older Than Specified Days

There are two files in the current which were last accessed at least 4 days ago. Let us use the ‘stat‘ command to verify this:

$ stat tmp2 tmp3
Check File Access Time
Check File Access Time

As you can see, the files are 4 and 5 days old respectively. Note that the program ignores any fraction part of the timestamp when calculating how old a file is; hence, for example, when files older than 4 days or more are to be specified, we specify +3.

Delete Files Older Than N Days

Finally, let’s call the rm command with the argument '-exec' to delete these files.

$ find . -atime +3 -exec rm {} \;
Delete Files Older Than Specified Days
Delete Files Older Than Specified Days

Thus the two files ‘tmp2’ and ‘tmp3’ were deleted.

Conclusion

In this article, we have seen how to delete files older than a specified number of days in Linux. As you might know, in Linux (and Unix) file systems, the creation time of a file is not maintained. The three timestamps maintained for a file are; its last access time, last modification time, and its last status change time.

Here we have considered the last access time for searching the files. Users can choose to go for the modification or status change time too: simply use the options '-mtime' or '-ctime' respectively, instead of '-atime', and the rest of the syntax remains the same.

Thank you for reading! Let us know your thoughts in the comments below!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.