find . -name file.txt
find /var -name file.txt
find /var -iname file.txt
/var/file.txt /var/File.txt /var/FILE.TXT /var/FiLe.TxT
Find all directories whose name is Emre in /home directory.
find /home -type d -name Emre
/Emre
Find all Python files whose name is emre.py in a current working directory.
find . -type f -name emre.py
./emre.py
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) ==== <code bash>find -maxdepth 2 -name passwd
./etc/passwd
find / -maxdepth 3 -name passwd
./usr/bin/passwd ./etc/pam.d/passwd ./etc/passwd
find -mindepth 3 -maxdepth 5 -name passwd
./usr/bin/passwd ./etc/pam.d/passwd
Find all Python files in a directory.
find . -type f -name "*.py"
./file.py ./stuff.py ./linux.py ./devops.py ./unix.py
Find all the files whose permissions are 755.
find . -type f -perm 0755 -print
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 all the SGID bit files whose permissions set to 755.
find / -perm 2755
Find all the Sticky Bit set files whose permission are 777.
find / -perm 1777
Find all SUID set files.
find / -perm /u=s
Find all SGID set files.
find / -perm /g=s
Find all Read Only files.
find / -perm /u=r
Find all Executable files.
find / -perm /a=x
Find all 755 permission files and use chmod command to set permissions to 777.
find / -type f -perm 0755 -print -exec chmod 777 {} \;
Find all 777 permission directories and use chmod command to set permissions to 755.
find / -type d -perm 755 -print -exec chmod 777 {} \;
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. <code bash>find . -type f -name "*.py" -exec rm -f {} \;
OR
find . -type f -name "*.docs" -exec rm -f {} \;
To find all empty files under certain path.
find /home -type f -empty
To file all empty directories under certain path.
find /home -type d -empty
To find all hidden files, use below command.
find /home -type f -name ".*"
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
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 only the socket files.
find . -type s
To find all or single file called file.py under /var var directory of owner root.
find /var -user root -name file.py
To find all files that belongs to user emre under /tmp directory.
find /tmp -user warnaud
To find all files that belongs to group DBA under /tmp directory.
find /tmp -group dba
To find all .py files of user warnaud under /tmp directory.
find /tmp -user warnaud -iname "*.py"
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
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 {} \;"
alias rmcore="find . -iname core -exec rm {} \;"
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 {} \;"
To find all the files which are modified 40 days back.
find / -mtime 40
To find all the files which are accessed 40 days back.
find / -atime 40
To find all the files which are modified more than 50 days back and less than 100 days.
find / -mtime +50 –mtime -100
To find all the files which are changed in last 1 hour.
find / -cmin -60
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
To find all 50MB files, use.
find / -size 50M
To find all the files which are greater than 40MB and less than 90MB.
find / -size +40M -size -90M
To find all 100MB files and delete them using one single command.
find / -size +100M -exec rm -rf {} \;
Find all .mp3 files with more than 10MB and delete them using one single command.
find / -type f -name *.mp3 -size +10M -exec rm {} \;