BASH Find

DATE YESTERDAY
date -d "1 day ago" '+%Y-%m-%d'

LAST X COMMAND USED
Find the last time you used tar...

!tar:p

Run that same command again...

!tar

Thanks to CLIMagic

FILES MATCHING PATTERN AND REMOVE FILE EXTENSION
for FILE in *.conf; do echo "${FILE%%.*}"; done

LARGEST DIRECTORIES OR FILES
Directories...

du -Sh | sort -rh | head -n 15

Files...

find. -type f -exec du -Sh {} + | sort -rh | head -n 15

Thanks to Xmodulo.com

FIND AND TAR FILES
The + means tar will only be called once...

find. -type f -exec tar -czvf backup.tar.gz '{}' \+;

Working example...

find public_html/app/design/frontend/ public_html/skin/frontend/ -type f -mtime -12 -print -exec tar -czvf paullys_tweaked_files.tar.gz '{}' \+;

TEXT IN TYPES OF FILES
find. -type f -name '*.doc' -exec grep -Hn "text here" {} \;

FIND THEN RENAME / ADD EXTENSION TO ALL FILES
find. -type f -exec mv '{}' '{}'.jpg \;

FILES OWNED BY A USER
find /path/to/search/ -user nobody -print

ZERO LENGTH FILES
find /path/to/search/ -size 0

e.g.

Find any zero length files named tmp.* older than 1 day and delete them...

find /tmp/ -type f -size 0 -name 'tmp.*' -mtime +1 -exec rm -rf {} \;

FIND AND DELETE
Find any files older than 7 days, then delete them but show what you are deleting.

find /path/to/directory -type f -mtime +7 -exec rm {} \; -print

FIND AND COPY
Find any text files, then copy them to a different directory.

find. -name "*.txt" -exec cp {} /path/to/directory \;

FIND AND MOVE
Find any text files, then move them to a different directory.

find. -name "*.txt" -exec mv {} /path/to/directory \;

FILES NEWER THAN ANOTHER FILE
touch --date "2011-12-31" /tmp/foo touch /tmp/bar find /tmp/ -type f -newer /tmp/foo

FILES OLDER THAN X FILE
find /path/to/search -type f -name 'fish' \! -newer /path/to/search/in/fish \! -samefile /path/to/search/in/fish |sort

e.g.

find /home/MailScanner/archive-nfs/ -type f -name 'messages' \! -newer /home/MailScanner/archive-nfs/20150327/messages \! -samefile /home/MailScanner/archive-nfs/20150327/messages |sort

TEXT FILES CREATED IN THE LAST 30 DAYS
This will find *.txt files...

find ~/Documents/ -type f -mtime -30 -name '*.txt' -print

This will find true ASCII files...

find ~/Documents/ -type f -mtime -30 -print -exec file {} + | grep ASCII

MOST RECENTLY CHANGED FILES
find. -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort

MULTIPLE FILE TYPES
find /path/to/search/ -type f \( -name "*.epub" -or -name "*.mobi" \) -printf '%f\n' |sort |uniq

EMPTY DIRECTORIES / FOLDERS
find. -empty -type d

ALL EMAIL FORWARD FILES
find /home/ -maxdepth 2 -type f -name '.forward'