#TIL : Using BSD find util to find and exec command on file and folder


22 May 2017 / by KhanhIceTea

Simple syntax of find

$ find [find_path] -type [file_type] -exec [command] {} \;

Add filename matching pattern to filter the result

$ find [find_path] -name "*.php" -type [file_type] -exec [command] {} \;

Where file_type is :

  • b : block special
  • c : character special
  • d : directory
  • f : regular file
  • l : symbolic link
  • p : FIFO
  • s : socket

Examples:

Fix common file and directory permissions

$ find . -type f -exec chmod 644 {} \;
$ find . -type d -exec chmod 755 {} \;

Check syntax all PHP files

$ find . -type f -name "*.php" -exec php -l {} \; | grep -v 'No syntax errors detected'

Removed all log files

$ find . -type f -name "*.log" -exec rm -f {} \;

WANT MORE ???

$ man find

Sound good ?