Tuesday, August 25, 2020

Unix - Shell Script - Find files and execute a command on matches

Description:

Often time we need to perform additional tasks on file/folder found in a certain path. Instead of fetching the list and looping through the items, it's easier to combine the search and additional task in to one command, more like chaining commands. "find" unix command allows executing additional command to be executed on matched items. This article provides few examples to demonstrate how to do so with explanation.

Example-01: Delete all the log files older than 14 days

find . -maxdepth 1 -type f -mtime +14 -exec rm -fr {} \;

Explanation:

  • Scan current folder path for only files (find .)
  • Skip scanning sub folder(s) (-maxdepth 1)
  • Consider only files, exclude directory (-type f)
  • Match files which are older than 14 days (-mtime +14)
  • Perform delete on matched items (-exec). -exec allows to execute a command on items
  • Forcefully delete any match found without any confirmation message (rm -fr)
  • Use {} (curly braces) as a placeholder for each file that the find command locates
  • Need to add command terminator (\;). This is a must if -exec attribute is used in find command


Example-02: Copy all the matching files 

find . -maxdepth 1 -type f -exec cp {} ~/ \;

Explanation:

  • Copy all matched items in home directory (-exec cp {} ~/). 
  • Other parts are explained in example-01


Example-03: Change permission of matched files

find . -maxdepth 1 -type f -exec chmod 744 {} \;

Explanation:

  • Change the file permission to 744 (-exec chmod 744). 
  • Other parts are explained in example-01


Example-04: Execute multiple commands. Change permission and move all files to a folder 

find . -maxdepth 1 -type f -exec chmod 744 {} \; -exec mv {} ~/test/ \;

Explanation:

  • Chains multiple commands using (-exec) multiple times. Each command must be ended with (\;)
  • First command changes the permission and second command moves the files to test folder under home path.
  • Other parts are explained in example-01


References:

  1. Detail about 'find' command - https://kb.iu.edu/d/admm

No comments: