Site icon New Generation Enterprise Linux

Taming `find`: Executing Commands Safely on Found Files

Quick Tip

Taming `find`: Executing Commands Safely on Found Files

Challenge: You need to perform an action on multiple files found by the find command, but you’re concerned about unintended consequences if the command is executed with incorrect arguments or on the wrong files.

The Solution: Use the -exec action with a semicolon (\;) at the end of the command to ensure that each found file is processed individually and safely.

find /path/to/search -name "*.log" -type f -exec chmod 644 {} \;

Why it works: The -exec action allows you to execute a command for each file found. The `{}` placeholder represents the current file being processed, and the \; terminates the command for each individual file, preventing potential issues with batch processing.

Pro-Tip: For slightly better performance on commands that can handle multiple arguments, consider using -exec command {} +, which groups found files into batches for the executed command.

Linux Tips & Tricks | © ngelinux.com | 4/27/2026

0 0 votes
Article Rating
Exit mobile version