Tutorial corner


find command with exec example

Spread the love

Find command in unix , is  one of the useful command which we use almost daily in our unix/linux env. Last time we discussed about find command in unix with example.In this unix tutorial , we will realize how we can maximize find’s power by going through “find command with exec example in unix” . Hope you enjoy this tutorial  and learn something new related to find command.

find command with exec example

find command with exec example

Find command with exec example

Before we start some example of find command with exec , lets first understand what exactly exec command in unix does ?


By definition , the  exec command replaces the current shell process with the specified command. Normally, when you run a command a new process is spawned (forked). The exec  command does not spawn a new process. Sounds too much technical??  Ok , Just treat as , some command to execute some action .

Check here for find command in unix with example

Lets take some find command with exec example to  understand it better  :).

Unix command to find and delete files

Let suppose you want to find all logs file in current directory and delete it  , you can perform this activity via below unix find command.

find . -type f -name “*.log” -exec rm -f {} \;

  • First part of command i.e. find . -type f -name “*.log” will search all the logs file in current directory.
  • Next it will send its search result to next command via {}. {} in above command contains the argument for next command i.e. rm
  • rm -f option is to remove all files forcefully ( i.e. -f option do for rm command)
  • The “\;” in argument indicates the exec command line has ended.
  • So basically find command give the result of searched file and rm -f {} remove those files via exec command.

Unix find command to find files and change its permission

Similarly if We want to find some file and change it permission , we can combine  find command with chmod .

find / -type f -perm 0777 -print -exec chmod 644 {} \;

Unix find command to find files and change its group

We can  perform multiple action also on searched result . Example we can find the files and change its owner and permission via single command.

 find . -name “*” -exec chgrp -v new_group ‘{}’ \; -exec chmod -v 770 ‘{}’ \;

So in above tutorial , we have gone through  some example of find command with exec.  Hope you enjoyed this article , please feel free to add your comment in the end.


The Author

Alisha Lamba

Hello Friends , I am Alisha Lamba .I love to write article on latest technologies like Informatica , ETL , data warehouse , SQL-PL SQL
Copyright 2015 - Tutorial Corner Frontier Theme