Skip to Content

10 Advanced Find Exec examples in Linux

Are you tired of manually searching through countless files and directories to find what you need?

Look no further than the find exec command!

With this powerful tool, you can easily locate and take action on files that meet your specific criteria.

Simply provide a directory and set your search conditions to quickly find what you’re looking for. And with the exec option, you can perform actions such as deleting or moving those files with ease.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

By learning how to use the find exec command, you can save time and effort in managing your files and directories.

 

Find exec command syntax

There are two syntaxes for find exec.

find /path [args] -exec [cmd] {} \;

  • {} Is a placeholder for the result found by find
  • \; Says that for each found result, the command cmd is executed once with the found result.
  • It is executed like this: cmd result1; cmd result2; …; cmd result N

 

find /path [args] -exec [cmd] {} \+

  • {} Is a placeholder for the result found by find
  • \+ Says that for all found results, the command cmd is executed with all the found results.
  • It is executed like this: cmd result1 result2 … result N

 

when we should use find exec \; other than \+

  • The tool run by -exec does not accept multiple files as an argument
  • Running the tool on so many files at once might use up too much memory
  • We want to start getting some results as soon as possible, even though it will take more time to get all the results

 

Find exec with du – Collect file size

In this example, we will find all files under /tmp and collect the size for each file.

# find /tmp/ -type f -exec du -sh {} \;

Here, -type f means lookout for regular files. With the following find exec example, we can store the output to a file.

# find /tmp/ -type f -exec du -sh {} \; > /root/du_datababse.out

Find exec with rm – Remove files 

In the below find exec example, we will list files older than 10 days

List files older than 10 days under /tmp directory

# find /tmp/ -type f -mtime +10 -exec ls -l {} \;

To remove files older than 10 days

# find /tmp/ -type f -mtime +10 -exec rm -rf {} \;

Here -mtime means file’s data was last modified n*24 hours ago

The /tmp/ directory is typically used to store temporary files, and over time, these files can accumulate and consume disk space unnecessarily.

By running this command, you can automatically delete files that have not been modified in the last 10 days, freeing up disk space and ensuring that only relevant and recent files remain in the /tmp/ directory.

It is important to exercise caution when using the rm command with the -exec option, as it will permanently delete the matched files without further confirmation. Make sure you review and verify the command before executing it to avoid unintentional data loss.

Find exec with mv – Rename files

Suppose you have multiple files spread across different directories that need to be renamed. Using this command, you can search for files matching the specified pattern and rename them in bulk.

Mv command is used to rename files in Linux.

This command we use find exec to rename files where the found files are stored in {} and then the files are renamed with _renamed extension

# find / -type f -name 'howtouselinux*' -exec mv {} {}_renamed \;

The command utilizes the find command to locate files matching the specified criteria and the -exec option to execute the mv command for each matched file.

Find exec with chmod – change file permissions

We can combine find exec with multiple commands in one line. Find will continue to run one by one.

So each consecutive -exec command is executed only if the previous ones returned true (i.e. 0 exit status of the commands).

# find /tmp/dir1/ -type f -exec chown root:root {} \; -exec chmod o+x {} \;

Find exec with grep in Linux

We can combine find exec with grep if we wish to look for files with certain content.

For example below we need the list of files that has the string “howtouselinux”. But find exec grep print filename didn’t work here as we only get matched string.

# find /tmp/ -type f -exec grep -i howtouselinux {} \;

This is a dummy config file owned by howtouselinux
This is a dummy config file owned by howtouselinux
This is a dummy config file owned by howtouselinux

Find exec with grep and print filename

Now in the above command, we get a list of output from files that contain howtouselinux string. But it does not print the filename.

Here find exec grep print filename using different methods. In the below example we will combine find exec andd print filename.

# find /tmp/ -type f -exec grep -i howtouselinux {} \+

/tmp/dir2/text3:This is a dummy config file owned by howtouselinux
/tmp/dir2/text1:This is a dummy config file owned by howtouselinux
/tmp/dir2/text5:This is a dummy config file owned by howtouselinux

Alternatively, we can also use the below commands to combine find exec grep print filename.

# find /tmp/ -type f -exec grep -i “howtouselinux” {} \; -exec echo {} \;
# find /tmp/ -type f -exec grep -i “howtouselinux” {} \; -print

Find exec with shell script function

We can combine find exec with shell script function.

# find ./ -type f -exec bash -c 'ls -lrt {}' \;

Find exec with pipe

We can combine find exec with pipe. In the below example we are going to combine find exec with pipe multiple times.

# find /tmp/dir1/ -type f -exec sh -c 'egrep -i a "$1" | grep -i howtouselinux' sh {} \;

This example is a little complex. It is part of shell programming. We can refer to the following example.

sh -c ‘echo “You gave me $1, thanks!”‘ sh “apples”
You gave me apples, thanks!

That example works like this.
egrep -i a filename |grep -i howtouselinux

Grep and find exec with sed

We can combine find exec with sed or with awk. In the below example we combine grep and find exec with sed.

# find /tmp/dir1/ -type f -exec grep howtouselinux {} \; -exec echo {} \; | sed 's/howtouselinux/deep/g'

Find exec with links

In the below example we will combine find exec with the link option.

# find /tmp/dir1/ -links +1 -type f -exec echo {} \;

Related:

LutzW

Wednesday 14th of February 2024

Enjoyed the examples. Quite often '... -exec rm ...' can be replaced with '... -delete'

David Cao

Monday 19th of February 2024

Thanks.

Ery Fen

Monday 27th of November 2023

Cool. The examples are great. Thanks.