Advanced Find Command Examples

Advanced File and Folder Search at Linux

When you purchase a server or VPS they don’t come up with a GUI installation, so searching files over terminal screen or on SSH is the quickest and most used method. Indeed searching files over terminal windows gives us lots of opportunities, while you can search files or folder according to their date, permissions, name,  file type and size you can even run a command over search results.

Search File or Directory by Name

To search files or folders which includes a specific name inside of it we can use –name expression. The usage of this command should be;

find -name ‘example’

Results :
[root@test ~]# find -iname ‘example’
./example

Warning: -name command is case sensitive; it will not show up Example” file. To do this you need to use –iname command.

Find –iname ‘example’

Results :
[root@test ~]# find -iname ‘example’
./example
./Example

This will bring all “example” named files inside your search directory.

But this command also will not bring you all of the files or folders which includes “example” phrase inside of it. Imagine that we are looking for a file which is starting with “example”. For this search we need to use wildcard “*” .

find  -iname ‘example*’

The result will be something like that.

[root@test ~]# find  -iname ‘example*’
./example
./example.conf
./Example
./Example.ted
./example.txt

Or you can use wildcard before filename.

find  -name ‘*example’

If you want to find files with specific extension, usage will be :

find -name ‘*.conf’

This command will bring us all files or folders with .conf extension. Until now we make all searches under our current directory, to make this search from main directory folder you should use this command;

find / -name ‘*.conf’

Results :
/usr/share/doc/dbus-1.10.24/examples/example-system-enable-stats.conf
/usr/share/doc/dbus-1.10.24/examples/example-session-disable-stats.conf
/usr/share/doc/krb5-libs-1.15.1/examples/kdc.conf
/usr/share/doc/krb5-libs-1.15.1/examples/krb5.conf
/usr/share/dbus-1/session.conf

/usr/share/dbus-1/system.conf
/root/example.conf

This command will search all files from the main directory. But searching all files will take time and consumes server resources, so if you know the possible location of the files which you are looking for,  it is better to perform a search as below.

find /root/ -name ‘*.conf’

Result :
/root/example.conf

Searching According to the Size

We can also search files by –size extension. With using a suffix on by adding to the end of value you entered. c for bytes, k for Kilobytes, M for Megabytes, G for Gigabytes, b for 512-byte blocks. Let’s make a trial.

find / -size +700M

Again here and + used for determining the over and lower values. +700M means to find files bigger than 700 Megabytes if you say -700M find files smaller than 700 Megabytes.

Finding Files According to User or Permission

We can search files according to their owner. If we want to search files which own by nginx than we just need to use –useruser”. Check the following statement :

find / -name ‘*.jpg’ -user nginx

If you want to search user groups you can use –group . But sometimes we need to search files according to its permissions . If we need something like that this time we can use             –perm.

find / -perm 644

It will bring us all files or folders with a permission level of 644.

Find Files According to Date or Time

Sometimes we need find files concerning modified date and time. On Linux systems data stored with access times, modification times, and change times and with “-atime”, “-mtime”, and “-ctime” parameters we make a search.  

find /home/ -name ‘*.log’ -mtime -1

With this command, we will search all log files modified last 1 day. You can determine the greater or lower by adding + or  .

Note: Change times means last time of the file’s inode meta-data was changed. Not modification of a file.

But if one day isn’t enough for us we can use minute versions of these commands. To do that we simply add –mmin expression as an example :

find /home/ -name ‘*.log’ -user nginx -mmin -10

This will bring you all log extension files and folders, which is modified in the last 10 minutes under home folder.

This is used mostly for detecting the malware or injection attacks if somebody reaches your files which normally they shouldn’t reach; it can be detected by this command. Beside -mmin -cmin and –amin can be used as well.

Searching Only By Type

There is an expression called –type this will help us narrow our search according to the type of it.

  •    f: a regular file
  •    d: directory
  •    l: symbolic link
  •    c: character devices
  •    b: block devices

If you are searching just a file extension probably you will find only files but in some cases, some directories can be shown up inside of your results. By using extensions above you can filter your search results. For example for searching folders :

find / -type d -name ‘image’

You will find just all folders name with image. If we write f instead of d than it will find all files named as image.

Advanced Searching Combining Find Commands

We can find files or folders with find command but sometimes we may need to apply an additional  command over our search result.

As an example, we want to change the permission level of all folders under a domain name.

find /var/www/domain.com/ -type d -exec chmod 755 {} +

With this command, we will search and find all folders under domain.com and change its permissions to 755. Chmod is well known and used command at Linux. You can start a new command after –exec . If you want to change all files permission you can just type :

find /var/www/domain.com/ -type f -exec chmod 644 {} +

Beside chmod, you can use chown or smillar commands too.

Leave A Comment?