Find Command in Linux

Post on the find command in Linux.

Find files modified in the last 7 days:

find /opt/ -mtime +7

Finding Files:

find /opt/ -type f -name "abc*"

Finding Directories:

find /opt/ -type d -name "abc*"

Find and Delete:

find /opt/ -mtime +7 -type f -delete

Specific Filename:

find /opt -name myfile.txt

Using Wildcards:

find /opt -name "*.php"

Ignore Case sensitivity:

find /opt -iname "*.Php"

Limit the Depth:

find /opt/ -maxdepth 2 -name "*.php"

Filters - invert:

find /opt/ -not -name "*.php"
  or
find /opt/ ! -name "*.php"

Combine Multiple Searches:

# not
find /opt/ -name 'abc*' ! -name '*.php'
# or
find /opt/ '*.php' -o -name '*.txt'

Multiple Directories:

find /opt/ /home/ -type f -name "abc*"

Hidden Files:

find ~ -type f -name ".*"

Certain Permissions:

find . -type f -perm 0664
find . -type f ! -perm 0777
# read only
find /etc -maxdepth 1 -perm /u=r
# executables
find /bin -maxdepth 2 -perm /a=x

Belongs to a user:

find . -user bob
find . -user bob -name '*.php'

Belongs to a group:

find /var/www -group developer

Modified 50days ago:

find / -mtime 50

Accessed in the last 50 days:

find / -atime 50

Modified in a time range:

find / -mtime +50 –mtime -100

Modified in the last 30 Minutes:

find /home/bob -cmin -30

Modified in the last hour:

find / -mmin -60

Access files in the last hour:

find / -amin -60

Files that is 150MB:

find / -size 150M

File size in range:

find / -size +50M -size -100M

Older than, bigger than:

find /opt/ -type f -size +4M -ctime +2

Top 5 Largest Files:

find . -type f -exec ls -s {} \; | sort -n -r | head -5

Top 5 Largest, Ascending order:

find . -type f -exec ls -s {} \; | sort -n | head -5

Empty Files:

find /tmp -type f -empty

Empty Directories:

find /tmp -type d -empty

Delete Matching results:

find /tmp -type f -name "*.txt" -exec rm -f {} \;

Delete files larger than 10MB:

find /home/bob/dir -type f -name *.log -size +10M -exec rm -f {} \;