Geek

Linux Lexicon: Finding Files And Directories In Linux


Finding files and directories in Linux can prove to be quite a tedious task for a beginner. But knowing how to locate a particular file or directory is an indispensable skill. This article is aimed at helping you learn to find files and directories in Linux using the find and locate commands, along with the various options that can be used along with these commands.

At times you may download or save a file, only to realize later that you completely forgot the place where you stored it. These commands can prove to be pretty irksome at times.

While Linux contemporaries such as Windows or Mac have extensively built search tools to help you find files and directories that you are looking for, you can do the same thing using the Terminal in Linux using the commands discussed below:

Find Command

As the name pretty much itself suggests, the find command is used to search for files or directories. It can be used to filter files by name, size, permissions, modification time and more. The format for the find command is as follows:

find [path...] [expression]


The find command recursively searches for files in the path that match the expression provided. In case no arguments are supplied to it, as in the example above, it finds all the files in the current directory.

Finding Files and Directories by Name

The most instinctive way to search for a file is by its name. You can do it in the following manner:

find [path] -name “pattern”

This command performs a case sensitive search and displays the files whose name matches the pattern:


In case you wish to perform a search which ignores the case of the pattern, then you can do it in the following way:

find [path] -iname “pattern”


Bonus Tip: You can even perform a ls on the search result by simply adding the -ls option. Here’s an example:


If you want to find all files except those which match a particular pattern, then you can do it in the following manner:

find [path] -not -name “pattern_to_avoid”

or,

find [path] ! -name “pattern_to_avoid”


Sometimes you might not have an idea as to what the exact name of the file was but might know just a keyword appearing in the name. In order to search such a file, you use the asterisk (*) symbol in the pattern. Suppose you wish to find a file whose name starts with “Lin”, we can do so in the following manner:


Finding Files and Directories by Type

As we told you in our article on Handling File & Directory Permissions, certain characters can be used as depicters for a particular file type, like:

f: Regular File
d: Directory
l: Symbolic Link

If you want to search for a file by its type then all you need to do is this:

find [path] -type <character>

Suppose I wish to find all directories on my Desktop, I can do it in the following manner:


Also Read: 20 Quirky Things You Didn’t Know The Linux Terminal Could Do

Finding Files and Directories by Time

It is also possible to look for files and directories by the time when they were last accessed (atime), modified (mtime), or the last time, the file’s inode metadata was changed (ctime). You can do it in the following way:

find [path] -{mtime,atime,ctime} <no_of_days>

Here’s an example, suppose I wish to find all the files on my desktop that were accessed yesterday. All I have to do is this:


One could also specify minutes instead of days. For example, if I want to search for the files modified in the last minute, I can do this by typing the following command:


In fact, it is even possible to specify a range of time for finding files and directories required. You can do it like this:


This command has been used to find out files that are more than 3 days old but less than 5 days old. The + operator is used to denote the more than criteria and the – operator is used to indicate the less than criteria.

Finding Files and Directories by Size

In order to find files and directories by size, we utilize the -size option along with the find command. We specify the size in number along with a letter indicating the unit of space. The letter could be either of the following:

c: For Bytes
k: For Kilobytes
M: For Megabytes
G: For Gigabytes

Here’s an example of how to find files of size more than 10mb:


Ranges can be specified in the same manner as time by using + and operators.

Also Read: Linux Lexicon: Basic Linux Commands | fossBytes

Bonus Tip: You can also search for files or directories, newer than a particular file or directory by using the -newer option. Here’s how:

 find [path] -newer “filename/directoryname”

Here is an example where I have tried to search for files newer than my file “GIT Commands.odt”:


Finding Files and Directories by Owners and Permissions

It is also possible to find a file or directory having a particular owner or a permission assigned to it. To search for a file belonging to a particular file owner or group owner, you can use the find command in the following manner:

find [path] -user <nameofowner>

or

find [path] -group <nameofgroupowner>


In case you wish to find a file or directory on the basis of the permissions assigned to it, then you can do it by using the find command in this way:

find [path] -perm <permissionnumber>


Locate Command

You may have observed that the find command takes a little extra time to return results because it evaluates each and every file. In order to hasten things up, you can use another command known as ‘locate‘. You can use it in the following manner:

locate [path] pattern


Once a day, a process known as ‘updatedb‘ is run on your system which is responsible for indexing all the files on it. The locate command simply queries the index or database create by updatedb instead of looking for each file on the system like the find command, thereby making it much faster.

But the downside to it is that the data is not in real-time. Suppose if you created a file just a few minutes ago and then run the locate command to find it, there is a significant chance that it might not crop up in the results.

Similarly, files that you might have deleted recently could also pop up in the results, even thought they don’t exist on the system anymore. Although you can run uptadedb manually by running the following command:

sudo updatedb

You can check out the man pages of both the find and locate commands to find out options other than the ones that have been mentioned here. Although searching through the terminal can prove to be tricky at times, but I am sure once you have practiced enough, you will find it to be a much more convenient method for finding files and directories.

Read Our Whole Linux Lexicon Tutorial Series Here

Got any doubts, queries of your own, or any suggestions on the topics that you would like us to cover? Drop them in the comments below.

To Top

Pin It on Pinterest

Share This