🐧 Linux / Shell Scripting
Intermediate
How do you use find with -exec?
Answer
find with -exec runs a command on each matched file. Syntax: find /path -name "*.log" -exec command {} \; — the {} is replaced by the filename, and \; terminates the -exec expression (each file gets its own command invocation). Faster alternative: -exec command {} + — appends all filenames to one command invocation (like xargs). Example: find /tmp -mtime +30 -exec rm {} + deletes files older than 30 days. find . -type f -name "*.php" -exec grep -l "TODO" {} \; finds PHP files containing TODO. Combine with -not, -and, -or for complex filters.
Previous
What is the difference between fg, bg, and & in bash?
Next
What is the difference between source and executing a script?