Exec of Linux find command

Time:2021-2-5

Find is a Linux command that we often use, but what we usually find out is not just a look, but also further operation. At this time, the function of exec is revealed.

Exec explains:

-The exec parameter is followed by the command, whose termination is marked by;, so the semicolon after the command is indispensable. Considering that the semicolon has different meanings in different systems, a backslash is added in front of it.

The curly brackets represent the file name found by find.

When using find, as long as you write the desired operation 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 suggested that before executing RM command to delete files, it is better to use LS command to check that they are the files to be deleted. The exec option is followed by the command or script to be executed, followed by a pair of {}, a space and 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 the command only outputs the relative path and file name from the current path.

Example 1:The LS – L command is placed in the – exec option of the find command

Command:


find . -type f -exec ls -l {} \;

Output:


[[email protected] 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
[[email protected] test]#

explain:

In the above example, the find command matches all the normal files in the current directory and lists them with the LS – L command in the – exec option.

Example 2:Search the directory for files that were changed n days ago and delete them

Command:


find . -type f -mtime +14 -exec rm {} \; 

Output:

[[email protected] 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
[[email protected] test]# find . -type f -mtime +14 -exec rm {} \;
[[email protected] 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
[[email protected] test]#

explain:

Before deleting files in any way in the shell, you should check the corresponding files first, and be careful! When using commands such as MV or RM, you can use the security mode of the – exec option. It will prompt you before working on each matched file.

Example 3:In the directory to find the change time in the N days before the file and delete them, give a prompt before deleting

Command:


find . -name "*.log" -mtime +5 -ok rm {} \;

Output:

[[email protected] 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
[[email protected] test]# find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./log_link.log > ? y
< rm ... ./log2012.log > ? n
[[email protected] 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
[[email protected] test]#

explain:

In the above example, the find command finds all the files in the current directory whose names end in. Log and have been changed for more than 5 days, and deletes them, but gives a prompt before deleting them. Press y to delete the file, press n to not delete.

Example 4:-Using grep command in Exec

Command:


find /etc -name "passwd*" -exec grep "root" {} \;

Output:


[[email protected] test]# find /etc -name "passwd*" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[[email protected] test]#

explain:

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 the files named “passwd *”, such as passwd passwd.old 、 passwd.bak And then execute the grep command to see if there is a root user in these files.

Example 5:Find the file and move it to the specified directory

Command:


find . -name "*.log" -exec mv {} .. \;

Output:

[[email protected] test]# ll
Total 12
drwxr-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
[[email protected] test]# cd test3/
[[email protected] 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
[[email protected] test3]# find . -name "*.log" -exec mv {} .. \;
[[email protected] test3]# ll
Total 0
[[email protected] test3]# cd ..
[[email protected] 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
[[email protected] test]#

Example 6:Execute CP command with exec option

Command:


find . -name "*.log" -exec cp {} test3 \;

Output:

[[email protected] test3]# ll
Total 0
[[email protected] test3]# cd ..
[[email protected] 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
[[email protected] 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
[[email protected] test]# cd test3
[[email protected] 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
[[email protected] test3]#

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

IOS dyld loading process (preliminary discussion)

Compilation process of IOS app code source file: load h、. m、. CPP and other documents Pretreatment: replace macros, delete comments, expand header files, and generate I file compile: will I file is converted into assembly language to generate S file assembly: convert assembly file into machine code file to generate O documentation link: Yes O […]