Finding files is a very common task on any operating system.The Linux Find Command is one of the most important and much used command in Linux sytems.It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions.
In this article, let us review 15 practical examples of Linux find command that will be very useful to both newbies an experts.
1Find Files Using Name in Current Directory
Find all the files whose name is emre.txt in a current working directory.
# find . -name emre.txt ./emre.txt
2Find Files Under Specific Directory
Find all the files under /var directory with name emre.txt.
# find /var -name emre.txt /var/emre.txt
3Find Files Using Name and Ignoring Case
Find all the files whose name is emre.txt and contains both capital and small letters in /var directory.
# find /var -iname emre.txt ./emre.txt ./EmrE.txt ./EMRE.txt
4Find Directories Using Name
Find all directories whose name is Emre in /home directory.
# find /home -type d -name Emre /Emre
5Find PHP 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
6Limit Search To Specific Directory Level Using mindepth and maxdepth
Find the passwd file under all sub-directories starting from root directory.
Emre-MacBook-Air:~ emreozkan$ 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
7Find all PHP Files in Directory
Find all Python files in a directory.
# find . -type f -name "*.py" ./emre.py ./sysaix.py ./linux.py ./devops.py ./unix.py
8Find Files With 755 Permissions
Find all the files whose permissions are 755.
# find . -type f -perm 0755 -print
9Find Files Without 777 Permissions
Find all the files without permission 777.
# find / -type f ! -perm 777
10Executing 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 "emre.py" -exec md5sum {} \; d41d8cd98f00b25435124128ecf8427e ./emre.py 34235263434212353445745613213213 ./var/emre.py d435123237dasdxz2337523572172144 ./vaar/EMRE.py dfe123123sdd12312312dsffgdfr1334 ./eMre.py
11Find SGID Files with 644 Permissions
Find all the SGID bit files whose permissions set to 755.
# find / -perm 2755
12Find Sticky Bit Files with 551 Permissions
Find all the Sticky Bit set files whose permission are 777.
# find / -perm 1777
13Find SUID Files
Find all SUID set files.
# find / -perm /u=s
14Find SGID Files
Find all SGID set files.
# find / -perm /g=s
15Find Read Only Files
Find all Read Only files.
# find / -perm /u=r
16Find Executable Files
Find all Executable files.
# find / -perm /a=x
17Find 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 {} \;
18Find 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 {} \;
19Find and remove single File
To find a single file called emre.py and remove it.
# find . -type f -name "emre.py" -exec rm -f {} \;
20Find 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 {} \;
21Find all Empty Files
To find all empty files under certain path.
# find /home -type f -empty
22Find all Empty Directories
To file all empty directories under certain path.
# find /home -type d -empty
23File all Hidden Files
To find all hidden files, use below command.
# find /home -type f -name ".*"
24Finding 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
25Finding 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
26Find Files Based on file-type using option -type
Find only the socket files.
# find . -type s
27Find Single File Based on User
To find all or single file called emre.py under /var var directory of owner root.
# find /var -user root -name emre.py
28Find all Files Based on User
To find all files that belongs to user emre under /tmp directory.
# find /tmp -user emre
29Find all Files Based on Group
To find all files that belongs to group DBA under /tmp directory.
# find /tmp -group dba
30Find Particular Files of User
To find all .py files of user emre under /tmp directory.
# find /tmp -user emre -iname "*.py"
31Find 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
32Create 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 emre="find . -iname emre.py -exec rm {} \;" # emre
Remove the core files generated by c program. # alias emre2="find . -iname emre2 -exec rm {} \;" # emre2
33Remove 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 {} \;" # rm300m # rm1g # rm2g # rm5g
34Find Last 40 Days Modified Files
To find all the files which are modified 40 days back.
# find / -mtime 40
35Find Last 40 Days Accessed Files
To find all the files which are accessed 40 days back.
# find / -atime 40
36Find 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
37Find Changed Files in Last 1 Hour
To find all the files which are changed in last 1 hour.
# find / -cmin -60
38Find Modified Files in Last 1 Hour
To find all the files which are modified in last 1 hour.
# find / -mmin -60
39Find Accessed Files in Last 1 Hour
To find all the files which are accessed in last 1 hour.
# find / -amin -60
40Find 50MB Files
To find all 50MB files, use.
# find / -size 50M
41Find Size between 40MB – 90MB
To find all the files which are greater than 40MB and less than 90MB.
# find / -size +40M -size -90M
42Find and Delete 100MB Files
To find all 100MB files and delete them using one single command.
# find / -size +100M -exec rm -rf {} \;
43Find 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 {} \;
Hocam merhaba,
AIX’i nerede kullandınız.
Could you show some examples of how to use compount conditions for the find command using the -o (or) and the -a (and) operators?