====== Reference ======
* https://sysaix.com/43-practical-examples-of-linux-find-command
===== file =====
find . -name file.txt
===== file in specific dir =====
find /var -name file.txt
===== file with part of the name =====
find /var -iname file.txt
/var/file.txt
/var/File.txt
/var/FILE.TXT
/var/FiLe.TxT
===== Find Directories Using Name =====
Find all directories whose name is Emre in /home directory.
find /home -type d -name Emre
/Emre
===== Find Python Files Using Name =====
Find all Python files whose name is emre.py in a current working directory.
find . -type f -name emre.py
./emre.py
===== Limit Search To Specific Directory Level Using mindepth and maxdepth =====
==== Find the passwd file under all sub-directories starting from root directory. ====
find / -name passwd
/usr/bin/passwd
/usr/share/uucp/passwd
/private/etc/pam.d/passwd
/private/etc/passwd
==== Find the passwd file under root and one level down. (i.e root — level 1, and one sub-directory — level 2) ====
find -maxdepth 2 -name passwd
./etc/passwd
==== Find the passwd file under root and two levels down. (i.e root — level 1, and two sub-directories — level 2 and 3 ) ====
find / -maxdepth 3 -name passwd
./usr/bin/passwd
./etc/pam.d/passwd
./etc/passwd
==== Find the password file between sub-directory level 2 and 4. ====
find -mindepth 3 -maxdepth 5 -name passwd
./usr/bin/passwd
./etc/pam.d/passwd
===== Find all Phyton Files in Directory =====
Find all Python files in a directory.
find . -type f -name "*.py"
./file.py
./stuff.py
./linux.py
./devops.py
./unix.py
===== Find Files With 755 Permissions =====
Find all the files whose permissions are 755.
find . -type f -perm 0755 -print
===== Find Files Without 777 Permissions =====
Find all the files without permission 777.
find / -type f ! -perm 777
Executing Commands on the Files Found by the Find Command
The find command calculates the md5sum of all the files with the name emre.py (ignoring case). {} is replaced by the current file name.
find -iname "file.py" -exec md5sum {} \;
d41d8cd98f00b25435124128ecf8427e ./file.py
34235263434212353445745613213213 ./var/FilE.py
d435123237dasdxz2337523572172144 ./vaar/FILE.py
dfe123123sdd12312312dsffgdfr1334 ./FiLe.py
===== Find SGID Files with 644 Permissions =====
Find all the SGID bit files whose permissions set to 755.
find / -perm 2755
===== Find Sticky Bit Files with 551 Permissions =====
Find all the Sticky Bit set files whose permission are 777.
find / -perm 1777
===== Find SUID Files =====
Find all SUID set files.
find / -perm /u=s
===== Find SGID Files =====
Find all SGID set files.
find / -perm /g=s
===== Find Read Only Files =====
Find all Read Only files.
find / -perm /u=r
===== Find Executable Files =====
Find all Executable files.
find / -perm /a=x
===== Find Files with 777 Permissions and Chmod to 644 =====
Find all 755 permission files and use chmod command to set permissions to 777.
find / -type f -perm 0755 -print -exec chmod 777 {} \;
===== Find Directories with 755 Permissions and Chmod to 777 =====
Find all 777 permission directories and use chmod command to set permissions to 755.
find / -type d -perm 755 -print -exec chmod 777 {} \;
===== Find and remove single File =====
To find a single file called emre.py and remove it.
find . -type f -name "delete.py" -exec rm -f {} \;
===== Find and remove Multiple File =====
To find and remove multiple files such as .py or .docs, then use.
find . -type f -name "*.py" -exec rm -f {} \;
OR
find . -type f -name "*.docs" -exec rm -f {} \;
===== Find all Empty Files =====
To find all empty files under certain path.
find /home -type f -empty
===== Find all Empty Directories =====
To file all empty directories under certain path.
find /home -type d -empty
===== File all Hidden Files =====
To find all hidden files, use below command.
find /home -type f -name ".*"
===== Finding the Top 5 Big Files =====
The following command will display the top 5 largest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files the command has to process.
find . -type f -exec ls -s {} \; | sort -n -r|head -5
===== Finding the Top 5 Small Files =====
Technique is same as finding the bigger files, but the only difference the sort is ascending order.
find . -type f -exec ls -s {} \; | sort -n | head -5
In the above command, most probably you will get to see only the ZERO byte files ( empty files ). So, you can use the following command to list the smaller files other than the ZERO byte files.
find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
===== Find Files Based on file-type using option -type =====
Find only the socket files.
find . -type s
===== Find Single File Based on User =====
To find all or single file called file.py under /var var directory of owner root.
find /var -user root -name file.py
===== Find all Files Based on User =====
To find all files that belongs to user emre under /tmp directory.
find /tmp -user warnaud
===== Find all Files Based on Group =====
To find all files that belongs to group DBA under /tmp directory.
find /tmp -group dba
===== Find Particular Files of User =====
To find all .py files of user warnaud under /tmp directory.
find /tmp -user warnaud -iname "*.py"
===== Find Files by Size =====
Using the -size option you can find files by size.
Find files bigger than the given size
find ~ -size +200M
Find files smaller than the given size
find ~ -size -200M
Find files that matches the exact given size
find ~ -size 200M
===== Create Alias for Frequent Find Operations =====
If you find some thing as pretty useful, then you can make it as an alias. And execute it whenever you want
Remove the files named a.out frequently
alias rmfile="find . -iname file.py -exec rm {} \;"
===== Remove the core files generated by c program =====
alias rmcore="find . -iname core -exec rm {} \;"
===== Remove big archive files using find command =====
The following command removes *.tar files that are over 300M.
find / -type f -name *.tar -size +300M -exec rm -i {} \;"
Remove all *.zip file that are over 200M using the alias rm200m (Remove 200M). Use the similar concepts and create alias like rm1g, rm2g, rm5g to remove file size greater than 1G, 2G and 5G respectively.
alias rm300m="find / -type f -name *.zip -size +300M -exec rm -i {} \;"
alias rm1g="find / -type f -name *.zip -size +1G -exec rm -i {} \;"
alias rm2g="find / -type f -name *.zip -size +2G -exec rm -i {} \;"
alias rm5g="find / -type f -name *.zip -size +5G -exec rm -i {} \;"
===== Time base find =====
==== Find Last 40 Days Modified Files ====
To find all the files which are modified 40 days back.
find / -mtime 40
==== Find Last 40 Days Accessed Files ====
To find all the files which are accessed 40 days back.
find / -atime 40
==== Find Last 50-100 Days Modified Files ====
To find all the files which are modified more than 50 days back and less than 100 days.
find / -mtime +50 –mtime -100
==== Find Changed Files in Last 1 Hour ====
To find all the files which are changed in last 1 hour.
find / -cmin -60
==== Find Modified Files in Last 1 Hour ====
To find all the files which are modified in last 1 hour.
find / -mmin -60
Find Accessed Files in Last 1 Hour
To find all the files which are accessed in last 1 hour.
find / -amin -60
===== Find 50MB Files =====
To find all 50MB files, use.
find / -size 50M
===== Find Size between 40MB – 90MB =====
To find all the files which are greater than 40MB and less than 90MB.
find / -size +40M -size -90M
===== Find and Delete 100MB Files =====
To find all 100MB files and delete them using one single command.
find / -size +100M -exec rm -rf {} \;
===== Find Specific Files and Delete =====
Find all .mp3 files with more than 10MB and delete them using one single command.
find / -type f -name *.mp3 -size +10M -exec rm {} \;