Linux find -exec Command (Multiple Commands, Examples & Practical Guide)

Linux find -exec Command (Multiple Commands, Examples & Practical Guide)

The find -exec option in Linux allows you to execute commands on files that match specific search conditions.

The command becomes extremely powerful when combined with utilities like grep, sed, awk, or shell scripts, enabling batch operations across large directory structures.


Linux find -exec Cheat Sheet

Use CaseCommand ExampleDescription
Print matched filesfind /path -type f -exec echo {} \;
List file detailsfind /path -type f -exec ls -l {} \;
Delete filesfind /path -type f -name "*.log" -exec rm {} \;
Delete files fasterfind /path -type f -name "*.log" -exec rm {} +
Change file permissionsfind /path -type f -exec chmod 644 {} \;
Change file ownershipfind /path -type f -exec chown user:group {} \;
Search text in filesfind /path -type f -exec grep "error" {} \;
Replace text in filesfind /path -type f -exec sed -i 's/old/new/g' {} \;
Run multiple commandsfind /path -type f -exec chown root {} \; -exec chmod 600 {} \;
Rename filesfind /path -type f -name "*.txt" -exec mv {} {}.bak \;
Run shell scriptfind /path -type f -exec bash script.sh {} \;
Use pipes in commandfind /path -type f -exec sh -c 'grep error "$1" | sort' sh {} \;
Find and compress filesfind /path -type f -exec gzip {} \;
Move files to another directoryfind /path -type f -exec mv {} /backup/ \;
Count lines in filesfind /path -type f -exec wc -l {} \;

Linux find -exec Command Syntax

Basic syntax of find -exec

The general syntax of the find -exec command is:

find [PATH] [OPTIONS] -exec COMMAND {} \;

Here:

  • PATH – Directory where the search begins
  • OPTIONS – Conditions such as file name, type, size, or modification time
  • COMMAND – Command executed on each matched file
  • {} – Placeholder replaced with the matched filename

Example:

find /tmp -type f -exec ls -l {} \;

This command lists details of every regular file found under /tmp.

Understanding the {} placeholder

The {} symbol is replaced by the filename returned by the find command. It can appear anywhere in the executed command.

Example:

find /var/log -type f -exec echo "Processing file: {}" \;

Output will show the filename for every matched file.

Difference between ; and +

There are two ways to terminate the -exec command.

Using \;

find /tmp -type f -exec rm {} \;
  • Executes the command once per file
  • Slower when processing many files

Using +

find /tmp -type f -exec rm {} +
  • Passes multiple files to the command at once
  • More efficient for large numbers of files

How find executes commands for each file

When \; is used, find runs the command separately for each file.

Example:

find /tmp -type f -exec echo {} \;

This produces an individual command execution for every file matched.

Using + groups files together, reducing the number of command executions.

Using multiple -exec options in one command

You can run multiple commands sequentially by chaining multiple -exec options.

Example:

find /tmp -type f -exec chown root:root {} \; -exec chmod 600 {} \;

This command:

  1. Changes ownership of each file
  2. Then updates the file permissions

Simple find -exec Examples for Beginners

To print the name of each matched file:

find /tmp -type f -exec echo {} \;

This simply prints every file found.

Display file details using find -exec ls

To list detailed information about matched files:

find /tmp -type f -exec ls -l {} \;

This behaves similar to ls -l on each file returned by find.

Delete files using find -exec rm

To remove files matching a condition:

find /tmp -type f -name "*.log" -exec rm {} \;

This deletes all .log files under /tmp.

Change file permissions using find -exec chmod

You can update permissions for multiple files.

find /var/www -type f -exec chmod 644 {} \;

This sets the permission of all files under /var/www to 644.

Change file ownership using find -exec chown

To update ownership of files:

find /data -type f -exec chown user:group {} \;

This changes the owner and group of matched files.


Running Multiple Commands with find -exec

Execute two commands with find -exec

Example:

