Common Linux commands (19)-find exec

Source: Internet
Author: User

Common Linux commands (19)-find exec

Find is a commonly used Linux Command, but we generally look for it not only, but also further operations. At this time, the role of exec becomes apparent.


EXec explanation:

-The exec parameter is followed by the command, and its termination is the end sign. Therefore, the semicolon after this command is indispensable, given that semicolons in various systems have different meanings, the front is followed by a backslash.

{} Curly brackets indicate the names of the files found by the previous find.

When using find, you only need to write the desired operation in a file, and you can use exec to search with find, 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 and delete old files. Before executing the rm command to delete files, we recommend that you use the ls command to check whether 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, a \, and a semicolon. 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 this command only outputs the relative path and file name from the current path.


Instance 1:Place the ls-l command in the-exec option of the find command.

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

Note: The find command matches all common files in the current directory and lists them using the ls-l command in the-exec option.

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

Instance2:Find the files whose modification time is earlier than n days in the directory and delete them.

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

Note: You should check the corresponding file before deleting the file in shell in any way. Be careful! When using commands such as mv or rm, you can use the-exec option safe mode. It will prompt you before performing operations on each matching file.


Instance3:Find and delete files whose modification time is earlier than n in the directory. A prompt is displayed before deletion.

Command: find.-name "*. log"-mtime + 5-OK rm {}\;

Note: In the preceding example, the find command searches for all file names in the current directory. files whose names end with logs and whose change time is more than five days ago are deleted, but a prompt is provided before the deletion. Press y to delete the file, and press n to not delete the file.


Instance4:-Execute the grep command

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

Note: any form of commands can be used in the-exec option. In the above example, we use the grep command. The find command First matches all files named "passwd *", such as passwd and passwd. old, passwd. bak, and then run the grep command to check whether there is a "spp" keyword in these files.


Instance5:Search for files and move them to the specified directory

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


Instance6:Run the cp command with the exec Option

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








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.