Ubuntu

3 ways to find files and directories in Linux

This article will go through how you can find files and directories in Linux. We will be using the locate, find, and grep command. Each command is discussed in detail below.

We have used Ubuntu 20.04 LTS to demonstrate the process.

Using Locate command to find files and directories in Linux

The locate command offers faster results as it reads through the mlocate.db database which has all the file paths in your system.

Install locate

If you get the message below, you need to install locate.

The command for installing locate is:

sudo apt-get install mlocate

The first step is entering your password. Once you enter your password, the installation starts.

You need to update the search database after the installation. Since locate relies on this database, you need to update it regularly to ensure efficiency. You need superuser privileges to update the mlocate database.

sudo updatedb

Searching for files using the filename

To look for files using their names, use the following command:

locate {filename}

We’ll be looking for a file called, “textfile.txt”, so the command will be:

locate textfile.txt

The result will be the path to where your file is located.

Ignoring case sensitivity

By default, locate processes queries in a case-sensitive manner, meaning textfile.txt and TEXTFILE.TXT will point to different search results.

As you can see, TEXTFILE.TXT could not be located.

You can ignore case sensitivity, by using the –i option. The command is:

locate –i {file name}

As you can see, the file has now been located.

Using find command to find files and directories in Linux

Compared to locate, find is a lot more powerful, but slower. It is slower because it searches through your disk for the files. What makes find great is that you can find files and directories in Linux based on user groups, when the files were modified or accessed, file permissions, dates, and size.

Looking for files in the current directory

You can view all the files with a particular name in your current directory by using the following command:

find . -name {filename}

You will see all the files that have the name “textfile.txt” in the current directory.

Looking for files in the home directory

To look for files in your home directory, use /home. The command will be:

find /home -name {filename}

You will see all the files named, Hello.txt in your home directory.

Ignoring case sensitivity

By default, find processes queries in a case-sensitive manner, meaning hello.txt and Hello.txt will point to different search results.

As you can see, hello.txt could not be found.

To ignore case sensitivity, use the following command:

find –iname {file name}

As you can see in the image below, hello.txt has been located.

Find using wildcards

You can use wildcards to search for files given certain criteria. The asterisk (*) wildcard allows you to match instances of a character.

The example below uses the following commands:

find . –name ‘*he’

This command lists all the file names ending with ‘he’

find . –name ‘he*’

This command lists all the file names starting with ‘he’

Find using file size

To find files based on size, use:

find –size {filesize} –type f

This command looks for files that have a size of 68 bytes.

To look up files greater than or smaller than a particular size, add “+” or “” to your command respectively.

find –size -{filesize} –type f

The example below looks for files smaller than 512 bytes.

This command looks for files greater than 512 bytes.

Find files based on when they were last modified

Use the command below to look for files based on when they were last modified:

find -iname "*.txt" -mtime -60

This command looks for files that were modified in the last 60 days.

Find directories

To find a directory, use the command:

find –name “{file name}” –type d

You can see the path to the directory as a result.

Find read-only files

To find read-only files, use the command

find / -perm /u=r

You can see all the read-only files in the terminal.

Using Grep command to find files and directories in Linux

Grep, Global Regular Expression Print is used in Linux to find strings in a particular file. If grep finds those characters, it prints the line. The command is:

grep {string} {file name}

In the example below, we’ll be searching for the string “is” in the file “textfile.txt”. You can see the lines that contain the “is”.

Print line numbers

You print out the line numbers along with the matching string using:

grep {string} {filename} –n

The example below prints the line number where the string “is” is found.

Invert Match

This option lets you print the lines that do not match the pattern you entered.

grep {string} {file name} –v

The example below prints out the line from the text file “textfile.txt” that does not contain “is”.

Ignoring case sensitivity

By default, grep processes queries in a case-sensitive manner, meaning, is and IS will point to different search results.

As you can see, “IS” could not be found.

Use the command below to ignore case:

grep {string} {file name} –i

Recursive search

Grep does not let you search directories, by default. You get the following error:

You can search within directories using:

grep {string} . –R

The example below searches for the string “some” in the current directory.

In this article, we saw how locate, find, and grep work to find files and directories in Linux. We also went through some cases on how they are used.

Similar Posts