Often you may want to use Bash to delete all files that are older than a certain number of days in a particular directory.
You can use the following syntax to do so:
find . -mtime +3 -delete
This particular syntax will delete all files in the current directory that are older than (i.e. have been modified longer than) 3 days ago.
Note that we use the command -mtime +n to match all files that have a modification time older than 3 days from the current time.
It’s worth noting that the n command uses the following logic:
- -mtime +n: Match files modified more than n days ago
- -mtime n: Match files modified exactly n days ago
- -mtime -n: Match files modified less than n days ago
Note that you can also replace find . with find /some/directory/path to match all files in a specific directory.
The following example shows how to use this syntax in practice.
Example: How to Delete Files Older than X Days in Bash
This article is currently being written on 7/31/2024.
Suppose we use the ls -l command to first view all files in our current directory along with their most recently modified time:

Now suppose that we would like to delete all files that have been modified more than 3 days ago.
We can use the following syntax to do so:
find . -mtime +3 -delete
The following screenshot shows how to use this syntax in practice:

Notice that when we run the ls -l command again to view the updated files in the directory we can see that all files that were older than 3 days ago have been deleted.
Feel free to change the 3 to a different number to instead delete files that have been modified longer than a different number of days ago.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to List All Files in Folder by Size
Bash: How to List All Files in Folder Without Extension
Bash: How to List All Files Matching a Specific Pattern
