FIND Command Usage Notes for Webmasters

File manipulation using FIND

These examples are Ubuntu 12.04 and Apache2


find a file by name #

sudo find ~/ -name 'banner3.jpg' 2>/dev/null

find and copy # combined to make a script to double filter

sudo find /media/backup01/* -name '*.txt' -exec cp {} ~/dump \;

+ increases speed

sudo find . -name '*.py' -exec grep --color 'xrange' {} +

FIND and EXEC (execute another command)

sudo find /var/www/ -type f -name "*.jpg" -exec chmod 775 {} \;

FIND and DELETE lines matching pattern in log files

sudo find *.log -type f -exec sed -i -e '/88\.66\.4\.92/d' {} \;

FIND and edit file (not create a new file)

sudo find . -type f -exec sed -i -e 's/pattern/replace/g' {} \;

find all files in /directoryname and copy to another directory /home/username/directory

sudo find /directoryname -iname "*file*.txt" -exec cp {} /home/username/directory \;

useful for upgrading software – list all directories in /var/www THEN copy all files in directory-name to each

sudo find /var/www -maxdepth 1 -type d | cp -Rf ~/directory-name/* '*'

FIND directories 1 level deep and execute a script on them

sudo find -maxdepth 1 -type d | ~/ '*'

FIND directories 1 level deep and move them to another directory

sudo find -maxdepth 1 -type d | mv '*' /home/username/directory-name

FIND and DELETE directories

sudo find /media/ -type d -name 'directory-name' -exec rm -r {} \;

FIND and DELETE images larger than x

sudo find /var/www/ -type f -size +100k -exec rm -r {} \;

FIND and DELETE files older than 5 days | -mtime = days

sudo find /path/to/files* -mtime +5 -exec rm {} \;

More…AWK GREP SED VI and FIND Cheatsheet

You may also like...