[Linux] I want to delete all files on the server at once using the find command!

table of contents
Introduction
Hello. I'm Paru, a first-year infrastructure engineer in the Systems Solutions Department, a 2024 graduate.
Before I knew it, the new year had begun, and it's almost been a year since I joined Beyond. I'm terrified by how quickly time flies! ⛄
a client's server during actual work, but there were too many files to delete them one by one, which would be too inefficient!share how to delete files using the Linux command "find"onto delete filesasked
It's convenient if you can master it, butone wrong move could potentially erase an entire section, which is terrifying, so I'll practice using it to avoid any scary experiences!
There are also many other articles related to Linux commands for beginners, so please take a look if you are interested
[Linux] What do the "~", "$", and "#" symbols mean on the command line? [For beginners]
What is the find command?
Basic format
First, the `find` command issearch for files and directoriesa Linux command used to
It's possible to specify multiple files and directories as search targets.
The basic format of the find command is as follows:
find [search path] [options] [action]
When actually applied, it looks like this
find /var/log/test_log -name "file_1"-print
The above command specifies the file name (-name "file_1") under /var/log/test_log and displays a list (-print)
Because they can be modified in any way you like, some commands may seem complicated and difficult to use, but the basic format is actually quite simple. We'll go into more detail about options and actions in the next section
Option example
We'll also introduce the options used with the `find` command.
There are many more options, but we'll mainly list the basic ones and the commands necessary for this article.
| option | explanation |
-name "[file name]" |
specifying the file nameSearch by |
-iname "[file name]" |
specifying the file name(case-insensitive). |
-path "[file name]" |
matches by specifying a substring of a path name or a specific pattern.Search for |
-ipath "[filename]" |
matches using a substring of a path name or a specific pattern(case-insensitive). |
-type [file type] |
the file typeYou can search by specifying - you can specify the type by adding 'f' (for regular files) or 'd' (for directories) after 'type'. |
-mtime "[number of days]" |
before the specified number of days.Search for files that were updated |
-atime "[number of days]" |
before a specified number of days ago.Search for files that were accessed |
-newermt "[date and time]" |
since the specified date and time.Search for files that have been updated |
How to specify deletion in an action
In this"action",the method of processingrefers to
This time, we want to focus on deletion methods, sohow to delete files using the `rm` commandI will introduce
Even simply deleting files can involve several methods.
—used when passing multiple files specified by the find command to the rm command for deletion| xargs rm -p", "-exec rm {} +", and "-exec rm {} \;, as examples, and compare them in a table.
| action | Method of processing | Command Description | How to handle arguments |
| | xargs -p rm | Since multiple files are passed to `rm` at once, `rm` is executed only once → Processing is fast even when deleting a large number of files. |
"xargs" is not an option; it's a single command that can be used in conjunction with various other commands by connecting them with "|". |
Whitespace characters (spaces, line breaks, etc.) are recognized as argument separators → If there are spaces in the path, it may not be recognized correctly. |
| -exec rm {} + | Since multiple files are passed to `rm` at once, `rm` is executed only once → Processing is fast even when deleting a large number of files. |
"-exec" is an option for the find command → it cannot be used in conjunction with other commands. |
Arguments are fixed → Even if there are spaces in the path, the arguments are recognized as a single unit. |
| -exec rm {} \; | Since multiple files are passed to `rm` one by one, `rm` is executed for each file → deleting a large number of files becomes slow and resource-intensive. |
"-exec" is an option for the find command → it cannot be used in conjunction with other commands. |
Arguments are fixed → Even if there are spaces in the path, the arguments are recognized as a single unit. |
I've summarized it roughly, but looking at this table,-exec rm -i {} \;" takes longer to process than "-exec rm {} +don't you wonder if
While it's certainly not suitable for tasks like deleting a massive number of files, "commands and programs that are designed with the assumption that the number of arguments is fixedfor-exec ~ {} \;.
For example,FileDeleter.exelet's say we create a program called
This program can perform deletion operations in the same way as rm, but itonly accept one argument.
, using this program and the find commandfile_1, file_2, and file_3 Let's say we want to delete three files
"-exec FileDeleter.exe {} +using file_1, file_2, and file_3 will passFileDeleter.exe", which can only accept one argument, will not execute correctly.
Conversely, using "-exec FileDeleter.exe {} \;one by oneFileDeleter.exe, so it will execute correctly.
The `rm` command we'll be using here accepts a variable number of arguments, as long as the number of arguments doesn't exceed the maximum value.
However, some programs, like the example "FileDeleter.exe," are designed with the assumption that the number of arguments is fixed.
In such cases, you might encounter situations where `-exec ~ {} +` won't work, but `-exec ~ {} \;` will definitely execute.
Therefore,which action to use based on what kind of processing you want to do to the target argumentsyou need to choose
I've gone on at length about "-exec," but
which can efficiently delete multiple files and can be used in conjunction with other commands,xargs to perform file deletion. (I want to master the use of user-friendly commands! ✊)
Try deleting the file using the find command
Before you delete
This time, we will use the following command format to perform the deletion
find [search target path] -type [type] [options] '[number of days or date and time]' | xargs -p rm
The ` xargs`command takes arguments (in this case, the files or directories to be deleted) and passes them all at once to the command (in this case, `rm`).
The `-p`option displays a confirmation prompt. You must answer `y` to the confirmation "Do you want to delete?", otherwise the files will not be deleted, so it's best to always include this option.
As a side note, if you want to delete all directories under the specified path,rmoption to-ryou need to add
However, since we are only deleting files in this case, we won't add it.
Lastly and most importantly, as a precaution, try running "ls" instead of "rm" before deleting.
If you've entered the command correctly, it will simply find and display the relevant file,preventing accidental deletion.
Delete files older than the specified date and time
Let's actually try it
1. Create a file
I have created the following files.
This time, like to delete all files that were updated before December 5, 2024.
[root@localhost test_log]# ll /var/log/test_log total 0 -rw-r--r--. 1 root root 0 Dec 1 2024 file_1 -rw-r--r--. 1 root root 0 Dec 2 2024 file_2 -rw-r--r--. 1 root root 0 Dec 3 2024 file_3 -rw-r--r--. 1 root root 0 Dec 4 2024 file_4 -rw-r--r--. 1 root root 0 Dec 5 2024 file_5 -rw-r--r--. 1 root root 0 Dec 6 2024 file_6 -rw-r--r--. 1 root root 0 Dec 7 2024 file_7 -rw-r--r--. 1 root root 0 Dec 8 2024 file_8 -rw-r--r--. 1 root root 0 Dec 9 2024 file_9
2. Check the files to be deleted
the option "! -newermt" and specify 2024/12/5.
instead of rm ls files updated between 2024/12/1 and 12/4(file_1 to file_4)are found.
When asked "Do you want to run ls?", type "y" and it will display the files to be deleted.
[root@localhost ~]# find /var/log/test_log -type f ! -newermt 'Dec 5 2024' | xargs -p ls ls /var/log/test_log/file_2 /var/log/test_log/file_3 /var/log/test_log/file_4 /var/log/test_log/file_1?...y /var/log/test_log/file_1 /var/log/test_log/file_2 /var/log/test_log/file_3 /var/log/test_log/file_4
3. Delete files
Once you've confirmed the files you want to delete, it's time to delete them.
`ls` `rm` and execute the command. You'll be asked "Do you want to rm?", soytype
[root@localhost ~]# find /var/log/test_log -type f ! -newermt 'Dec 5 2024' | xargs -p rm rm /var/log/test_log/file_1 /var/log/test_log/file_2 /var/log/test_log/file_3 /var/log/test_log/file_4?...y
4. Check the file
It has been successfully deleted.
You can confirm that files updated before December 5, 2024 have been removed.
[root@localhost test_log]# ll /var/log/test_log total 0 -rw-r--r--. 1 root root 0 Dec 5 2024 file_5 -rw-r--r--. 1 root root 0 Dec 6 2024 file_6 -rw-r--r--. 1 root root 0 Dec 7 2024 file_7 -rw-r--r--. 1 root root 0 Dec 8 2024 file_8 -rw-r--r--. 1 root root 0 Dec 9 2024 file_9
Delete files after a specified date and time
Next, like to delete all files that were updated after December 5, 2024.
1. Create a file
I created the file just like before
[root@localhost test_log]# ll /var/log/test_log total 0 -rw-r--r--. 1 root root 0 Dec 1 2024 file_1 -rw-r--r--. 1 root root 0 Dec 2 2024 file_2 -rw-r--r--. 1 root root 0 Dec 3 2024 file_3 -rw-r--r--. 1 root root 0 Dec 4 2024 file_4 -rw-r--r--. 1 root root 0 Dec 5 2024 file_5 -rw-r--r--. 1 root root 0 Dec 6 2024 file_6 -rw-r--r--. 1 root root 0 Dec 7 2024 file_7 -rw-r--r--. 1 root root 0 Dec 8 2024 file_8 -rw-r--r--. 1 root root 0 Dec 9 2024 file_9
2. Check the files to be deleted
This time,-newermtwe'll specify
about specifying the date and timeThe tricky partis that "! -newermt"searches excluding the specified date and time, while "-newermt"including the specified date and timesearches
Therefore, if you want to delete files updated after 2024/12/5, 2024/12/6 you must specify
instead of `rm` `ls` and execute the command.
When asked "Do you want to run `ls`?", type "y" and the files to be deleted will be displayed.
files updated between 2024/12/6 and 12/9 (file_6 to file_9) have been found.
[root@localhost ~]# find /var/log/test_log -type f -newermt 'Dec 6 2024' | xargs -p ls /var/log/test_log/file_6 /var/log/test_log/file_7 /var/log/test_log/file_8 /var/log/test_log/file_9?...y /var/log/test_log/file_6 /var/log/test_log/file_7 /var/log/test_log/file_8 /var/log/test_log/file_9
3. Delete files
Now, let's delete it.
`ls` `rm` and execute the command. You will be asked "Do you want to rm?", soytype
[root@localhost ~]# find /var/log/test_log -type f -newermt 'Dec 6 2024' | xargs -p rm rm /var/log/test_log/file_6 /var/log/test_log/file_7 /var/log/test_log/file_8 /var/log/test_log/file_9?...y
4. Check the file
You can see that files created after 2024/12/5 have disappeared
[root@localhost test_log]# ll /var/log/test_log total 0 -rw-r--r--. 1 root root 0 Dec 1 2024 file_1 -rw-r--r--. 1 root root 0 Dec 2 2024 file_2 -rw-r--r--. 1 root root 0 Dec 3 2024 file_3 -rw-r--r--. 1 root root 0 Dec 4 2024 file_4 -rw-r--r--. 1 root root 0 Dec 5 2024 file_5
Bonus: I want to specify it in more detail
If I were to write out each step, it would get too long, so I'll stop here
Finally, I'd like to introduce a command to delete only files from a specified date and time.
Assuming you have a file structure similar to the one below, and you want to delete only the files from December 3rd to December 7th:
[root@localhost test_log]# ll /var/log/test_log total 0 -rw-r--r--. 1 root root 0 Dec 1 2024 file_1 -rw-r--r--. 1 root root 0 Dec 2 2024 file_2 -rw-r--r--. 1 root root 0 Dec 3 2024 file_3 -rw-r--r--. 1 root root 0 Dec 4 2024 file_4 -rw-r--r--. 1 root root 0 Dec 5 2024 file_5 -rw-r--r--. 1 root root 0 Dec 6 2024 file_6 -rw-r--r--. 1 root root 0 Dec 7 2024 file_7 -rw-r--r--. 1 root root 0 Dec 8 2024 file_8 -rw-r--r--. 1 root root 0 Dec 9 2024 file_9
Using the same method as before, you can specify the date using the "-newermt" option, like this: "-newermt "Dec 3 2024" ! -newermt "Dec 8 2024"". This allows for a bit more flexibility in specifying the date, so it might be useful.
For your reference!
[root@localhost test_log]# find /var/log/test_log -type f -newermt "Dec 3 2024" ! -newermt "Dec 8 2024" | xargs -p rm rm /var/log/test_log/file_5 /var/log/test_log/file_3 /var/log/test_log/file_4 /var/log/test_log/file_6 /var/log/test_log/file_7?...y [root@localhost test_log]# ll /var/log/test_log total 0 -rw-r--r--. 1 root root 0 Dec 1 2024 file_1 -rw-r--r--. 1 root root 0 Dec 2 2024 file_2 -rw-r--r--. 1 root root 0 Dec 8 2024 file_8 -rw-r--r--. 1 root root 0 Dec 9 2024 file_9
summary
What did you think?
This time, I focused on deleting files with the find command, but it is a command that can be used conveniently for many different purposes, so I will continue to study it so that I can master it 🔥
Thank you for reading to the end!
Reference sites:
What is the find command? A guide to searching for files and directories using Linux commands.
7 rules for easily understanding how to use the find command + practical knowledge.
The difference between find -exec and find | xargs.
9
