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!
Table of Contents
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.
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!
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.
Above, the searches are case sensitive. If you would like to do a case insensitive search, you can use “-iname” option.
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.
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.
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.
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.
As you can see above, after our command, there is no file like ppp5.php.
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.
We can mention the directory name also in the find line.
To find empty directories, we will use “-type d”. As you can see below, the empty directory that we have created before is there.
Sometimes we need to find hidden files in Linux system. To do this, we will use Linux find command with “.*” like below.
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.
Again, we can do group specific search like below:
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.
Now,, let’s search the files that are modified in ten days.
Laslty, let’s search the files that are changed in then days.
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.
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.
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.
Again, for the changed files, you can use the below Linux find command.
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.
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.
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.
Leave a Reply