Linux find Command
Learn Linux find Command with 40 Examples
Published
- Linux find Command
- find Command Syntax
- find -name to find Files by Name
- Find all the files whose name is python3 in /bin directory
- Find all the files whose name is python3 in /home/ directory
- Case-insensitive file searching using find command
- Case-insensitive file searching excluding directories
- find -type f Searching File excluding directories
- find -type d Searching Directories Using Name
- find all .txt files in all Directories
- Find and remove single File
- Find and remove Multiple File
- Find all Python Files Using Name
- Find SUID Files
- Find SGID Files
- Find Read-Only Files
- Find Executable Files
- Find all Empty Files
- Find all Empty Directories
- Find Files With 777 Permissions
- Find Files Without 777 Permissions
- Find SGID Files with 644 Permissions
- Find Sticky Bit Files with 551 Permissions
- Find Files with 777 Permissions and Chmod to 644
- Find Directories with 777 Permissions and Chmod to 755
- File all Hidden Files
- Find Single File Based on User
- Find all Files Based on User
- Find all Files Based on Group
- Find Particular Files of User
- Find Last 30 Days Modified Files
- Find Last 60 Days Accessed Files
- Find Last 10-20 Days Modified Files
- Find Changed Files in Last 40 Minutes
- Find Modified Files in Last 30 Minutes
- Find Accessed Files in Last 2 Hour
- Find 50MB Files
- Find Size between 100MB – 200MB
- Find and Delete 200MB Size Files
- Find Specific Files and Delete
Linux find Command
In Linux find command is used to search and locate a single or list of files and directories based on conditions you specify for files that match the arguments.
Linux find command can search files by permissions, users, groups, file types, date, size, and other possible criteria.
find Command Syntax
~$ find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
find -name to find Files by Name
Find all the files whose name is python3 in a current working directory.
Command: find . -name python3
~$ find . -name python3
Find all the files whose name is python3 in /bin directory
Command: find /bin/ -name python3
~$ find /bin/ -name python3
output:
~$ /bin/python3
Find all the files whose name is python3 in /home/ directory
Command: find /home/ -name python3
~$ find /home/ -name python3
# This will search files and directories with the name python inside /home/ directory.
# python3 doesn't exist inside /home/ so it won't show anything.
Case-insensitive file searching using find command
Command: find /bin/ -iname python3
~$ find /bin/ -iname python3
# This will search files and directories with the name python in any available case inside /home/ directory.
Case-insensitive file searching excluding directories
Command: find /bin/ -type f -iname python3.8
~$ find /bin/ -type f -iname python3.8
# This will only search files not the directories inside /bin/ directory in any available case.
find -type f Searching File excluding directories
Command: find /bin/ -type f -name python3.8
~$ find /bin/ -type f -name python3.8
# This will only search files not the directories inside /bin/ directory in any available case.
find -type d Searching Directories Using Name
Command: find /usr/ -type d -name games
~$ find /usr/ -type d -name games
find .txt in Current Directory
Command: find . -name “*.txt”
~$ find . -name "*.txt"
find all .txt files in all Directories
Command: find / -name “*.txt”
~$ find / -name "*.txt"
# To stop processing press CTRL + C
Find and remove single File
Command: find . -type f -name “file.txt” -exec rm -f {} ;
~$ find . -type f -name "file.txt" -exec rm -f {} \;
Find and remove Multiple File
Command: find . -type f -name “*.txt” -exec rm -f {} ;
~$ find . -type f -name "*.txt" -exec rm -f {} \;
Find all Python Files Using Name
Find all python files whose name ending with “.py” extension in a current working directory.
~$ find . -type f -name "*.py"
~$ find . -type f -name index.py
Find SUID Files
Said permission is called SUID, which stands for Set owner User ID. This is a special permission that applies to scripts or applications. If the SUID bit is set, when the command is run, it’s effective UID becomes that of the owner of the file, instead of the user running it.
Command: find / -perm /u=s
~$ find / -perm /u=s
Find SGID Files
SGID (Set Group ID up on execution) is a special type of file permissions given to a file/folder. In Linux/Unix when a program executes, it inherits access permissions from the logged-in user.
Command: find / -perm /g=s
~$ find / -perm /g=s
Find Read-Only Files
Command: find /bin/ -perm /u=r
~$ find /bin/ -perm /u=r
Find Executable Files
Command: find /bin/ -perm /a=x
~$ find /bin/ -perm /a=x
Find all Empty Files
Command: find /usr -type f -empty
~$ find /usr -type f -empty
Find all Empty Directories
Command: find /usr -type d -empty
~$ find /usr -type d -empty
Find Files With 777 Permissions
Command: find . -type f -perm 0777 -print
~$ find . -type f -perm 0777 -print
Find Files Without 777 Permissions
Command: find / -type f ! -perm 777
~$ find / -type f ! -perm 777
Find SGID Files with 644 Permissions
Command: find / -perm 2644
~$ find / -perm 2644
Find Sticky Bit Files with 551 Permissions
Command: find / -perm 1551
~$ find / -perm 1551
Find Files with 777 Permissions and Chmod to 644
Command: find / -type f -perm 0777 -print -exec chmod 644 {} ;
~$ find / -type f -perm 0777 -print -exec chmod 644 {} \;
Find Directories with 777 Permissions and Chmod to 755
Command: find / -type d -perm 777 -print -exec chmod 755 {} ;
~$ find / -type d -perm 777 -print -exec chmod 755 {} \;
File all Hidden Files
Command: find /tmp -type f -name “.*”
~$ find /tmp -type f -name ".*"
Find Single File Based on User
To find all or single files called file.txt under / root directory of owner root. Command: find / -user root -name file.txt
~$ find / -user root -name file.txt
Find all Files Based on User
Command: find /home -user root
~$ find /home -user root
Find all Files Based on Group
Command: find /home -group root
~$ find /home -group root
Find Particular Files of User
Command: find /home -user root -iname “*.txt”
~$ find /home -user root -iname "*.txt"
Find Last 30 Days Modified Files
Command: find / -mtime 30
~$ find / -mtime 30
Find Last 60 Days Accessed Files
Command: find / -atime 60
~$ find / -atime 60
Find Last 10-20 Days Modified Files
Command: find / -mtime +10 –mtime -20
~$ find / -mtime +10 –mtime -20
Find Changed Files in Last 40 Minutes
Command: find / -cmin -40
~$ find / -cmin -40
Find Modified Files in Last 30 Minutes
Command: find / -mmin -30
~$ find / -mmin -30
Find Accessed Files in Last 2 Hour
Command: find / -amin -120
~$ find / -amin -120
Find 50MB Files
Command: find / -size 20M
~$ find / -size 20M
Find Size between 100MB – 200MB
Command: find / -size +100M -size -200M
~$ find / -size +100M -size -200M
Find and Delete 200MB Size Files
Command: find / -type f -size +200M -exec rm -f {} ;
~$ find / -type f -size +200M -exec rm -f {} \;
Find Specific Files and Delete
Command: find / -type f -name *.mp3 -size +10M -exec rm {} ;
~$ find / -type f -name *.mp3 -size +10M -exec rm {} \;