[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 24-year-old graduate and a first-year infrastructure engineer in the System Solutions Department.
Before I know it, the new year is over, and it's been almost a year since I joined Beyond, and I'm scared of how fast time is passing⛄
This time, I was asked to delete files on a customer's server during actual work, but there were so many files that it would be inefficient to delete them one by one! I would like to introduce how to delete files using "find" that I learned when I encountered this problem
It's convenient if you know how to use it, but if you make one mistake, something completely different can disappear, which could be scary, so I'd like to practice how to use it so I don't get scared!
There are also many 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 when you
want to search for files or directories It is possible to specify multiple files and directories as search targets.
The basic format of the find command is as follows.
find [path to search] [options] [action]
When you actually apply it, it looks like this.
Find/var/log/test_log -Name "file_1" -print
The above content specifies the file name (-name "file_1") under /var/log/test_log and displays a list (-print).
You can modify it to make it more convenient, so you may see some commands that look complex and difficult, but the basic form is relatively simple. We'll explore options and actions a bit more in the following sections.
Option example
We will also introduce the options used in the Find command.
There are still many, but here are the basic and necessary commands in this article.
option | explanation |
-name "[file name]" |
Search by specifying the file name |
-iname "[file name]" |
specifying file name (case insensitive) |
-Path "[File name]" |
Search for matches by specifying pathname substrings or specific patterns |
-Ipath "[File name]" |
partial strings and specific patterns in the path name (do not distinguish the size of characters) |
-Type [File type] |
Search by specifying file type - type can be specified by adding f (regular file) or d (directory) |
-mtime "[Number of days]" |
Search for updated files before the specified number of days |
-Atime "[Number of days]" |
Search for files accessed before the specified number of days |
-newermt "[Date]" |
Search for updated files after the 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 how to delete processing, so I would like to introduce the method of deletion processing using RM
There are multiple ways to delete it, even if you simply delete it.
the example is | XARGS RM -P ", " -Exec RM {{ + ", " -exec RM {} \; , which is used to pass and delete multiple files specified by the Find command to the RM command. , I compared it as a table.
action | Method of processing | Command explanation | How to handle arguments |
| XARGS -P RM | Since multiple files are passed together to the RM, the RM is executed only once → the processing is quick when deleting a large amount of files. |
can be used in combination with various commands by connecting with one command → "|" instead of options. |
White blank characters (space, line breaks, etc.) are recognized as arguments → It may not be correctly recognized if there is space in the path. |
-EXEC RM {} + + | Since multiple files are passed together to the RM, the RM is executed only once → the processing is quick when deleting a large amount of files. |
"-Exec" is one of the options of the Find command → cannot be used with other commands |
The argument is fixed → Even if there is space in the path, it recognizes the argument as a pound |
-EXEC RM {} \; | Since multiple files are passed to RM one by one, RM is executed by the number of files → It takes a long time to delete a large amount of files. |
"-Exec" is one of the options of the Find command → Cannot be used with other commands |
The argument is fixed → Even if there is space in the path, it recognizes the argument as a pound |
I looked at this table roughly, but when I look at this table, " -exec RM -I {} \; " takes time to process than -exec RM {} + Don't you wonder?
Certainly, it is not suitable for processing such as deleting a huge amount of files, but the commands and programs that were created in advance that the argument was fixed in advance was " -". EXEC ~ {} \ ;
For example, suppose you have created a program called
FileDeleter.exe This program can be deleted in the same way as RM, but only one is received .
Suppose you use this program and Find command File_1 , File_2 , File_3
if you use " -exec filedeleter.exe {{} + you can pass File_1 , File_2 , and File_3 you cannot execute
FileDeleter.exe Conversely, if you use -exec filedeleter.exe {} \; you can execute multiple files to FileDeleter.exe
The RM command used this time fluctuates the number of arguments that can be received unless the number of arguments exceeds the maximum value, but
some of the arguments are fixed, such as the "FileDeleter.exe" listed in the example. Some are created on the premise.
In such a case, "-EXEC ~ {} +" may not have teeth, but in "-exec ~ {} \;", there may be things that can be performed reliably.
Therefore, it is necessary to select which action to use depending on what kind of processing you want to do for the target arguments
I talked about "-exec" for a long time, but
I would like to delete multiple files efficiently and use XARGS that can be used together with other commands (I want to use a easy -to -use command ✊ ✊)
Let's delete a file using the find command!
before deleting
This time, use the following command format to execute the deletion.
Find [Pass to search] -Type [Type] [Options] '[Number of days or date and time] | XARGS -P RM
" Xargs " receives the argument (the file or directory to be deleted here) and passes it to the command (RM here) at once.
" -P " is an option that displays the confirmation prompt. Do you want to delete it? It will not disappear unless you answer "Y" to the confirmation of, so it is better to put it on.
As a supplement, if you want to erase the whole directory under the specified path, you need to add
-r to RM This time, I do not need to delete only the file, so I will not attach it.
Finally, the most important thing is to specify "LS" instead of RM before deleting insurance insurance.
If you can enter the command correctly, you can only find and display the file, so you can prevent incorrect deletion.
Delete files older than specified date and time
I will actually try it.
1. Create a file
The following files were created.
This time, erase all the files that were updated before 2024/12/5 .
[root@localhost test_log]# ll/var/log/test_log TOTAL 0 -RW-r ----. 1 Root Root 0 Dec 1 2024 File_1 -RW-R--. 1 Root Root 0 Dec 2 2024 File_2 -RW-R----. 1 Root root 0 Dec 3 2024 File_3 -RW-R----. 1 Root Root 0 Dec 4 2024 File_4 -rw-R--. 1 Root ROOT 0 Dec 5 2024 File_5 -RW-R--. 1 Root Root 0 Dec 6 2024 File_6 -RW-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. Confirmation of file to be deleted
Specify 2024/12/5 with
the option " ! -Newermt LS instead of RM confirm that
(File_1 ~ File_4) on 2024/12/1 to 12/4 Do you want to LS? If you driven "Y", it will display the file to be deleted.
[root@localhost ~]# find/var/log/test_log -Type 'dec 5 2024' | 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 file you want to delete, it is time to delete it.
ls rm and run the command and rm? When asked, 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 disappeared safely.
You can confirm that files updated before 2024/12/5 have disappeared.
[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 the specified date and time
Next, I would like to delete all files updated after 2024/12/5
1. Create a file
I created the file as before.
[root@localhost test_log]# ll/var/log/test_log TOTAL 0 -RW-r ----. 1 Root Root 0 Dec 1 2024 File_1 -RW-R--. 1 Root Root 0 Dec 2 2024 File_2 -RW-R----. 1 Root root 0 Dec 3 2024 File_3 -RW-R----. 1 Root Root 0 Dec 4 2024 File_4 -rw-R--. 1 Root ROOT 0 Dec 5 2024 File_5 -RW-R--. 1 Root Root 0 Dec 6 2024 File_6 -RW-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. Confirmation of file to be deleted
This time, specify
-newermt A confusing point about date and time specifications is that `` ! -newermt '' searches without including the specified date and time , whereas `` -newermt'' searches including
the specified date and time Therefore, if you want to delete files updated after 2024/12/5, you must specify 2024/12/6 It's confusing.
Then, as before, ls and execute the command.
Do you want ls? When asked, type "y" and the files to be deleted will be displayed.
You can confirm that files updated from 2024/12/6 to 12/9 (file_6 to file_9) can be searched
[root@localhost ~]# find /var/log/test_log -type f -newermt 'Dec 6 2024' | xargs -p ls 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 I'll delete it.
ls rm and run the command and rm? When asked, 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 confirm 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 a little more detail.
If I were to write out the steps one by one, it would be too long, so I'll stop.
Finally, I would like to introduce a command that deletes only files at a specified date and time.
As before, suppose you have a file like the one below, but you want to delete only 12/3~12/7.
[root@localhost test_log]# ll/var/log/test_log TOTAL 0 -RW-r ----. 1 Root Root 0 Dec 1 2024 File_1 -RW-R--. 1 Root Root 0 Dec 2 2024 File_2 -RW-R----. 1 Root root 0 Dec 3 2024 File_3 -RW-R----. 1 Root Root 0 Dec 4 2024 File_4 -rw-R--. 1 Root ROOT 0 Dec 5 2024 File_5 -RW-R--. 1 Root Root 0 Dec 6 2024 File_6 -RW-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
As before, you can use the "-newermt" option to specify things like "-newermt "Dec 3 2024" ! -newermt "Dec 8 2024"". This seems to be useful as it allows for a slightly more flexible specification method.
for your information!
[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, we focused on deleting files using the find command, but it is a useful command that can be used for a variety of other purposes, so we will continue to study so that we can master it🔥
Thank you for watching until the end!
Reference site:
What is the find command? Introducing how to search files and directories with Linux commands.
7 rules + practical knowledge to easily understand how to use the find command.
Difference between find -exec and find | xargs