Linux provides a lot of important tools to make the administrator’s life easy. find command is one of the most important commands in Linux world. Find command provides a search according to the file owner, file size, file change date, filename, etc. Now we can start with simple examples and then go to more complex examples.
find Command Syntax
Find command syntax is like below. First, the path we want to search is given and then different options are provided.
find PATH OPTIONS
Find Files According Name
The easy and fast method to find files is using their names. We search the current directory with a file named tmux. We will use -name option of the find command and the double dot means we want to search the current working directory.
$ find . -name tmux
Find Files Under Home Directory
To search and find files under home directory of all normal users following find command can be issued.
$ find /home -name ismail
Find Files Ignoring Case or Case Insensitive
By default find command search provided text case sensitive. To make a search ignoring case sensitivity prefix -iname parameter with it like below. In this example, we will search for bash as case-insensitively in the whole file system.
$ find / -iname bash

Search For Only Directories
Searching only directories can be done by specifying file type like below. We will use -type option and d to specify file type as the directory.
$ find / -type d -name bash
Find Python Files
Searching python files with their extension can be done like below. The extension py
can be changed anything we want.
$ find . -name '*.py'
Find Files with 777 Permission
777 permission can be a security problem if the files owned by root or high privileged users. We can search for files according to their permissions. We will use -perm
option and the permission value.
$ find / -type f -perm 0777 -print
Find SUID Enabled Files
We can search for files and directories according to their suid
values. We will use -perm
and /u=s
to specify user suid flag.
$ find / -perm /u=s

Find Read-Only Files
Searching read only files can be done below. We will use /u=r
which means user is read-only.
$ find / -perm /u=r
Find Files with Permission 777 and change to 664
Searching files with 777 permission and making them more secure with 664 can be done with the following command. We can execute a command in found files with -exec
option. {}
is used to specify the file name in -exec
part.
$ find / -type f -perm 0777 -print -exec chmod 644 {} \;

-exec
gives the ability to execute a command on the found file.- {} is used to specify founded files to run chmod on these files
Find and Remove Files
There is a simple shortcut to find files and remove them. In this example, we will remove files with .py
extension by using rm
command.
$ find . -type f -name "*.py" -exec rm -f {} \;
Find Empty Files
Find can found empty files. We only need to specify the -empty
option to the find command like below.
$ find / -type f -empty

Find All Hidden Files
Hidden files generally used by operating systems or applications for configuration issues. We can use a single dot as hidden file syntax because we know that hidden files in Linux start with .
point.
$ find /tmp -type f -name ".*"
Find Files For Given User
Search files according to ownership. We will use -user
option to specify the user name. In this example, we want to find root
user files with .txt
extension.
$ find / -user root -name *.txt

Find Files For a Group
Search files according to group ownership. If the group does not exists find give a message like find: ‘developer’ is not the name of an existing group.
$ find /home -group ismail

Find Files Modified Last 5 Days
Search files according to their modification date. We will use -mtime
options which mean modification time to find files that have modified the last 5 days.
$ find /home/ismail/ -mtime 5
Find Files Access Last 5 Days
Search files those accessed last 5 days. We will use -atime
as option for access time.
$ find /home/ismail -atime 5
Find Files Modified Between Last 50 and 100 Days
Search for files modified last 50 and last 100 days.
$ find /home -mtime +50 -mtime -100

Find Files Changed Last 2 Hours
Search for files those changed last 2 hours
$ find /home/ -cmin -120

Find Files Sized 10MB
We can find files according to their size. We will use -size
option for this. In this example, we will find that size is 10MB. We can use -
o +
to specify than lesser or bigger than the given size
$ find /home -size 10M
Find Files According to Size and Delete
Search files those bigger than 100MB and delete them.
$ find /tmp -size +100M -exec rm -rf {} \;
3 thoughts on “Linux Find Command With Examples”