Linux Shell Script Learning Introduction notes 3__linux

Source: Internet
Author: User

Today continue the last Find command content, mainly find and-exec and xargs combination and use.

(9) The Find command can be combined with other commands with the help of an option-exec. -exec is one of the most powerful features of find.

For example, if we want to replace the ownership of a user's entire file with another user (such as root), then you can use-user to find all the files that root owns and then change ownership with-exec. (Note that you must execute the Find command as root to make ownership changes)

Example 1:

# Find. -type f-user root-exec chown slynux {} \;
In this command, {} is a special string that is used in combination with the-EXEC option. For each matching file, {} is replaced with the appropriate file name. For example, the Find command finds two files Test1.txt and Text.txt, and the owner is Slynux, so finding will execute:

Chown Slynux {} It will be resolved to Chown slynux Test1.txt and Chown slynux.

Example 2:

Writes all C program files in a given directory to a single file all_c_files.txt. We can use Find to locate all C files and then combine-exec with the cat command:

$ find. -type f-name "*.C"-exec cat {} \    > All_c_file.txt
-exec can be followed by any command. {} represents a match. For any matching file name, {} will be replaced with the file name changed.

Example 3:

Copy the. txt file 10 days ago to the old directory with the following command:

$ find  -type f-name "*.txt"-mtime-10-exec cp {} old  \;
-exec can combine with printf to generate useful output information. For example

$ find. -type f-name "*.txt"-exec  printf  "Text file:%s\n" {} \;

(10): Let find skip a specific directory

Sometimes you need to jump out of subdirectories to improve performance when searching for directories and performing certain actions. The technique of excluding certain files or directories from the search process is called construction. The method is as follows:

$ find. \ (-name "*.git" prune\)-O (-type f-print\)

# instead of direct use \ (-type f-print\), select the desired filter
The above command prints the name (path) of all files not included in the. git directory.




Related Article

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.