How To Delete Files Older Or Newer Than N Days Using find (With Extra Examples)
This article explains how to delete files older or newer than N days, with extra examples like matching only files using a particular extension, and more. For this we'll use
You can delete all files and folders older than (with the file's data modification time older than) N days from a directory by using:
An explanation of the whole command and what you need to replace:
It's important to note that
Want to test the command without removing any files or folders? Remove
Let's take a look at an example. To delete all files and folders older than 10 days from the ~/Downloads folder you can use:
To delete all files and folders newer than (with a file modification time newer than) N days, use
Example in which we'll remove all files and folders from ~/Downloads that had their contents modified between now and 10 days ago:
You might also like: 179 Color Schemes For Your Gtk-Based Linux Terminal (Gnome Terminal, Tilix, Xfce Terminal, More)
Only delete regular files older than N days, keeping empty subdirectories:
Here we've used
Only delete files matching .extension older than N days from a directory and all its subdirectories:
You can add
You may also use
You might like: Bash History: How To Show A Timestamp When Each Command Was Executed
find
, a command line tool to search for files in a directory.You can delete all files and folders older than (with the file's data modification time older than) N days from a directory by using:
find /directory/path/ -mindepth 1 -mtime +N -delete
An explanation of the whole command and what you need to replace:
find
is the Unix command line tool for finding files (and more)/directory/path/
is the directory path where to find (and delete) files. Replace it with the path of the directory from which you want to delete files and folders older thanN
days-mindepth 1
is used to not apply any tests or actions at levels less than the specified level.-mindepth 1
means the command should process all files except the command line arguments. Without using this, the command would attempt to also remove/directory/path/
if it matches thefind
criteria. You can skip it in all the commands mentioned in this article if you want to also match the specified find path. Also, there's no need to specify this if you only look for files (-type f
for regular files for example), and not folders-mtime +N
is used match files that had their data (content) last modified N days ago. ReplaceN
with a number (integer). In this command, files with a modification time older thanN
days will be deleted. It's important to note that for example, if a file was last modified 1 day and 23 hours ago,-mtime +1
won't match it, treating it as if the file was last modified 1 day, 0 hours, 0 minutes, and 0 seconds ago; see this explanation on why that's the case, since thefind
man page doesn't do this very well-delete
deletes matched files and folders
It's important to note that
-delete
is a GNU extension, so it's not available in all find
implementations. For other ways of deleting files that works with all implementations, read the observations further down this article.Want to test the command without removing any files or folders? Remove
-delete
and the command will list all files it finds matching your criteria, without deleting them:find /directory/path/ -mindepth 1 -mtime +N
Let's take a 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 newer than (with a file modification time newer than) N days, use
-N
instead of +N
:find /directory/path/ -mindepth 1 -mtime -N -delete
Example in which we'll remove all files and folders from ~/Downloads that had their contents modified between now and 10 days ago:
find ~/Downloads -mindepth 1 -mtime -10 -delete
You might also like: 179 Color Schemes For Your Gtk-Based Linux Terminal (Gnome Terminal, Tilix, Xfce Terminal, More)
A few important observations that I recommend reading:
- The arguments order matters! You should add
-delete
after matching the files. If it's the first argument, every file (and folder) will be deleted from/directory/path/
, regardless of them matching your query or not. So always add-delete
at the end of the command. - Not all versions of
find
support-delete
, which is a GNU extension. In case thefind
version you're using doesn't support-delete
, you can use-exec rm -rf {} +
, though there arefind
versions that don't support this either from what I've read. Using-exec rm -rf {} +
to remove files older than N days, and only matching files (keeping empty subdirectories): - What should work with any find version is
exec rm {} \;
. However, this performs worse than the other two solutions already mentioned, because it spawns an external process for each file it removes. This is why I left this solution for last. Use this to remove all files and folders older than X days from a directory:
find /directory/path/ -mindepth 1 -type f -mtime +N -exec rm -rf {} +
This executes a single rm
command with the list of files it matches. Also, this ensures that filenames with white spaces are passed to the executed rm
command without being split up by the shell. It's also worth noting that even though rm
has the -rf
arguments here, it will only remove files since we specified -type -f
(match only files).
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
Only delete regular files older than N days, keeping empty subdirectories:
find /directory/path/ -mindepth 1 -type f -mtime +N -delete
Here we've used
-type f
to match only regular files. You can also use -type d
to match folders, or -type l
to match symbolic links.Only delete files matching .extension older than N days from a 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 1st level depth only directories:find /directory/path/ -mindepth 1 -maxdepth 1 -mtime +N -delete
You may also use
-ctime +N
, used to match (and delete in this example) files that had their status last changed N days ago (the file attributes/metadata AND/OR file content was modified), as opposed to -mtime
, which only matches files based on when their content was last modified:find /directory/path/ -mindepth 1 -ctime +N -delete
You might like: Bash History: How To Show A Timestamp When Each Command Was Executed