Linux provides us with tools that make it easy for us to find the location of files and commands quickly and efficiently.

Find Commands

whereis

We can use the whereis command to get the location of a command’s binary, source, and user manual.

Here are some options provided by the whereis command:

Option Meaning
-s Only search for source files.
-b Only search for binary files.
-m Only search for manual files.

By default, all three options are used:

1
whereis ls    # This is equivalent to whereis -sbm ls
img

Outputs the sources, binaries and manuals of the ls command

locate

We can also use the locate command to find the location of a command.

Here are some options provided by the locate command:

Option Meaning
-b or ‑‑basename Search by the filename.
-w or ‑‑wholename Search by the path + filename.
-i or ‑‑ignore-case The path or filename can be lowercase or uppercase.

By default, the option -w is used:

1
locate '/bin/ls'    # This is equivalent to locate -w '/bin/ls'
img

Outputs the filename that starts with ls under the /bin directory

Find Files

With the find command, we can find almost all the files we want.

Let’s prepare some directories:

1
2
3
4
5
6
7
8
9
touch file01 file02 file03
mkdir -p dir01/dir01_01/dir01_01_01
mkdir -p dir01/dir01_02/dir01_02_01
mkdir -p dir01/dir01_03/dir01_03_01
mkdir -p dir02/dir02_01/dir02_01_01
mkdir -p dir02/dir02_02/dir02_02_01
mkdir -p dir02/dir02_03/dir02_03_01
ll
tree dir01 dir02
img

Create multiple multilevel directories

Here are some options provided by the find command:

Option Meaning
-name Search by the filename.
-path Search by the pathname.
-type d Search for directories.
-type f Search for regular files.
-type l Search for symbolic(soft) links.

Find By Name

Iterate over all files or directories in a subdirectory whose names start with dir:

1
find -name 'dir*'
img

Finds all the directories or files whose names starts with dir

Let’s look for files or directories that begin with dir01:

1
find -name 'dir01*'
img

Finds all the directories or files whose names starts with dir

Find By Path

If we want to find the directory we want more effectively, we can use the path option.

Let’s look for directories that are structured like this: 01_02/dir

1
find -path '*01_02/dir*'
img

Finds all the directories or files whose names starts with dir

Search for Directories

We can find directory type files with the -type d parameter:

1
find -type d
img

Finds all the directories

Search for Regular Files

We can find regular file type files with the -type f parameter:

1
find -type f
img

Finds all the regular files

We can find symbolic/soft link type files with the -type l parameter:

1
2
3
ln -s file01 file01_link    # This will create a symbolic link called file01_link
ll file01_link
find -type l
img

Finds the created symbolic(soft) links

References WHEREIS(1), LOCATE(1), Findutils, 2 Finding Files, 4 File Name Databases,

Buy me a coffeeBuy me a coffee