scripting:bash:find
Differences
This shows you the differences between two versions of the page.
| scripting:bash:find [2023/08/22 10:36] – created warnaud | scripting:bash:find [2023/08/22 11:18] (current) – warnaud | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Reference ====== | ||
| + | * https:// | ||
| + | |||
| + | ===== file ===== | ||
| + | |||
| + | <code bash> | ||
| + | ===== file in specific dir ===== | ||
| + | <code bash> | ||
| + | ===== file with part of the name ===== | ||
| + | <code bash> find /var -iname file.txt</ | ||
| + | < | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | |||
| + | |||
| + | ===== Find Directories Using Name ===== | ||
| + | |||
| + | Find all directories whose name is Emre in /home directory. | ||
| + | |||
| + | <code bash> | ||
| + | < | ||
| + | |||
| + | ===== Find Python Files Using Name ===== | ||
| + | |||
| + | Find all Python files whose name is emre.py in a current working directory. | ||
| + | |||
| + | <code bash> | ||
| + | < | ||
| + | |||
| + | ===== 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 the passwd file under root and one level down. (i.e root — level 1, and one sub-directory — level 2) ==== | ||
| + | |||
| + | <code bash> | ||
| + | < | ||
| + | ==== 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 the password file between sub-directory level 2 and 4. ==== | ||
| + | |||
| + | <code bash> | ||
| + | < | ||
| + | ./ | ||
| + | |||
| + | ===== Find all Phyton Files in Directory ===== | ||
| + | |||
| + | Find all Python files in a directory. | ||
| + | |||
| + | <code bash> | ||
| + | < | ||
| + | ./stuff.py | ||
| + | ./linux.py | ||
| + | ./devops.py | ||
| + | ./ | ||
| + | |||
| + | ===== Find Files With 755 Permissions ===== | ||
| + | Find all the files whose permissions are 755. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Files Without 777 Permissions ===== | ||
| + | |||
| + | Find all the files without permission 777. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | 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> | ||
| + | < | ||
| + | 34235263434212353445745613213213 | ||
| + | d435123237dasdxz2337523572172144 | ||
| + | dfe123123sdd12312312dsffgdfr1334 | ||
| + | |||
| + | ===== Find SGID Files with 644 Permissions ===== | ||
| + | |||
| + | Find all the SGID bit files whose permissions set to 755. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Sticky Bit Files with 551 Permissions ===== | ||
| + | |||
| + | Find all the Sticky Bit set files whose permission are 777. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find SUID Files ===== | ||
| + | Find all SUID set files. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find SGID Files ===== | ||
| + | |||
| + | Find all SGID set files. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Read Only Files ===== | ||
| + | |||
| + | Find all Read Only files. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Executable Files ===== | ||
| + | |||
| + | Find all Executable files. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== 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 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 and remove single File ===== | ||
| + | |||
| + | To find a single file called emre.py and remove it. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find and remove Multiple File ===== | ||
| + | |||
| + | To find and remove multiple files such as .py or .docs, then use. | ||
| + | |||
| + | <code bash> | ||
| + | OR | ||
| + | <code bash> | ||
| + | |||
| + | ===== Find all Empty Files ===== | ||
| + | |||
| + | To find all empty files under certain path. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find all Empty Directories ===== | ||
| + | |||
| + | To file all empty directories under certain path. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== File all Hidden Files ===== | ||
| + | |||
| + | To find all hidden files, use below command. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== 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> | ||
| + | |||
| + | ===== 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> | ||
| + | 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 Files Based on file-type using option -type ===== | ||
| + | |||
| + | Find only the socket files. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== 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 all Files Based on User ===== | ||
| + | |||
| + | To find all files that belongs to user emre under /tmp directory. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find all Files Based on Group ===== | ||
| + | |||
| + | To find all files that belongs to group DBA under /tmp directory. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Particular Files of User ===== | ||
| + | |||
| + | To find all .py files of user warnaud under /tmp directory. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Files by Size ===== | ||
| + | |||
| + | Using the -size option you can find files by size. | ||
| + | Find files bigger than the given size | ||
| + | |||
| + | <code bash> | ||
| + | Find files smaller than the given size | ||
| + | |||
| + | <code bash> | ||
| + | Find files that matches the exact given size | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== 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> | ||
| + | |||
| + | ===== Remove the core files generated by c program ===== | ||
| + | |||
| + | |||
| + | <code bash> | ||
| + | |||
| + | |||
| + | ===== Remove big archive files using find command ===== | ||
| + | |||
| + | The following command removes *.tar files that are over 300M. | ||
| + | |||
| + | <code bash> | ||
| + | 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 rm1g=" | ||
| + | alias rm2g=" | ||
| + | alias rm5g=" | ||
| + | ===== Time base find ===== | ||
| + | |||
| + | ==== Find Last 40 Days Modified Files ==== | ||
| + | |||
| + | To find all the files which are modified 40 days back. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ==== Find Last 40 Days Accessed Files ==== | ||
| + | |||
| + | To find all the files which are accessed 40 days back. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ==== 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 Changed Files in Last 1 Hour ==== | ||
| + | |||
| + | To find all the files which are changed in last 1 hour. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ==== Find Modified Files in Last 1 Hour ==== | ||
| + | |||
| + | To find all the files which are modified in last 1 hour. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | <code bash> | ||
| + | To find all the files which are accessed in last 1 hour. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find 50MB Files ===== | ||
| + | |||
| + | To find all 50MB files, use. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Size between 40MB – 90MB ===== | ||
| + | |||
| + | To find all the files which are greater than 40MB and less than 90MB. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find and Delete 100MB Files ===== | ||
| + | |||
| + | To find all 100MB files and delete them using one single command. | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | ===== Find Specific Files and Delete ===== | ||
| + | |||
| + | Find all .mp3 files with more than 10MB and delete them using one single command. | ||
| + | |||
| + | <code bash> | ||
