Finding a file in Linux can occasionally be more difficult than other operating systems. This is particularly true if you use Linux and must utilize the command line because it lacks a graphical user interface.

It should go without saying that every respectable Linux desktop environment includes a search feature for files and folders. Because this is Linux, if your default desktop doesn't, you can always install and use an app to simplify browsing your directory hierarchy.

The find command's usage will be covered in this manual. You can use various filters and criteria to search for files on your system. The locate command, which can be employed to look for files in a different way, will also be briefly covered.

What is the find command in Linux?

You may quickly and effectively search for files and directories and also block devices with the find command. The find command's basic syntax is provided below:

find /path/ -type f -name file-to-search

The path indicates the anticipated location of the file. The place to begin a file search is here. 

The file descriptors are represented by -type. These are only a few possible ones:

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

And finally, -name represents the file type name you want to search.

Find a file in Linux by the name

Using their names, files can be found in the simplest method possible. You could employ the following syntax to utilize the search command to find a file by name:

find -name "query"

Since this will be case-sensitive, a search for query will not return the same results as a search for Query.

Use the -iname option to find a file by name while dispensing with the query's case:

find -iname "query"

You can invert the search with -not to find all files that don't follow a particular pattern:

find -not -name "query_to_avoid"

An exclamation point (!) can also be used to invert the search, as in the following example:

find \! -name "query_to_avoid"

Remember that if you use !, you must escape it with a backslash (/) so that find can take action before the shell tries to understand it.

Find a file in Linux by type.

With the -type parameter, you can indicate the files you're looking for. It operates as follows:

find -type type_descriptor query

For instance, you could use the following command to find every character device on your computer:

find /dev -type c

In Linux systems, device files are normally mounted in the /dev directory, hence this command expressly only looks for devices in that directory:

Output

/dev/vcsa5

/dev/vcsu5

/dev/vcs5

/dev/vcsa4

/dev/vcsu4

/dev/vcs4

/dev/vcsa3

/dev/vcsu3

/dev/vcs3

/dev/vcsa2

/dev/vcsu2

/dev/vcs2

. . .

With a command similar to the one below, you can look for all files with the.conf extension. This example searches the /usr directory for files that match:

find /usr -type f -name "*.conf"

Output

/usr/src/linux-headers-5.4.0-88-generic/include/config/auto.conf

/usr/src/linux-headers-5.4.0-88-generic/include/config/tristate.conf

/usr/src/linux-headers-5.4.0-90-generic/include/config/auto.conf

/usr/src/linux-headers-5.4.0-90-generic/include/config/tristate.conf

/usr/share/adduser/adduser.conf

/usr/share/ufw/ufw.conf

/usr/share/popularity-contest/default.conf

/usr/share/byobu/keybindings/tmux-screen-keys.conf

/usr/share/libc-bin/nsswitch.conf

/usr/share/rsyslog/50-default.conf

. . .

Find a file in Linux by time and size.

You can filter results by size and time using the find in several different ways.

Size

The -size parameter allows you to filter files based on their size. To accomplish this, you must append a particular suffix to the end of a numerical size number to specify whether you are counting the size in terms of bytes, megabytes, gigabytes, or another scale. 

The following list of frequently used size suffixes:

  • c: bytes
  • k: kilobytes
  • M: megabytes
  • G: gigabytes
  • b: 512-byte blocks

To give an example, the command below will locate all files in the /usr directory that are exactly 50 bytes long:

find /usr -size 50c

Instead, you can use the following syntax to discover files that are shorter than 50 bytes:

find /usr -size -50c

You might use the following command to locate files in the /usr directory that are larger than 700 Megabytes:

find /usr -size +700M

Time 

Linux keeps track of the access, modification, and change times for each file.

  • Access Time: The most recent read-write operation on a file.
  • Modification Time: The most recent time the file's contents were changed.
  • Change Time: The most recent alteration to the file's inode metadata.

You can use the -atime, -mtime, and -ctime parameters to base your find searches on these criteria. 

By passing a value, you must specify how many days in the past you want to search when using any of these options. Like the size options described above, you can add the plus or minus signs to these selections to indicate "more than" or "less than."

Run the following command, for instance, to locate files in the /usr directory that were modified during the last day:

find /usr -mtime 1

Use this command to get files that were accessed during the last day:

find /usr -atime -1

You could use the following command to locate files that last had their meta information altered more than three days ago:

find /usr -ctime +3

Additionally, you can provide minutes rather than days using the companion parameters for these options:

find /usr -mmin -1

The files that have been edited at the last minute will be provided. Using a reference file as a benchmark, the find command can compare files and return the newer ones:

find / -newer reference_file

With this approach, all files on the system that have been created or modified more recently than the reference file will be returned.

Find a file in Linux by owner and permissions.

Using the -user and -group parameters, you can also look for files based on who owns them: users or groups. Run the following command to discover all files held by the syslog user in the /var directory:

find /var -user syslog 

Similarly, you can enter the following to designate files in the /etc directory that belong to the shadow group:

find /etc -group shadow 

Additionally, you can look for files with particular permissions.

Use this syntax to define the permissions using octal notation if you wish to match a precise set of permissions:

find / -perm 644 

This will only match files that have the required permissions.

Find a file in Linux using the locate command.

The locate command is an alternative to find. This command can easily search the entire file system and is frequently quicker.

By changing your package lists and subsequently installing the mlocate package, you can install the command on Debian or Ubuntu using apt:

sudo apt update

sudo apt install mlocate

Instead, you can install mlocate on Rocky Linux, CentOS, and other RedHat-derived distributions by using the dnf command:

sudo dnf install mlocate

Because it uses a database containing a list of all the files on the file system, locating is quicker than finding. 

Although a cron job typically updates this database once each day, you can manually update it using the updatedb command: 

sudo updated

The locate database must constantly be up-to-date if you would like to find new files. They won't appear in your search results if you add new files before the routine cron runs or issue the updatedb command. Several ways to locate let you filter the results. The following syntax is the most basic method to use it to find files:

locate query

Any files and directories that have the string query anywhere within their file path will be matched by this. 

You can use the -b flag to look for files whose "basename" matches the query to return files whose names contain the query itself rather than all files with the query in the directories preceding them:

locate -b query

Use the -e flag to limit the results returned by locate to items that are still in the file system (i.e., files that haven't been deleted since the last updatedb call as well as the current locate call):

locate -e query

The -S option allows you to receive statistics on the data that locate has catalogued:

locate –S

Conclusion 

Finding files on your Linux system is easier using the find and location commands. The choice of which command to use in a given situation is up to you, even though both commands can be strengthened by fusing them with other utilities through pipelines. If you are managing multiple systems or need a reliable environment for running these commands, you might want to consider upgrading to a Linux VPS. For those looking to enhance their server management capabilities, [buying a Linux VPS] can provide a more efficient and controlled experience.

People also read: