The usage of exec in find
Use with other commands with the help of the -exec option
Find all root files in the current directory and change ownership to user tom
find . -type f -user root -exec chown tom {} \;
In the above example, {} is used in conjunction with the -exec option to match all files and is then replaced with the corresponding filename.
Find all .txt files in your home directory and delete them
find $HOME/. -name "*.txt" -ok rm {} \;
In the above example, -ok behaves the same as -exec, but it will give a prompt whether to perform the corresponding operation.
Find all .txt files in the current directory and concatenate them into the all.txt file
find . -type f -name "*.txt" -exec cat {} \;> all.txt
Move .log files older than 30 days to the old directory
find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
Find all .txt files in the current directory and print them in the form of "File: file name"
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
Because multiple commands cannot be used in the -exec parameter in a single-line command, the following method can accept multiple commands after -exec
-exec ./text.sh {} \;
Pass external variable value to awk
VAR=10000
echo | awk -v VARIABLE=$VAR '{ print VARIABLE }'
Another way to pass external variables:
var1="aaa"
var2 ="bbb"
echo | awk '{ print v1,v2 }' v1=$var1 v2=$var2
Use when input comes from a file:
awk '{ print v1,v2 }' v1=$var1 v2=$var2 filename
above In the method, variables are separated by spaces as awk command-line arguments following the BEGIN, {} and END statement blocks.