How do you use find with -exec?

Why Interviewers Ask This

Mid-level Linux / Shell Scripting roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

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.

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Linux / Shell Scripting answers easy to follow.