find /tmp -type f -exec echo "Processing {}" \; -exec ls -l {} \;

This prints the filename and then shows file details.

Chain multiple -exec commands in a single find

You can execute several operations in sequence.

find /tmp -type f -exec chmod 644 {} \; -exec chown root {} \;

This modifies permissions and ownership for each file.

Using logical AND operations with find -exec

Since each command runs sequentially, you can mimic logical operations.

Example:

find /tmp -type f -exec chmod 600 {} \; -exec echo "Updated permissions for {}" \;

The second command runs after the first one succeeds.

Running sequential commands on matched files

Multiple commands are executed one after another.

Example:

find /tmp -type f -exec cp {} /backup/ \; -exec echo "Copied {}" \;

This copies each file and prints confirmation.


Using find -exec with grep

Search file content using find exec grep

To search for a pattern inside files:

find /var/log -type f -exec grep "error" {} \;

This scans every file for the word "error".

To show both the file name and matching text:

find /var/log -type f -exec grep -H "error" {} \;

The -H option prints the filename before the matched text.

Search for patterns across multiple directories

Example:

find /etc -type f -exec grep "root" {} \;

This searches configuration files for the word "root".

Case-insensitive search using find exec grep

Use the -i flag for case-insensitive search.

find /var/log -type f -exec grep -i "warning" {} \;

This finds all occurrences of "warning" regardless of case.


Using find -exec with sed

Replace text in files using find exec sed

You can replace text across multiple files using sed with the -i option.

find /etc -type f -name "*.conf" -exec sed -i 's/oldvalue/newvalue/g' {} \;

This command searches for .conf files under /etc and replaces oldvalue with newvalue.

Batch editing configuration files

System administrators often need to update configuration parameters across multiple files.

find /etc -type f -name "*.cfg" -exec sed -i 's/enable=false/enable=true/g' {} \;

This command updates configuration settings across all .cfg files.

Updating file paths using sed and find

If directory paths change, sed can update file references.

find /var/www -type f -name "*.conf" -exec sed -i 's|/old/path|/new/path|g' {} \;

This replaces old directory paths with new ones.

Removing unwanted patterns across files

You can remove or clean unwanted text patterns.

find /tmp -type f -exec sed -i '/DEBUG/d' {} \;

This removes all lines containing the word DEBUG.


Using find -exec with awk

Extract specific columns from files

You can extract columns from files containing structured data.

find /data -type f -name "*.csv" -exec awk -F',' '{print $1, $3}' {} \;

This prints the first and third columns from each CSV file.

Filter log entries using awk

You can filter log files for specific conditions.

find /var/log -type f -exec awk '/ERROR/ {print $0}' {} \;

This prints all log entries containing ERROR.

Summarize data across multiple files

You can generate summaries or statistics.

find /logs -type f -exec awk '{count++} END {print count}' {} \;

This counts the number of lines in each log file.

Process configuration files using awk

You can extract configuration parameters.

find /etc -type f -name "*.conf" -exec awk -F'=' '{print $1}' {} \;

This prints configuration keys from configuration files.


Running Shell Commands with find -exec

Execute bash commands using sh -c

You can execute shell commands with sh -c.

find /tmp -type f -exec sh -c 'echo Processing file: "$1"' sh {} \;

This runs a shell command for each file found.

Run complex scripts on matched files

You can run custom scripts on every matched file.

find /data -type f -exec bash process_file.sh {} \;

This executes a script called process_file.sh on each file.

Passing multiple arguments to shell scripts

You can pass filenames as arguments to scripts.

find /data -type f -exec sh -c 'script.sh "$@"' sh {} +

This sends multiple filenames to the script in batches.

Running custom shell functions with find

You can execute shell logic using loops or conditions.

find /tmp -type f -exec bash -c '
for file do
    echo "Handling $file"
done
' bash {} +

This runs a loop that processes each matched file.


Using find -exec with Pipes

Running piped commands inside find -exec

Example:

find /var/log -type f -exec sh -c 'grep error "$1" | sort' sh {} \;

This searches for the word error and sorts the output.

Using grep and cut together with find

You can extract specific parts of matching results.

find /logs -type f -exec sh -c 'grep ERROR "$1" | cut -d":" -f1' sh {} \;

This prints filenames where ERROR appears.

Combining multiple filters with pipes

Multiple filters can be chained together.

find /var/log -type f -exec sh -c 'grep -i error "$1" | grep -i database' sh {} \;

This searches logs for entries containing both words.

Processing log files with find exec pipe

You can analyze logs across directories.

find /var/log -type f -exec sh -c 'cat "$1" | grep -i warning | wc -l' sh {} \;

This counts warning messages in each log file.


Frequently Asked Questions

1. What does find -exec do in Linux?

The find -exec option allows you to execute a command on files that match the find search criteria. The placeholder {} represents the matched file name.

2. How do you run multiple commands with find -exec?

You can chain multiple -exec options in a single find command. Each command runs sequentially on the matched files.

3. What is the difference between -exec {} \; and -exec {} +?

The ; form runs the command once per file, while + groups multiple files together and runs the command fewer times, improving performance.

4. How do you combine find -exec with grep?

You can search files and pass them to grep using find -exec grep 'pattern' {} ; which searches the pattern in each matched file.

5. Can find -exec be used with pipes?

Yes. You can execute shell commands with pipes using find -exec sh -c 'command1 | command2' {} ;.

Practical Automation Examples with find -exec

The find -exec option is extremely useful for automating repetitive administrative tasks in Linux. By combining find with commands like rm, sed, tar, or custom scripts, you can manage large numbers of files efficiently without writing complex shell scripts.

Cleaning temporary files automatically

Temporary files can accumulate over time and consume disk space. You can use find -exec to remove files older than a certain number of days.

find /tmp -type f -mtime +7 -exec rm -f {} \;

This command removes all files in /tmp that were modified more than 7 days ago.

Bulk editing configuration files

When managing servers, you may need to update configuration parameters across many files.

find /etc -type f -name "*.conf" -exec sed -i 's/old_setting/new_setting/g' {} \;

This replaces configuration values across multiple .conf files.

Managing backup files

You can compress or move files to backup directories automatically.

find /var/log -type f -mtime +30 -exec gzip {} \;

This compresses log files older than 30 days.

Another example moves files to a backup directory:

find /data -type f -mtime +60 -exec mv {} /backup/ \;

Running maintenance scripts on matched files

You can execute custom scripts on files that match specific conditions.

find /var/www -type f -exec bash maintenance_script.sh {} \;

This runs a maintenance script on each file returned by the find command.


Frequently Asked Questions

1. What does find -exec do in Linux?

The find -exec option allows you to execute a command on files that match the find search criteria. The placeholder {} represents the matched file name.

2. How do you run multiple commands with find -exec?

You can chain multiple -exec options in a single find command. Each command runs sequentially on the matched files.

3. What is the difference between -exec {} \; and -exec {} +?

The ; form runs the command once per file, while + groups multiple files together and runs the command fewer times, improving performance.

4. How do you combine find -exec with grep?

You can search files and pass them to grep using find -exec grep 'pattern' {} ; which searches the pattern in each matched file.

5. Can find -exec be used with pipes?

Yes. You can execute shell commands with pipes using find -exec sh -c 'command1 | command2' {} ;.

Summary

The find -exec option is one of the most powerful features of the Linux find command. It allows you to execute commands on files that match search conditions, making it possible to automate file management tasks such as deleting files, updating permissions, modifying content, or running scripts.

By combining find -exec with tools like grep, sed, awk, and shell scripts, you can build flexible automation workflows for system administration and maintenance tasks. Understanding how find processes files and how the {} placeholder works helps you safely execute commands across large numbers of files.


Official Documentation

For more details about the find command and its options, refer to the official documentation:


You may also find the following guides helpful:

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.