10 find exec multiple commands examples in Linux/Unix

In this article I will share multiple find exec examples. Find is a very helpful utility for every system admin for day to day tasks but you can also 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.

 

Find exec multiple commands syntax

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

Syntax to be used for find exec multiple commands:

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

Here,

PATH			Provide the path and locations where you wish to execute find exec grep
OPTIONS			Check man page of find command to get the list of all supported options with find
COMMAND			provide the command which you wish to combine with find
-exec command ;         Execute command; true if 0 status is returned.  All following arguments to find are taken to be arguments to the command
			until an argument consisting of `;' is encountered.  The string `{}' is replaced by the current file name being
			processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some 
			versions of find.

-exec command {} +	This variant of the -exec action runs the specified command on the selected files, but the command line is built by 
			appending each selected file name at the end; the total number of invocations of the command will be much less than 
			the number of matched files.

NOTE:

NOTE:

In this article I will assume that you have basic knowledge on usage of find command or you can refer below article to learn more about find command and it's usage
40 most used examples of find command in Linux or Unix

 

Find exec example 1: Collect md5sum

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

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

Here, -type f means look out for regular file

Similarly you can find exec multiple commands to collect sha512sum or sha256sum for all file with find command. In the same find exec example to store the output to a file

# find /tmp/ -type f -exec md5sum {} \; > /root/checksum_datababse.out

 

Find exec example 2: Remove files older than certain time

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

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

To remove files older than 5 days

# find /tmp/ -type f -mtime +5 -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 'file*' -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

 

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 {} \;

 

Combine find exec with grep in Linux or Unix

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 "deepak" 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 deepak {} \;
This is a dummy config file owned by deepak
This is a dummy config file owned by deepak
This is a dummy config file owned by deepak

 

Combine find exec grep print filename

Now in the above command we get a list of output from files which contains deepak 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 deepak {} \+
/tmp/dir2/text3:This is a dummy config file owned by deepak
/tmp/dir2/text1:This is a dummy config file owned by deepak
/tmp/dir2/text5:This is a dummy config file owned by deepak

Alternatively you can also use below commands to combine find exec grep print filename:

# find /tmp/ -type f -exec grep -i "deepak" {} \; -exec echo {} \;

# find /tmp/ -type f -exec grep -i "deepak" {} \; -print

 

Combine find exec shell script function

We can combine find exec shell script function. In the below example I 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

 

Combine find exec with pipe

We can combine find exec with pipe. You can also use multiple pipes with find exec grep multiple commands and string. In the below example I am 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

 

Combine grep and find exec with sed

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

Here,

  • we find all the files under /tmp/dir1
  • grep for deepak in all the files which were found under /tmp/dir1
  • print the output on STDOUT
  • Use sed and replace deepak with deep

 

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  deepak "$@" | cut -d":" -f1' {} +

 

Lastly I hope the steps from the article to find exec multiple commands in Linux or Unix was helpful. So, let me know your suggestions and feedback using the comment section.

 

Related Searches: find exec multiple commands, find exec grep print filename, find exec example, find exec with pipe, find exec with sed. find exec shell script in Linux or Unix

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.