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.