In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the exec of the find command in the linux system. It is very detailed and has a certain reference value. Friends who are interested must read it!
Find is a very common Linux command, but what we usually find is not just a look, but there will be further operations, and then the role of exec becomes apparent.
Exec explained:
The-exec parameter is followed by the command command, which ends with a;, so the semicolon after this command is indispensable. Considering that the semicolon has different meanings in each system, it is preceded by a backslash.
{} curly braces represent the file name looked up by find earlier.
When using find, as long as the desired operation is written in a file, you can use exec to cooperate with find search, which is very convenient. In some operating systems, only the-exec option is allowed to execute commands such as ls or ls-l. Most users use this option to find old files and delete them. It is recommended that before actually executing the rm command to delete files, it is best to use the ls command to take a look at them and make sure that they are the files you want to delete. The exec option is followed by the command or script to be executed, followed by a pair of {}, a space, a\, and finally a semicolon. In order to use the exec option, you must also use the print option. If you verify the find command, you will find that it only prints the relative path and file name from the current path.
The instance 1:ls-l command is placed in the-exec option of the find command
Command:
Find. -type f-exec ls-l {}\
Output:
The code is as follows:
[root@localhost test] # find. -type f-exec ls-l {}\
-rw-r--r-- 1 root root 127 10-28 16:51. / log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47. / test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47. / test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47. / test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54. / log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19. / log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02. / log.log
-rw-r--r-- 1 root root 37 10-28 17:07. / log.txt
-rw-r--r-- 1 root root 0 10-28 14:47. / test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47. / test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47. / test3/log3-1.log
[root@localhost test] #
Description:
In the above example, the find command matches all the normal files in the current directory and uses the ls-l command in the-exec option to list them.
Example 2: find files in the directory that were changed before n days and delete them
Command:
Find. -type f-mtime + 14-exec rm {}\
Output:
The code is as follows:
[root@localhost test] # ll
Total 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
Lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log-> log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxrwx 2 root root 4096 10-28 14:47 test3
Drwxrwxrwx 2 root root 4096 10-28 14:47 test4
[root@localhost test] # find. -type f-mtime + 14-exec rm {}\
[root@localhost test] # ll
Total 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
Lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log-> log.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxrwx 2 root root 4096 11-12 19:32 test3
Drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test] #
Description:
Before deleting a file in any way in shell, you should check the corresponding file first, and be careful! When using commands such as mv or rm, you can use the safe mode of the-exec option. It will prompt you before operating on each matched file.
Example 3: look for files whose change time is earlier than n days in the directory and delete them. Give a prompt before deleting them.
Command:
Find. -name "* .log"-mtime + 5-ok rm {}\
Output:
The code is as follows:
[root@localhost test] # ll
Total 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
Lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log-> log.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxrwx 2 root root 4096 11-12 19:32 test3
Drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test] # find. -name "* .log"-mtime + 5-ok rm {}\
< rm ... ./log_link.log >? Y
< rm ... ./log2012.log >? N
[root@localhost test] # ll
Total 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxrwx 2 root root 4096 11-12 19:32 test3
Drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test] #
Description:
In the above example, the find command looks for all files in the current directory whose filenames end with .log and changes more than 5 days, and deletes them, but prompts you before deleting them. Press y key to delete the file, press n key not to delete.
Use the grep command in the instance 4:-exec
Command:
Find / etc-name "passwd*"-exec grep "root" {}\
Output:
The code is as follows:
[root@localhost test] # find / etc-name "passwd*"-exec grep "root" {}\
Root:x:0:0:root:/root:/bin/bash
Root:x:0:0:root:/root:/bin/bash
[root@localhost test] #
Description:
Any form of command can be used in the-exec option. In the above example, we use the grep command. The find command first matches all files with the file name "passwd*", such as passwd, passwd.old, passwd.bak, and then executes the grep command to see if a root user exists in these files.
Example 5: move the lookup file to the specified directory
Command:
Find. -name "* .log"-exec mv {}.. \
Output:
The code is as follows:
[root@localhost test] # ll
Total 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-12 22:49 test3
Drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test] # cd test3/
[root@localhost test3] # ll
Total 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
[root@localhost test3] # find. -name "* .log"-exec mv {}.. \
[root@localhost test3] # ll
Total 0 [root@localhost test3] # cd..
[root@localhost test] # ll
Total 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-12 22:50 test3
Drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test] #
Example 6: execute the cp command with the exec option
Command:
Find. -name "* .log"-exec cp {} test3\
Output:
The code is as follows:
[root@localhost test3] # ll
Total 0 [root@localhost test3] # cd..
[root@localhost test] # ll
Total 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-12 22:50 test3
Drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test] # find. -name "* .log"-exec cp {} test3\
Cp: ". / test3/log2014.log" and "test3/log2014.log" are the same file
Cp: ". / test3/log2013.log" and "test3/log2013.log" are the same file
Cp: ". / test3/log2012.log" and "test3/log2012.log" are the same file
[root@localhost test] # cd test3
[root@localhost test3] # ll
Total 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test3] #
The above is all the contents of the article "how to use the exec of find commands in linux system". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.