How to use find to delete files older than N days (with more examples)
This article explains how to delete files older than N days, and other examples, such as matching only files with a specific extension, and so on. For this we will use find
, A command-line tool for searching files in directories.You can use the following command to delete all files and folders from the directory that are older than N days (the file data was modified earlier than).
find /directory/path/ -mindepth 1 -mtime +N -delete
Explanation of the entire command and what you need to replace:
find
Is a Unix command line tool for finding files (and more)/directory/path/
Is the directory path to find (and delete) files. Replace it with the path of the directory from which you want to delete files and folders older than that directoryN
day-mindepth 1
Used to not apply any tests or actions below the specified level.-mindepth 1
Indicates that the command should process all files except command line parameters. If this command is not used, the command will also attempt to delete/directory/path/
If withfind
standard. If you also want to match the specified search path, you can skip it in all the commands mentioned in this article. Also, if you are only looking for files, you do not need to specify this option (-type f
Such as regular files) instead of folders-mtime +N
Used for matching files whose data (content) has been modified in the last N days. ReplaceN
With numbers (integer). In this command, the modification time is earlier thanN
Days will be deleted. Please note, for example, if the file was last modified 1 day 23 hours,-mtime +1
Will not match it, consider it as the last modification of the file 1 day, 0 hours, 0 minutes, 0 seconds ago; See this explanation About why this is becausefind
Man page Did not do well-delete
Delete matching files and folders
Please pay attention -delete
Are GNU extensions, so not all are available find
achieve. For other ways to delete files that apply to all implementations, read later in this article.
Want to test a command without deleting any files or folders? Remove -delete
The command will list all files found that match your criteria without deleting them:
find /directory/path/ -mindepth 1 -mtime +N
Let’s look at an example. To delete all files and folders older than 10 days from the ~ / Downloads folder, you can use:
find ~/Downloads -mindepth 1 -mtime +10 -delete
To delete all files and folders that are N days later than the file modification time, use -N
Instead +N
:
find /directory/path/ -mindepth 1 -mtime -N -delete
We will delete all files and folders in ~ / Downloads with examples of content changed between now and 10 days:
find ~/Downloads -mindepth 1 -mtime -10 -delete
You may also like: 179 Gtk-based Linux terminal color schemes (Gnome terminal, Tilix, Xfce terminal, etc.)
I recommend reading some important observations:
- Parameter order is important! You should add
-delete
After matching files. If it is the first parameter, every file (and folder) will be deleted from it/directory/path/
Regardless of whether they match your query. So always add-delete
At the end of the command. - Not all versions
find
stand by-delete
This is a GNU extension. just in casefind
The version you are using is not supported-delete
, you can use it-exec rm -rf {} +
, Despite havingfind
According to what I read, this version is also not supported. Use-exec rm -rf {} +
Delete files older than N days, and delete only matching files (leave empty subdirectories): - Whatever lookup version should use is
exec rm {} ;
. However, this method performs worse than the other two solutions already mentioned because it spawns an external process for each file deleted. That’s why I finally gave up on this solution. Use this command to delete all files and folders older than X days from the directory:
find /directory/path/ -mindepth 1 -type f -mtime +N -exec rm -rf {} +
This executes a rm
List of commands and their matching files. In addition, this ensures that filenames with spaces are passed to the executable. rm
Command without being split by the shell. It’s worth noting that despite rm
Have -rf
Parameter here, it will only delete the file because we specified -type -f
(Matches files only).
find /directory/path/ -mindepth 1 -mtime +N -exec rm -rf {} ;
Other examples of using Find to delete files or folders based on their modification time
Delete only regular files older than N days and leave empty subdirectories:
find /directory/path/ -mindepth 1 -type f -mtime +N -delete
Here we have used -type f
Matches regular files only. You can also use -type d
Matching folders, or -type l
Matches symbolic links. Remove only files matching .extension older than N days from the directory and all its subdirectories:
find /directory/path/ -type f -mtime +N -name '*.extension' -delete
You can add -maxdepth 1
To prevent the command from going through subdirectories, and only delete files and first-level deep directories:
find /directory/path/ -mindepth 1 -maxdepth 1 -mtime +N -delete
You can also use -ctime +N
For matching (and removing in this example) files whose status has recently changed to N days (file attributes / metadata AND / OR file contents have been modified), not -mtime
To match files based only on when they were last modified:
find /directory/path/ -mindepth 1 -ctime +N -delete
You might like: Bash history: how to show timestamps as each command is executed