10 Linux Find Exec examples - Advanced Part

Updated: Apr 30

Find exec command on Linux is very powerful for every Linux system admin. We can combine find exec multiple commands to filter and execute certain tasks. For example: find exec grep a pattern and print only patching files, use find exec with pipe, combine fix exec with sed or awk in Linux or Unix.

10 Linux Find Exec examples  - Advanced Part
10 Linux Find Exec examples - Advanced Part

Find exec multiple commands syntaxes

The -exec flag to find causes to execute the given command once per file matched, and it will place the name of the file wherever we put the {} placeholder. The command must end with a semicolon, which has to be escaped from the shell, either as \; or as ";".


Find exec example 1: Collect file size

In this find exec example find all files under /tmp and collect md5sum for each file

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

Here, -type f means look out for regular file

Similarly we can find exec multiple commands to collect file size for all file with find command. In the same find exec example to store the output to a file.

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

Find exec example 2: Remove files older than certain time

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

# find /tmp/ -type f -mtime +10 -exec ls -l {} \;
To remove files older than 10 days

# find /tmp/ -type f -mtime +10 -exec rm -rvf {} \;
Here -mtime means file's data was last modified n*24 hours ago

Find exec example 3: Rename files

We use mv command 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 same are renamed with _renamed extension


Find exec example 4: Combine find exec multiple commands

We can combine find exec multiple commands in one line. find will continue to run one by one as long as the entire chain so far has evaluated to true. 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 example 5: Combine find exec with grep in Linux

You can combine find exec grep if you wish to look for files with certain content so use find exec grep. For example below I need the list of files which has string "howtouselinux" using find exec grep. 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 example 6: Combine find exec grep print filename

Now in the above command we get a list of output from files which contains howtouselinux string. But it does not print filename. Here with find exec grep print filename using different methods: In the below example we will combine find exec 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, you can also use 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 shell script function

We can combine find exec shell script function. In the below example We will combine find exec shell script function to rename a file if found

# find /tmp/dir1/ -type f -exec bash -c '
for item do
[[ $item =~ "file1" ]] && mv -v $item ${item}_renamed
done
' bash {} +
Follow pattern matching for more details

Find exec example 8: Combine find exec with pipe

We can combine find exec with pipe. We can also use multiple pipes with find exec grep multiple commands and string. 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 amit' sh {} \; -print

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 -e {}"\n" \; | sed  's/howtouselinux/deep/g'

Find exec example 10: Combine find exec grep with cut or awk

In the below example we will combine find exec grep with cut but in the same command you can combine find exec grep with awk

# find /tmp/dir1/ -type f -exec sh -c 'grep  howtouselinux "$@" | cut -d":" -f1' {} +

562 views

Join our newsletter. Get a free Linux account on Cloud.

Get a Free Cloud Server! 

We can use this cloud server to practice Linux commands. Never miss a post!

Thanks for submitting!