One linux command per day (20): exec of the find command

find is a Linux command that we use very often, but what we generally find is not just a look, but also further operations. At this time, the role of exec is revealed. 

exec explains:

The -exec parameter is followed by the command command, and its termination is marked with ;, so the semicolon after this command is indispensable, considering that the semicolon will have different meanings in each system, so add backslash.

{} The curly brackets represent the file name found by the previous find.

When using find, as long as you write the desired operation in a file, you can use exec to search with find, which is very convenient. Some operating systems only allow the -exec option 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 check to confirm 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, the print option must also be used. 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 

Order:

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

output: 

[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]#

illustrate: 

In the above example, the find command matches all ordinary files in the current directory and lists them using the ls -l command in the -exec option.

Example 2: Find files in a directory that were changed before n days and delete them

Order:

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

output:

[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]# 

illustrate:

Before deleting a file in any way in the shell, you should look at the corresponding file, be careful! Safe mode with the -exec option can be used when using commands such as mv or rm. It will prompt you before acting on each matched file. 

Example 3: Find files in a directory with a change time before n days and delete them, giving a prompt before deleting

Order:

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

output:

[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]#

illustrate:

In the above example, the find command finds all files in the current directory whose names end with .log and whose change time is more than 5 days, and deletes them, but gives a prompt before deleting. Press the y key to delete the file, and the n key not to delete it. 

 

Example 4: Using grep command in -exec

Order:

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

output:

[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]#

illustrate:

Any form of command can be used in the -exec option. In the above example we used grep command. The find command first matches all files named "passwd*", such as passwd, passwd.old, passwd.bak, and then executes the grep command to see if there is a root user in these files.

 

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

Order:

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

output:

[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  

Order:

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

output:

[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]#

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851247&siteId=291194637