One Linux command per day (20): exec of find command

Keywords: Linux shell

Links: http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html

Find is a common Linux command, but we usually find out not only to look at it, but also to have further operations, at this time the role of exec appears.

Exc explains:

- The exec parameter is followed by the command command, whose termination is marked by; so the semicolon after the command is indispensable. Considering the different meanings of semicolons in each system, the backslash is added in front.

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

When using find, as long as the desired operation is written in a file, you can use exec to cooperate with find, which is very convenient. In some operating systems, only the - exec option is allowed to execute commands such as l s or l s - L. Most users use this option to find old files and delete them. It is recommended that before you actually execute the rm command to delete files, you should first use the ls command to check 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 and a \, and finally a semicolon. In order to use the exec option, you must use the print option at the same time. 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: ls-l command is placed in the - exec option of 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]#

Explain:

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

Example 2: Find files in the directory that 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]#

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 operating on each matched file.

Example 3: Find files whose change time is before n days in the directory and delete them. Give prompts before deleting them.

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

Explain:

In the example above, the find command finds all files in the current directory that end with. log and change more than 5 days, and deletes them, but gives a hint before deleting them. Press y to delete the file, press n to delete it.

Example 4: -exec uses grep command

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

Explain:

Any form of command can be used in the - exec option. In the example above, we use the 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 files to move to a specified directory

Order:

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

Output:

[root@localhost test]# ll
//Total 12drwxr-xr-x 6 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: Executing 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" For the same document
cp: "./test3/log2013.log" And " test3/log2013.log" For the same document
cp: "./test3/log2012.log" And " test3/log2012.log" For the same document
[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]#

Posted by piersk on Wed, 17 Apr 2019 21:00:34 -0700