Linux Find Command

linux-find-command-ipcisco-kali-course

In system administration, we work with files. For some reason, we search for files and try to find the. In Linux, there are different file commands. Here, we will focus on Linux Find Command. We will give different examples to show you how to use Linux Find Command. Let’s start!

 

Linux Find Command Examples

 

First of all I have created files to use in our lesson. These files are abc, ppp.php,ppp2.php under root(/) directory and xyz, XYZ, ppp5.php under MyFolder directory. To do this I used the below linux commands.

 

kali@kali:~$ touch abc
kali@kali:~$ touch ppp.php
kali@kali:~$ touch ppp2.php
kali@kali:~$ mkdir EmptyXYZ
kali@kali:~$ mkdir Myfolder
kali@kali:~$ cd MyFolder/
kali@kali:~/MyFolder$ touch xyz
kali@kali:~/MyFolder$ touch XYZ
kali@kali:~/MyFolder$ touch xyz1
kali@kali:~/MyFolder$ touch xyz2
kali@kali:~/MyFolder$ touch ppp5.php

 

Firstly, let’s use linux find command alone with the file. Here, linux will search the file under the current directory. For the below example, there is a file named abc under root directory. So, it will find it.

 

kali@kali:~$ find abc

abc

 


You can Download Linux Cheat Sheet!


 

Case Senstive Search By Name

 

Now, let’s use “-name” option with linux find command. This command will search our file in all directories and it will give the location of the file.

 

find -name abc

 

If we would like to search any file in a specific directory, we can write the name of the folder.  Below, we will search xyz file under MyFolder.

 

kali@kali:~$ find -name abc
./abc
kali@kali:~$ find -name xyz
./MyFolder/xyz

case-sensitive-seach-in-kali-linux


 

Case Insenstive Search By Name

 

Above, the searches are case sensitive. If you would like to do a case insensitive search, you can use “-iname” option.

 

kali@kali:~$ find -iname xyz
./MyFolder/XYZ
./MyFolder/xyz

linux-case-insensitive-search-ipcisco


 

Find a Directory by Name

 

Above, we have searched for files. Now, let’s search for directories. To do this, we will use “type” option and “name” option together. For directories, we will use d as type.

 

kali@kali:~$ find -type d -name MyFolder
./MyFolder

 


 

Find PHP Files By Name

 

AS we have showed above, we can find files by names. This can be any text file or any other specific files. For example, you can find only php files by full filename.

 

kali@kali:~$ find -type f -name ppp.php

./ppp.php

 

Beside this command, we can also use asterisk (*) before the file extension and seach all the similar files. For example to find all the php files, we can use the below command.

 

kali@kali:~$ find -type f -name '*.php'
./ppp.php
./ppp2.php

 


 

Finding Files By File Permissions

 

We have learned how to use Linux find command with files, directories and with specific type of files. Now, let’s learn how to find a files that has specific file permissions. For example in the below example, we will search for the files whose permission is 644.

 

kali@kali:~$ find -type f -perm 644
./abc1
./.config/xfce4/desktop/icons.screen0-813x823.rc
./.config/xfce4/desktop/icons.screen0-1904x910.rc
….
./abc2
….
./MyFolder/xyz2
./MyFolder/XYZ
./MyFolder/xyz
./MyFolder/xyz1
./MyFolder/ppp5.php
….
./ppp2.php

finding-files-by-permission-in-kali-linux


 

Find a File and Remove With Linux Find Command

 

Sometimes we seach files to use them. But sometimes we search them to remove. In Linux, to find and remove a file, we can use below command as an example. Here, we will use find command and “-exec rm -rf {} \;” command in the same line to delete the found file. We will find and remove ppp5.php.

 

kali@kali:~/MyFolder$ ls
ppp5.php  xyz  XYZ  xyz1  xyz2
kali@kali:~/MyFolder$ cd ..
kali@kali:~$ find -type f -name "ppp5.php" -exec rm -rf {} \;
kali@kali:~$ cd MyFolder/
kali@kali:~/MyFolder$ ls
xyz  XYZ  xyz1  xyz2

 

As you can see above, after our command, there is no file like ppp5.php.

 


 

How to Find Empty Files and Directories in Linux

 

To find empty files in Linux system, we can use Linux find command again. This time we will use “-empty” parameter in the search line. If we use this command under any directory, it will find all the empty files under this directory.

 

