User Tools

Site Tools


scripting:bash:find

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

scripting:bash:find [2023/08/22 10:36] – created warnaudscripting:bash:find [2023/08/22 11:18] (current) warnaud
Line 1: Line 1:
 +====== Reference ======
 +  * https://sysaix.com/43-practical-examples-of-linux-find-command
  
 +
 +===== file =====
 +
 +<code bash>find . -name file.txt</code>
 +===== file in specific dir =====
 +<code bash>find /var -name file.txt</code>
 +===== file with part of the name  =====
 +<code bash> find /var -iname file.txt</code>
 +<code>/var/file.txt
 +/var/File.txt
 +/var/FILE.TXT
 +/var/FiLe.TxT</code>
 +
 +
 +===== Find Directories Using Name =====
 +
 +Find all directories whose name is Emre in /home directory.
 +
 +<code bash>find /home -type d -name Emre</code>
 +<code>/Emre</code>
 +
 +===== Find Python Files Using Name =====
 +
 +Find all Python files whose name is emre.py in a current working directory.
 +
 +<code bash>find . -type f -name emre.py</code>
 +<code>./emre.py</code>
 +
 +===== Limit Search To Specific Directory Level Using mindepth and maxdepth =====
 +
 +==== Find the passwd file under all sub-directories starting from root directory. ====
 +
 +
 +<code bash>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</code>
 +<code>./etc/passwd</code>
 +==== Find the passwd file under root and two levels down. (i.e root — level 1, and two sub-directories — level 2 and 3 ) ====
 +
 +<code bash>find / -maxdepth 3 -name passwd</code>
 +<code>./usr/bin/passwd
 +./etc/pam.d/passwd
 +./etc/passwd</code>
 +==== Find the password file between sub-directory level 2 and 4. ====
 +
 +<code bash>find -mindepth 3 -maxdepth 5 -name passwd</code>
 +<code>./usr/bin/passwd
 +./etc/pam.d/passwd</code>
 +
 +===== Find all Phyton Files in Directory =====
 +
 +Find all Python files in a directory.
 +
 +<code bash>find . -type f -name "*.py"</code>
 +<code>./file.py
 +./stuff.py
 +./linux.py
 +./devops.py
 +./unix.py</code>
 +
 +===== Find Files With 755 Permissions =====
 +Find all the files whose permissions are 755.
 +
 +<code bash>find . -type f -perm 0755 -print</code>
 +
 +===== Find Files Without 777 Permissions =====
 +
 +Find all the files without permission 777.
 +
 +<code bash>find / -type f ! -perm 777</code>
 +
 +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.
 +
 +<code bash>find -iname "file.py" -exec md5sum {} \;</code>
 +<code>d41d8cd98f00b25435124128ecf8427e  ./file.py
 +34235263434212353445745613213213  ./var/FilE.py
 +d435123237dasdxz2337523572172144  ./vaar/FILE.py
 +dfe123123sdd12312312dsffgdfr1334  ./FiLe.py</code>
 +
 +===== Find SGID Files with 644 Permissions =====
 +
 +Find all the SGID bit files whose permissions set to 755.
 +
 +<code bash>find / -perm 2755</code>
 +
 +===== Find Sticky Bit Files with 551 Permissions =====
 +
 +Find all the Sticky Bit set files whose permission are 777.
 +
 +<code bash>find / -perm 1777</code>
 +
 +===== Find SUID Files =====
 +Find all SUID set files.
 +
 +<code bash>find / -perm /u=s</code>
 +
 +===== Find SGID Files =====
 +
 +Find all SGID set files.
 +
 +<code bash>find / -perm /g=s</code>
 + 
 +===== Find Read Only Files =====
 +
 +Find all Read Only files.
 +
 +<code bash>find / -perm /u=r</code>
 +
 +===== Find Executable Files =====
 +
 +Find all Executable files.
 +
 +<code bash>find / -perm /a=x</code>
 +
 +===== Find Files with 777 Permissions and Chmod to 644 =====
 +
 +Find all 755 permission files and use chmod command to set permissions to 777.
 +
 +<code bash>find / -type f -perm 0755 -print -exec chmod 777 {} \;</code>
 +
 +===== Find Directories with 755 Permissions and Chmod to 777 =====
 +
 +Find all 777 permission directories and use chmod command to set permissions to 755.
 +
 +<code bash>find / -type d -perm 755 -print -exec chmod 777 {} \;</code>
 +
 +===== Find and remove single File =====
 +
 +To find a single file called emre.py and remove it.
 +
 +<code bash>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 {} \;</code>
 +OR
 +<code bash>find . -type f -name "*.docs" -exec rm -f {} \;</code>
 +
 +===== Find all Empty Files =====
 +
 +To find all empty files under certain path.
 +
 +<code bash>find /home -type f -empty</code>
 +
 +===== Find all Empty Directories =====
 +
 +To file all empty directories under certain path.
 +
 +<code bash>find /home -type d -empty</code>
 +
 +===== File all Hidden Files =====
 +
 +To find all hidden files, use below command.
 +
 +<code bash>find /home -type f -name ".*"</code>
 + 
 +===== 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.
 +
 +<code bash>find . -type f -exec ls -s {} \; | sort -n -r|head -5</code>
 +
 +===== Finding the Top 5 Small Files =====
 +
 +Technique is same as finding the bigger files, but the only difference the sort is ascending order.
 +
 +<code bash>find . -type f -exec ls -s {} \; | sort -n  | head -5</code>
 +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.
 +
 +<code bash>find . -not -empty -type f -exec ls -s {} \; | sort -n  | head -5</code>
 +
 +===== Find Files Based on file-type using option -type =====
 +
 +Find only the socket files.
 +
 +<code bash>find . -type s</code>
 +
 +===== Find Single File Based on User =====
 +
 +To find all or single file called file.py under /var var directory of owner root.
 +
 +<code bash>find /var -user root -name file.py</code>
 + 
 +===== Find all Files Based on User =====
 +
 +To find all files that belongs to user emre under /tmp directory.
 +
 +<code bash>find /tmp -user warnaud</code>
 +
 +===== Find all Files Based on Group =====
 +
 +To find all files that belongs to group DBA under /tmp directory.
 +
 +<code bash>find /tmp -group dba</code>
 +
 +===== Find Particular Files of User =====
 +
 +To find all .py files of user warnaud under /tmp directory.
 +
 +<code bash>find /tmp -user warnaud -iname "*.py"</code>
 +
 +===== Find Files by Size =====
 +
 +Using the -size option you can find files by size.
 +Find files bigger than the given size
 +
 +<code bash>find ~ -size +200M</code>
 +Find files smaller than the given size
 +
 +<code bash>find ~ -size -200M</code>
 +Find files that matches the exact given size
 +
 +<code bash>find ~ -size 200M</code>
 +
 +===== 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
 +
 +<code bash>alias rmfile="find . -iname file.py -exec rm {} \;"</code>
 +
 +===== Remove the core files generated by c program =====
 +
 +
 +<code bash>alias rmcore="find . -iname core -exec rm {} \;"</code>
 +
 +
 +===== Remove big archive files using find command =====
 +
 +The following command removes *.tar files that are over 300M.
 +
 +<code bash>find / -type f -name *.tar -size +300M -exec rm -i {} \;"</code>
 +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.
 +
 +<code bash>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 {} \;"</code>
 +===== Time base find =====
 +
 +==== Find Last 40 Days Modified Files ====
 +
 +To find all the files which are modified 40 days back.
 +
 +<code bash>find / -mtime 40</code>
 +
 +==== Find Last 40 Days Accessed Files ====
 +
 +To find all the files which are accessed 40 days back.
 +
 +<code bash>find / -atime 40</code>
 +
 +==== 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.
 +
 +<code bash>find / -mtime +50 –mtime -100</code>
 +
 +==== Find Changed Files in Last 1 Hour ====
 +
 +To find all the files which are changed in last 1 hour.
 +
 +<code bash>find / -cmin -60</code>
 +
 +==== Find Modified Files in Last 1 Hour ====
 +
 +To find all the files which are modified in last 1 hour.
 +
 +<code bash>find / -mmin -60</code>
 +
 +<code bash>Find Accessed Files in Last 1 Hour</code>
 +To find all the files which are accessed in last 1 hour.
 +
 +<code bash>find / -amin -60</code>
 +
 +===== Find 50MB Files =====
 +
 +To find all 50MB files, use.
 +
 +<code bash>find / -size 50M</code>
 +
 +===== Find Size between 40MB – 90MB =====
 +
 +To find all the files which are greater than 40MB and less than 90MB.
 +
 +<code bash>find / -size +40M -size -90M</code>
 + 
 +===== Find and Delete 100MB Files =====
 +
 +To find all 100MB files and delete them using one single command.
 +
 +<code bash>find / -size +100M -exec rm -rf {} \;</code>
 +
 +===== Find Specific Files and Delete =====
 +
 +Find all .mp3 files with more than 10MB and delete them using one single command.
 +
 +<code bash>find / -type f -name *.mp3 -size +10M -exec rm {} \;</code>