[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 System Solutions Department, who graduated in 2024.
Before I knew it, the new year had arrived, and it's been almost a year since I joined Beyond. I'm intimidated by how quickly time flies.
This time, I was asked to delete files from a client's server, but there were so many that deleting them one by one was too inefficient! I'll introduce a method I learned to delete files using "find."
It's convenient if you can get the hang of it, but if you make a mistake, something terrible could happen, like completely erasing a whole different part , so I'd like to practice using it so I don't have 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 “~”, “$”, and “#” mean on the command line? [For beginners]
What is the find command?
Basic format
First of all, the find command is a Linux command used to
search for files and directories You can specify multiple files and directories to search for.
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 will also introduce the options used with the find command.
There are many more options, but we will mainly list the basics and the commands required in this article.
| option | explanation |
-name "[file name]" |
Search by specifying file name |
-iname "[file name]" |
by file name (case insensitive) |
-path "[file name]" |
Search for matches by specifying a substring or specific pattern in a pathname |
-ipath "[filename]" |
matches to pathname substrings or specific patterns (case insensitive) |
-type [file type] |
Search by file type - You can specify type by adding f (normal file) or d (directory) after the type. |
-mtime "[number of days]" |
Search for files updated within a specified number of days |
-atime "[number of days]" |
Search for files accessed within a specified number of days ago |
-newermt "[date and time]" |
Search for files updated after a specified date and time |
How to specify deletion in an action
The action here refers to
the processing method This time, I would like to focus on the deletion processing method, so I would like to introduce the deletion processing method using rm
Simply put, there are multiple ways to delete files.
This time, we've created a table comparing the methods " | xargs rm -p ", " -exec rm {} + ", and " -exec rm {} \; ", which are used to pass multiple files specified with the find command to the rm command to delete them.
| 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 quick even when deleting a large number of files |
"xargs" is not an option, but a single command → You can use it with various 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 quick even when deleting a large number of files |
"-exec" is one of the options of the find command → It cannot be used in conjunction with other commands |
Arguments are fixed → Arguments are recognized as one block even if there are spaces in the path. |
| -exec rm {} \; | Since multiple files are passed to rm one by one, rm is executed for each file . This makes the process slow and heavy when deleting a large number of files. |
"-exec" is one of the options of the find command → It cannot be used with other commands |
Arguments are fixed → Arguments are recognized as one block even if there are spaces in the path. |
This is a rough summary, but when you look at this table, you might be wondering, " -exec rm -i {} \; takes longer to process than " -exec rm {} +
It's true that it's not suitable for performing operations such as deleting a huge number of files, but -exec ~ {} \; " is useful for commands and programs that are written with the assumption that the number of arguments is fixed
For example, let's say you've created a program called
FileDeleter.exe This program can perform deletion operations similar to rm, but only accept one argument .
you want to use this program and the find command to delete the files file_1 , file_2 , and file_3
if you use " -exec FileDeleter.exe {} + file_1 , file_2 , and file_3 will be passed all at once, and " FileDeleter.exe ", which can only accept one argument, will not run correctly.
Conversely, if you use " -exec FileDeleter.exe {} \; ", multiple files will be passed one by one to " FileDeleter.exe ", which will run correctly.
The rm command we'll use here can accept a variable number of arguments as long as the number doesn't exceed the maximum, but
some commands, like the example "FileDeleter.exe," are created with a fixed number of arguments in mind.
In such cases, you might encounter situations where "-exec ~ {} +" won't work, but "-exec ~ {} \;" will work reliably.
Therefore, you need to choose which action to use based on what you want to do with the target arguments
I've talked at length about "-exec", but
xargs , which can delete multiple files efficiently and can be used in conjunction with other commands . (I want to master easy-to-use 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
" xargs " receives arguments (the files and directories to be deleted in this case) and passes them all at once to the command (rm in this case).
" -p " is an option that displays a confirmation prompt. If you don't answer "y" to the question "Are you sure you want to delete?", the command will not disappear, so it's best to leave it on.
As a side note, if you want to delete the entire directory under the specified path, you need to add
-r to rm In this case, we are only deleting files, so we won't need to add it.
Finally, and most importantly, as a precaution, try running "ls" instead of "rm" before deleting.
If you entered the command correctly, it will simply find and display the file in question, preventing accidental deletion.
Delete files older than the specified date and time
Let's actually try it
1. Create a file
I created the following files.
This time, want to delete all files updated before 2024/12/5 .
[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.
ls instead of rm to confirm that the files updated between 2024/12/1 and 12/4 (file_1 to file_4) can be found.
You will be asked "Do you want to run ls?", so if you type "y", the files to be deleted will be displayed.
[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 have confirmed the files you want to delete, it's time to delete them.
ls rm and run the command. You will be asked if you want to rm, so type y
[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 the files updated before 2024/12/5 have been deleted.
[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, want to delete all files that were updated after 2024/12/5 .
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, we will specify
-newermt One tricky thing about specifying the date and time is that " ! -newermt " searches excluding the specified date and time , whereas " -newermt " searches
including the specified date and time So, if you want to delete files updated after 2024/12/5, you must specify 2024/12/6
, run the command specifying
ls instead of rm You will be asked, "Do you want to run ls?" If you type "y," the files to be deleted will be displayed.
You can confirm that the files updated between 2024/12/6 and 2024/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 run the command. You will be asked if you want to rm it, so type y
[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 would like to introduce a command to delete only files between specified dates.
As before, let's say you have the following files, and you want to delete only the files from 12/3 to 12/7.
[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 use the "-newermt" option to specify something like "-newermt "Dec 3 2024" ! -newermt "Dec 8 2024"". This gives you a little more freedom in how you specify things, so it seems like it might be useful.
Just 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 watching until the end!
Reference site:
What is the find command? Introducing how to search for files and directories with Linux commands
7 rules + practical knowledge to easily understand how to use the find command
The difference between find -exec and find | xargs
8