How to Use the Find and Locate Commands in Linux: A Beginner’s Guide

The command line is a very powerful tool that allows you to view and manage files in Linux. If you want to know how to search a Linux computer or server, you’ve come to the right place.

In this article, we will first talk about the command find of Linux and after the command locate. Both will help you find any file on your machine.

Why use Find and Locate commands in Linux?

New Linux users often say that they are confused by the location of their files on a server. This may be because most people are used to operating with Windows or MacOS, which have a clearer and more user-friendly directory layout.

Although there is some truth to this, Linux gives users more options to search for files using certain commands. In addition to the common filter-based search, it is also possible to find files by user permissions, size, timestamps, etc.

What’s great is that once you understand the commands, searching for files on your Linux platform is incredibly easy.

To do that, we will use the commands find Y locate on Linux.

One important thing to note is that we will be using an Ubuntu VPS in this guide. That said, the steps should also work for Debian, CentOS, or any other Linux distribution. If you don’t know how to connect to a VPS, you can continue before continuing.

Using the Find command in Linux

Let’s start by explaining the command find on Linux and how to use it.

The basic syntax

The command find is the most used command to find and filter files in Linux. The basic layout of this command is as follows:

find

Start with the keyword find, which alerts Linux that the following refers to a search for a file. The argument is the origin point from where you want to start the search. It can be replaced with various arguments, including:

  • / (slash) – searches the entire system.
  • . (point) – look in the folder you are currently working on (current directory).
  • ~ (tilde) – to search from your home directory.

To find out your current directory, use the command pwd.

The second argument is used for your file. This could be the name, type, creation date of the file, etc. The third argument is where the relevant search term will be specified.

Ways to use the find command in Linux

Let’s take a look at the various options that Linux provides to users:

search by name

Of course, the most common and obvious method of searching for a file is to use its name. To run a simple search query using the file name, use the command find as follows:

find . -name my-file

We use the option -yam and look for a file called my-file. Note that we start the search in our current directory using the argument . (point).

See also  URI vs URL: Differences and when to use them

Remember that the argument -yam searches for terms that are case sensitive on Linux. If you know the name of the file, but aren’t sure of its case, use the find command like this:

find . -name my-file

You can also search for all files without a keyword in the name. You can do this in two ways. The first method involves the use of the keyword -not as follows:

find . -not -name my-file

In the second, we can use the exclamation mark (!), although it must be preceded by the escape identifier (\) so that Linux knows that this is part of the command find.

find . \! -name my-file

You can also search for multiple files with a common format like .txt:

find . -name “*.txt”

This will list all text files starting with the current folder.

Finally, if you want to search for a certain file by name and delete it, use the argument -delete after the filename:

find . -name my-file -delete

Search by type

Linux allows users to list all information based on its types. There are several filters you can use:

  • d – directory or folder
  • F – regular file
  • he – symbolic link
  • c – character devices
  • b – block devices

A simple example of using the file type for searching can be seen below:

find / -type d

This will list all the directories present on your file system, having started the search from our root directory with the slash / symbol.

You can also concatenate the options -type Y -yam to make your searches even more specific:

find / -type f -name my-file

This will search for files named my-fileexcluding directories or links.

search by date

If you want to search for files based on their access date and modification date records, Linux offers you the tools to do so. There are 3 timestamps that Linux keeps track of in files:

  • Access time (-atime) – Most recent date the file was read or written.
  • Modification time (-mtime) – Most recent date the file was modified.
  • Change time (-ctime) – Most recent date the file’s metadata was updated.

This option should be used with a number that specifies how many days it has been since the file was accessed, modified, or changed:

find / -atime 1

This command will show all the files that were accessed a day ago from the current time.

We can make our queries more precise by adding the signs plus (+) Y less (-) preceding the number of days. For example:

find / -mtime +2

This will list all files that have a modification time of more than two days.

To find all files whose metadata was updated less than a day ago, run the following:

See also  What is a WordPress plugin? Complete guide for beginners

find / -ctime -1

Although not used often, there are some additional arguments that are also related to date searches. The argument -mmin searches for modified files based on minutes. It can be used like this:

find / -mmin -1

Furthermore, the argument -newer can be used to compare the age of two files and find the most recent one.

find / -newer my-file

You will get all the files that have been modified less time ago than your archive.

Search by size

Linux gives you the option to search for files based on their sizes. The basic syntax for searching files by size is:

find -size

You can specify the following size units:

  • c – bytes
  • k – kilobytes
  • M – megabytes
  • G – gigabytes
  • b – 512-byte chunks

A simple example of how to use the command find Linux for file sizes is as follows:

find / -size 10M

This will search your system for files that are exactly 10 megabytes in size. Just like when you searched based on time, you can further filter your searches with the plus and minus signs:

find / -size +5G

The above command will list all the files on your drive that are greater than 5 Gigabytes in size.

Search by property

Linux gives you the ability to specify your searches based on file ownership. To search for files of a certain owner, the following command must be executed:

find / -user john

This will return a list of all files owned by the named user John. Similar to usernames, we can also search for files via group names:

find / -group classroom

Search by permissions

Users can search for files based on the file permissions with the option -perm. For example:

find / -perm 644

On Linux, 644 corresponds to read and write permissions. Which means this command will search for all files that only have read and write permissions. You can play with this option a bit more, like so:

find / -perm -644

Adding a script will show all files that have at least 644 permission.

You can (in English) about permissions and the various codes corresponding to other Linux permissions.

Other useful options

In addition to all these file search methods, there are other useful options that you should remember.

For example, to find empty files and folders on your system, use the following:

find / -empty

Similarly, to search for all executables saved on your drive, use the option -exec:

find / -exec

To find readable files, you can run the following command:

find / -read

As you can see, there are a lot of options available for users to tailor their queries perfectly according to their needs. Let us now see the other command that can be used to search for files in Linux.

See also  Squarespace vs WordPress: Which is better for your website?

Using the Locate command in Linux

The command locate is a useful alternative as it is faster than find to perform searches. That’s because it only scans your Linux database instead of the entire system. Also, the syntax is easier to write.

How to install the locate package

By default, Linux does not come with the command locate pre-installed. To get the package, run the following commands in your terminal:

sudo apt-get update sudo apt-get install mlocate

The basic syntax

Now you can use the command to search for files using this syntax:

locate

The command locate standard can sometimes show files that have been deleted, if the database was not updated. The best solution is to manually update the database by running the following:

sudo updatedb

How to use the Linux locate command

We will share below the most common applications of the command locate of Linux.

Search by exact file name

The basic syntax only allows you to search for files that contain the search term. If you want to get the file with the exact name, you can use the option -r and add the dollar sign ($) at the end of the search term, for example:

locate -r my-file$

Count the number of files

To find out how many files appear in the search result, enter -c after the locate command.

locate -c my-file

Instead of listing all the files, it will give you the total number of them.

Ignore case sensitivity

Uses -Yo at your command locate to ignore case sensitivity. For example:

locate -i my-file

All files with this name will be displayed, regardless of their case.

show existing files

As we have mentioned, the command locate Linux can even show you a deleted file if you haven’t updated the database. Fortunately, you can solve this using the option -andSo:

locate -e my-file

By doing this, you will only get the files that exist at the time of executing the command locate.

Disable errors during search

The option -q will prevent any errors from appearing when the search is processed. To do this, simply enter:

locate -q my-file

Limit the number of search results

If you want to limit the number of search results, -n it will work. However, remember that you must put the option at the end of the command line. Take a look at this example:

locate my-file n 10

The script just…

Loading Facebook Comments ...
Loading Disqus Comments ...