The exec explain
The -exec argument is followed by the command command, which terminates with; The semicolon is the end of the command, so the semicolon after this command is indispensable. Considering that the semicolon can have different meanings in different systems, so the backslash is added.
The {} curly braces represent the name of the file found earlier by find.
When using find, it is very convenient to use exec in conjunction with find, as long as the desired operations are written in a file. On 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 old files and delete them. It is recommended that you run the ls command to check the files to be deleted before running the rm command to delete them. The exec option is followed by the command or script to execute, followed by a pair of {}, a space and a \, and finally a semicolon. In order to use the exec option, you must also use the print option. If you verify the find command, you will see that it outputs only the relative path and filename from the current path.
Find all files in the current directory.txt
File and print out detailed file information
> find . -type f -name "*.txt" -exec ls -l {} \;
Copy the code
The modification time in the current directory is 30 days ago.log
File and delete
> find . -type f -name "*.log" -mtime +30 -exec rm {} \;
Copy the code
Before your shell deletes a file in any way, you should look at the file, and be careful! The safe mode of the -exec option can be used when using commands such as mv or rm. It will prompt you before any action is taken on each matched file.
The modification time in the current directory is 30 days ago.log
File and delete, give a hint before deleting
> find . -type f -name "*.log" -mtime +30 -ok rm {} \;
Copy the code
In the example above, the find command looks for all files in the current directory whose file names end in.log and have changed more than 30 days ago and deletes them, but with a hint before deleting them. Press y to delete a file, n to not delete it.
-exec
usegrep
> find / -name "passwd*" -exec grep "root" {} \;
Copy the code
Any form of command can be used in the -exec option. In the example above we used the grep command. The find command first matches all files with the name passwd* and then runs the grep command to see if there is a root user in those files
Find all in the current directory.log
File and move to the specified directory
> find . -name "*.log" -exec mv {} .. /rumenz \;Copy the code
Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station