[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 with the find command.
There are many more, but I will mainly list the basic ones and the commands necessary for this article.
option | explanation |
-name "[file name]" |
Search by 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]" |
matches by specifying a substring of a path name or a specific pattern (case insensitive) |
-type [file type] |
Search by specifying file type - type can be specified by adding f (regular file) or d (directory) |
-mtime "[number of days]" |
Find files modified within the specified number of days |
-atime "[number of days]" |
Find files accessed within the specified number of days |
-newermt "[date and time]" |
Find files modified after the specified date and time |
How to specify deletion in an action
The action here refers to
the method of processing This time, I would like to focus on the method of deletion processing, so I would like to introduce how to perform deletion processing using rm
Even if you want to delete it simply, there are multiple ways to delete it.
This time, we will take as an example " | I made a table and compared them.
action | Processing method | Command description | How to handle arguments |
| xargs -p rm | Multiple files are passed to rm at once, so rm is executed only once → Processing is quick even when deleting a large number of files. |
can be used with various commands by connecting it with one command |
White space characters (spaces, new lines, etc.) are recognized as argument delimiters → If there are spaces in the path, it may not be recognized correctly. |
-exec rm {} + | Multiple files are passed to rm at once, so 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 → cannot be used with other commands |
Arguments are fixed → arguments are recognized as one group even if there are spaces in the path |
-exec rm {} \; | Since multiple files are passed to rm one by one, rm is executed for the number of files → processing is heavy and takes time when deleting a large number of files. |
"-exec" is one of the options of the find command → cannot be used with other commands |
Arguments are fixed → arguments are recognized as one group even if there are spaces in the path |
I've summarized it briefly, but looking at this table, it seems that " -exec rm -i {} \; " takes longer to process than -exec rm {} + Don't you wonder?
It is true that it is not suitable for use when you want to perform a process such as deleting a huge amount of files, but it is not suitable for use when you want to perform a process such as deleting a . ``exec ~ {} \; '' is useful.
For example, suppose you have created a program called
FileDeleter.exe This program can perform deletion operations similar to rm, but it accepts only one argument .
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, so " FileDeleter.exe ", which can only accept one argument, will not be executed correctly.
Conversely, if you use -exec FileDeleter.exe {} \; FileDeleter.exe '', so it can be executed correctly.
of
arguments that can be accepted by the rm command used this time varies as long as the number of arguments does not exceed the maximum value. There are some things that are created based on the premise. In such cases, there may be cases where "-exec ~ {} +" is ineffective, but "-exec ~ {} \;" can be executed reliably.
Therefore, you need to choose which action to use depending on what kind of processing you want to do with the target argument
I've talked about "-exec" for a long time, but
xargs , which can delete multiple files efficiently and can be used with other commands, to delete files. (I want to use easy-to-use commands✊)
Let's delete a file using the find command!
before deleting
This time, we will use the following command format to execute the deletion.
find [path to search] -type [type] [options] '[number of days or date and time]' | xargs -p rm
" xargs " receives arguments (files and directories to be deleted here) and passes them to the command (rm here) all at once.
" -p " is an option that displays a confirmation prompt. Do you want to delete it? It will not disappear unless you answer ``y'' to the confirmation question, so it is best to leave it on.
As a side note, if you want to delete all directories under the specified path, you need to add
-r to rm This time, we are only deleting the file and it is not necessary, so we will leave it unattached.
Last but not least, as an insurance policy, try running ``ls'' instead of rm before deleting.
If you enter the command correctly, it will simply search for the file and display it, which prevents accidental deletion.
Delete files older than specified date and time
I'll actually try it.
1. Create a file
I created a file like below.
This time, I would like 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 files to be deleted
Specify 2024/12/5 with
the option " ! -newermt ls instead of rm check that
the files updated from 2024/12/1 to 12/4 (file_1 to file_4) Do you want ls? When you are asked, type "y" and 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 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--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 files 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--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
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