10 Linux Find Exec examples - Advanced Part

Updated: 18 hours ago

Find exec command on Linux is very powerful for every Linux system admin. We can combine find with exec option and other commands to filter and execute certain tasks.


For example: find exec grep can print files with certain content. We will also cover find exec Linux examples like find exec rm, find exec pipe, find exec multiple commands, bash find exec, combine find exec with sed or awk.



Linux Find exec multiple commands syntaxes

The find exec causes find command to execute the given command once per file matched. It will place the name of the file wherever we put the {} placeholder. Find exec has two syntaxes.


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




Find exec example 1: Collect file size

In this find exec 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 rm example 2: Remove files older than a certain time

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

Find exec rename example 3: Rename files

Mv command is used to rename files. We can use this with find and exec to rename multiple files.


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

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 exec example 4: Combine find exec multiple commands

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 grep example 5: Combine 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 grep example 6: Combine 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 example 7: Combine 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 pipe example 8: Combine 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


Find exec example 9: Combine 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 example 10: 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:

20 Advanced Linux Find Command Examples

Find File By Name in Linux

20 Advanced Linux Find Command Examples

How to use Find Command in Linux

3 Ways to find the largest files in Linux

10 Linux Find Exec examples - Advanced Part

1,270 views
udemylinux.png

Join our newsletter! Get a free cloud server!  Never miss a post!

Thanks for submitting!