find command

Common uses

Deleting old files with find command

Below is an example of deleting MySQL backups older than 7 days

BACKUPLOC="/path/to/backups"
MYSQLBACKUPLOC="$BACKUPLOC/mysql"
find $MYSQLBACKUPLOC/backup*.sql.gz -mtime +7 -exec rm {} \;

Better syntax

JOBLOG="/path/to/job/log"
find $JOBLOG/ -type f -name "rem_20*.log" -mtime +7 -delete

Deleting files with a certain extension across directories

This will delete all *.txt.gzip files

find . -name "*.txt.gz" -exec rm -rf "{}" \;

Created/Modified/Accessed within the last hour

FILELOC="/path/to/loc"
# Created in the last 60 minutes
find $FILELOC -cmin -60
# Modified in the last 60 minutes
find $FILELOC -mmin -60
# Accessed in the last 60 minutes
find $FILELOC -amin -60

QR Code
QR Code tech:linux:find_command (generated for current page)