How to Safely Execute Commands on Found Files in Ubuntu (find -ok)

The Automation Risk

The Linux find command frequently utilizes the -exec flag to automatically run secondary commands (like rm or chmod) on every single file it discovers. While incredibly powerful, this is extremely dangerous. If your search parameters are slightly incorrect, you might accidentally delete thousands of critical system files in milliseconds. For high-risk operations where you need the power of -exec but demand the safety of manual verification, you must utilize the interactive execution flag.

Using the find Command with -ok

The Linux find command utilizes the -ok flag as a safe, interactive alternative to -exec. It pauses the script and demands explicit user confirmation before acting on each file.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To search the /var/log/ directory for files ending in .old and safely delete them one by one, type the following command exactly:
  3. find /var/log/ -name "*.old" -ok rm {} \;
  4. Press Enter.

Interactive Forensics

The syntax for -ok is identical to -exec; it requires the {} placeholder to represent the matched file, and it must be terminated with a \; (escaped semicolon). However, the execution behavior is fundamentally different. When find locates the first match (e.g., system.old), it halts the entire process and prints a prompt to the terminal: < rm ... /var/log/system.old > ?. You must type y and press Enter to execute the deletion, or type n to skip it. This provides an absolute safety net, allowing system administrators to surgically review destructive commands before the kernel executes them.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.