How to use the '-exec' option with the 'find' command in Linux to perform actions on found files?
Published on Aug. 22, 2023, 12:17 p.m.
To use the ‘-exec’ option with the ‘find’ command in Linux to perform actions on found files, you can include the ‘-exec’ option at the end of the find command followed by the command you want to execute on each file.
Here is an example command that uses the ‘-exec’ option to search for all files with the extension ‘.txt’ in the current directory and its subdirectories and delete them:
find . -type f -name "*.txt" -exec rm {} \;
This will find all files with the extension ‘.txt’ in the current directory and its subdirectories and execute the ‘rm’ command on each of them, which will delete them.
Note that you need to use curly braces {} to represent the file path and a semicolon \; to terminate the command.
I hope this helps! Let me know if you have any other questions.