The role of the -exec parameter in the Linux find command

  

We all know that the Linux command plus different parameters have different effects. The following small series will give you a detailed introduction to the -exec parameter in the Linux fing command. You have an understanding.

exec explained: with the

behind -exec parameter is the command command, its termination is based; to end as flags, following the command sentence semicolon is indispensable In view of the fact that semicolons in different systems have different meanings, a backslash is added in front.

{} The curly braces represent the names of the files found in the previous find.

When using find, just write the desired operation in a file, you can use exec to match find search, very convenient. In some operating systems, only the -exec option is allowed to execute commands such as l s or ls -l. Most users use this option to find old files and delete them. It is recommended to use the ls command to see the files you want to delete before actually executing the rm command to delete files. The exec option is followed by the command or script to be executed, followed by a pair { }, 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: The 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 normals in the current directory. Files, and use the ls -l command in the -exec option to list them.

Instance 2: Find files in the directory that changed before n days and delete them

Command:

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

Output:
Previous123Next page Total 3 pages

Copyright © Windows knowledge All Rights Reserved