kali@kali:~$ find -type f -empty
./abc1
./abc2
./ppp.php
./MyFolder/xyz2
./MyFolder/XYZ
./MyFolder/xyz
./MyFolder/xyz1
./MyFolder/ppp5.php
./ppp2.php

 

kali@kali:~$ cd MyFolder/
kali@kali:~/MyFolder$ find -type f -empty
./xyz2
./XYZ
./xyz
./xyz1
./ppp5.php

linux-find-command-empty-files

We can mention the directory name also in the find line.

 

kali@kali:~$ find MyFolder/ -type f -empty
MyFolder/xyz2
MyFolder/XYZ
MyFolder/xyz
MyFolder/xyz1
MyFolder/ppp5.php

 

To find empty directories, we will use “-type d”. As you can see below, the empty directory that we have created before is there.

 

kali@kali:~$ find -type d -empty
./.config/xfce4/xfwm4
./Documents
./.gnupg/private-keys-v1.d
./Videos
./Public
./.local/share/icc
./.cache/vmware/drag_and_drop/kcu7YJ
./Music
./Pictures
./EmptyXYZ
./Desktop
./Downloads
./Templates

 


 

How to Find Hidden Files With Linux Find Command

 

Sometimes we need to find hidden files in Linux system. To do this, we will use Linux find command with “.*” like below.

 

kali@kali:~$ find -type f -name ".*"
./.Xauthority
./.dmrc
./.bash_logout
./.viminfo
./.bash_history
./.xsession-errors.old
./.bashrc.original
./.xsession-errors
./.ICEauthority
./.profile
./.bashrc

 


 

Finding Files By User or Group

 

We can find files according to users and groups. Firstly, let’s start with users. We use “-user” command and after that we can mention the name of the user to fine its files. Below, we will list the files of user kali. This is our default user by the way.

 

ali@kali:~$ find -user kali
.
./abc1
./.config
./.config/xfce4
./.config/xfce4/desktop
…

 

Again, we can do group specific search like below:

 

kali@kali:~$ find -group kali
.
./abc1
./.config
./.config/xfce4
./.config/xfce4/desktop

 


 

Finding Files By Date

 

We can search files according to the date of their accessed, modified and changed.

 

To do this search by date, we will use “-atime”, “-mtime” and “-ctime”. As you can see the first letter of this commands show the actions; accessed, modified, changed.

 

Firstly, let’s search by access date and find the files that are accessed in ten days.

 

kali@kali:~$ find / -atime 10

finding-files-by-date-in-linux

Now,, let’s search the files that are modified in ten days.

 

kali@kali:~$ find / -mtime 10

 

Laslty, let’s search the files that are changed in then days.

 

kali@kali:~$ find / -ctime 10

 

We can also define a date range. For example let’s say that we will list the files that are accessed 10 days before to 20 days before.

 

kali@kali:~$ find / -atime +10 -atime -20

 


 

Finding Files By Time

 

We can also do file search with Linux Find Command with time. When it is accessed, modified and changed. This searches are done with minute values.

 

Again, to do this search by time, we will use “-amin”, “-mmin” and “-cmin”. As you can see the first letter of this commands show the actions; accessed, modified, changed.

 

To find the files that are accessed in 30 minutes, we will use the below command.

 

kali@kali:~$ find / -amin -30

 

If we would like to find the modified files in this 30 minutes, we will use the below command. Such a search can be very useful especially for user caused problems on the system. You can find which file has modified before the failure.

 

kali@kali:~$ find / -mmin -30

Again, for the changed files, you can use the below Linux find command.

 

kali@kali:~$ find / -cmin -30

finding-files-by-time-in-linux


 

Finding Files By File Size

 

Sometimes we can do file search with file size. We can do this in Linux system with Linux find command. To do this, we will use “-size” command with the specified size value.

We can use this find command with both finding a specified sized file or the files that is larger than a value and smaller than another value.

 

Below we will find all the files that are 10 MB in the system.

 

kali@kali:~$ find / -size 10M

 

We can also define a size range with Linux find command. To find files that are greater than 10 MB and smaller than 20 MB we will use the below command.

 

kali@kali:~$ find / -size +10M -size -20M

linux-find-command-finding-files-by-size


Last Word For Linux Find Command

 

File search is one of the most used functions in Linux Administration. And an efficient way of this job is using Linux Find command. In this lesson, we have learned how to use Linux Find command with different examples. You can do more practice on these commands and create your own example. This will improve your Linux administration talent.

Back to: Kali Linux Course

Leave a Reply

Your email address will not be published. Required fields are marked *