Linux and shell scripting – practical examples of find command

Here are some of the find command examples which I use very often used on Linux in a variety of shell scripts, mostly to delete some old logs, to detect files with low permissions, archiving and copying files changed in a certain level of time and similar actions
Search for a file “httpd.conf” everywhere in computer

find / -type f -name "httpd.conf" 2> /dev/null

find – command for searching
/ – to search where? (in this case on root of all folders).
type – file type (f – regular file, d – folder, l – symlink)
name – word or part of word (use wildcard) which you looking for
2> /dev/null – all errors (2>) generated while search (mostly because you don’t have permissions on files) send to black hole (/dev/null)

Find me on /tmp but not on his subfolders, all files with word “fraza” newer than 25.11.2015 17:00 – and everything what you find – move to /home/draza

find /tmp/ -maxdepth 1 -newermt "2015-11-25 17:00:00" -type f -name "*fraza*" -exec mv {} /home/draza \;

maxdepth – in how much levels in folder (subfolders) to search (1 mean current folder only)
newermt – file is (newer) and….changed (m) in period from (t)
exec – execute command over find result {}

Find me on folder /tmp and all subfolders all files with .log extension which begin with “program” – and it is not case sensitive, where there was not any modification in more than 3 days – and everything what you find – delete.

find /tmp -type f -iname "program*.log" -mtime +3 -exec rm {} \;

iname – same as name but not case sensitive
mtime – modification time (+3 more than 3 days, -3 less than 3 days, 3 – exactly 3 days)

Find me all files in /tmp accessed (by vi, grep, cat….) in the last 10 days

find /tmp/ -type f -atime -10

atime – access time (-10 mean less than 10 days)

Archive me all files changed today from /tmp

tar -czvf ../danas_menjani_fajlovi.tar.gz `find /tmp -type f -daystart -ctime -1`

ctime – file is changed in X*24 hours (chmod, chown… included)
daystart – but from today date

Find me all files in /tmp who are not

find /tmp -not -name ""

Find me all files in /tmp which are newer than file “draza.txt” in home directory

find /tmp -newer /home/draza/draza.txt

Find me all files in current folder who have full permission for all (chmod 777)

find . -type f -perm 0777

Find me all file under /tmp with 777 permissions, and change them permission to 644

find /tmp -type f -perm 0777 -print -exec chmod 644 {} \;

Find me all files in /tmp which owner is draza

find /tmp -user draza

Find me all files in /tmp who are modified in last 2 hours

find /tmp -mmin -120

mmin – modification time in…… -120 in last 120 minutes, +120 before 120 minutes

Give me all files from /tmp who are bigger than 5GB and smaller than 10GB

find /tmp -size +5G -size -10G