delete files in linux

How to Delete Files Older Than X Days in Linux

Sometimes you need to delete files that are older than specific number of days. For example, you may need to remove files that are older than 1 day, 7 days, 30 days, 1 month, 6 months, or even 1 year. It can be tedious to manually search and delete files individually. In this article, we will look at how to delete files older than X days. This should work on almost every Linux distribution.


How to Delete Files Older Than X Days

The find command in Linux allows you to pass many options, including one that allows you to execute a command on all found files. We will use this argument to find files that are older than specific time period and pass rm command to delete them.

Here are some common examples to find and delete files that are older than X days.


Delete Files older than 1 day

Here is the command to delete files that are older than 1 day.

find /path/to/files* -mtime +1 -exec rm {} \;

Let us look at the above command in detail.

The first argument is the path to files which can be files, directories or even wildcard characters. For example, if you want to find and delete all files in /home/ubuntu that are older than 1 day, use the following command.

find /home/ubuntu* -mtime +1 -exec rm {} \;

Next, -mtime is used to specify number of days old your files are. For example, if you specify +1 find command will search for files older than 1 day.

The 3rd argument is -exec that allows you to pass the command to be executed. We use rm command since we want to delete files. The {} \; is used to end the command.

Now we will look at some common use cases for this command.


Delete Files older than 7 days

Here is the command to delete files older than 7 days. Replace /path/to/files with the path to your files or directory.

find /path/to/files* -mtime +7 -exec rm {} \;


Delete Files older than 30 days or 1 month

Here is the command to delete files older than 30 days. Replace /path/to/files with the path to your files or directory.

find /path/to/files* -mtime +30 -exec rm {} \;


Delete Files older than 6 months

Here is the command to delete files older than 6 month (182 days). Replace /path/to/files with the path to your files or directory.

find /path/to/files* -mtime +182 -exec rm {} \;


Delete Files older than 1 year

Here is the command to delete files older than 1 year (365 days). Replace /path/to/files with the path to your files or directory.

find /path/to/files* -mtime +365 -exec rm {} \;

If you get ‘permission denied’ message while running any of the commands, use sudo keyword before the commands, or switch to a user that has the permission to remove files, and then try the above commands.

In this article, we have learnt how to find and remove files & folders older than specific time period. You may also use these commands in shell script to automate your tasks, or even create cron job for it.

Also read:

How to Check Inode Usage in Linux
How to Find Inode Number of File in Linux
How to Convert HTML to PDF in PHP
How to Create Ext4 Filesystem in Linux
How to Increase Inode Limit in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *