The exec explain
-exec
The argument is followed by
command
Command, whose termination is by; Is the end of the command, so the semicolon after the command is indispensable, considering that the semicolon will have different meanings on different systems, so the front is used with a backslash.
{}
The curly braces represent the filename previously found by find.
When you use find, you can use it by simply writing the desired operation in a file
exec
To cooperate with
find
It’s easy to find. In some operating systems only
-exec
Options such as
ls
or
ls -l
Such an order. Most users use this option to find old files and delete them. It is recommended that before actually executing the rm command to delete files, it is best to look at them with the ls command to make sure that they are the files to be deleted.
exec
Options are followed by a command or script to execute, followed by a pair
{}
, a space and a space
\
, and finally a semicolon. In order to use
exec
Options, must be used together
find
Command, you will see that the command only prints the relative path and filename from the current path.
Finds all properties in the current directory.txt
File and print out the detailed file information
> find . -type f -name "*.txt" -exec ls -l {} \;
Find the contents of the current directory modified 30 days ago.log
File and delete
> find . -type f -name "*.log" -mtime +30 -exec rm {} \;
in
shell
Before deleting a file by any means, be careful to look at the corresponding file! When used such as
mv
or
rm
Command, can be used
-exec
Options for the safe mode. It will prompt you before acting on each matched file.
Find the contents of the current directory modified 30 days ago.log
File and delete, give a prompt before deletion
> find . -type f -name "*.log" -mtime +30 -ok rm {} \;
In the above example, the find command looks for all files in the current directory whose names end in.log and have changed more than 30 days ago, and deletes them, albeit with a prompt before deleting them. Press Y key to delete the file, press N key not to delete.
-exec
usegrep
> find / -name "passwd*" -exec grep "root" {} \;
Any form of command can be used in the -exec option. In the example above we use the grep command. The find command first matches all file names
passwd*
And then execute the grep command to see if a root user exists in any of these files
Find all the files in the current directory.log
File and move to the specified directory
> find . -name "*.log" -exec mv {} .. /rumenz \;
The original link: https://rumenz.com/rumenbiji/… WeChat official account: entry station