Searching Files & Directory on CentOS, Ubuntu & Linux

In this chapter, you will learn

  1. How to find a files in CentOS, Ubuntu, and other Linux Distros?
  2. How to find a directory in CentOS, Ubuntu, and other Linux Distros?


To effectively manage files and directories on Linux, it's crucial to understand the methods for searching them.

In this chapter, we will research into various search tools, including find locate grep whereis which and ack commands.

We'll explore their usage, syntax, and provide examples to illustrate their functionality.


1. find Command

The find command is a powerful tool in the Linux system that empowers users to locate files and directories based on various criteria, such as file name, file type, size, and modification time.

Syntax:

find [path] [options] [expression]

Path: Specify the path or directory where you want to execute the search operation. If you do not provide a starting directory, the search will commence in the current working directory by default.

Options: This allows you to provide additional parameters to refine your search, enabling categorization based on criteria such as searching by name, searching by type, searching by modification date, and more.

Expression: Specifies the criteria for finding files.

Example:


1. Find files by name:
find /home/ -name "findme.txt"
2. Find all the ".txt" files under "home" directory:
find /home/ -name "*.txt"
3. Find directory:
find /home/ -type d -name smith
4. Find files by size:

Find all the files inside home directory which is larger than 10 MB.

find /home/ -size +10M
5. Find the files by modification date:

Finding all the files which is created or modified within 7 days.

find /home/ -mtime -7

2. locate Command

The locate command quickly identifies the locations of files and directories by utilizing a pre-built database. This database contains file locations, enhancing the efficiency of the command compared to the find command.

Syntax:

locate [options] filename

Example:

1. Find a File location:
locate findme.txt
2. Update the locate database:
sudo updatedb
3. Case Insensitive Search:
locate -i filename.txt
4. Display the number of matches:
locate -c filename.txt
Note

Newly added files cannot be searched using the locate command immediately. To include them in the search, you must first update the locate database using the sudo updatedb command. This ensures that the database is current and includes the latest file information.


3. grep Command

The grep command is an incredibly powerful word-searching tool in Linux-based systems. It utilizes patterns and regular expressions to match words within a .txt file.

Syntax:

grep "pattern" filename

Example:

Here, I am using a "findme.txt" file which contains the following demo content.

Quantum computers, a revolutionary leap in the world of computing, are poised to transform the way we process information and solve complex problems. Unlike classical computers that rely on bits for processing information, quantum computers leverage the principles of quantum mechanics to operate with quantum bits or qubits. In this article, we'll explore the fundamental concepts behind quantum computers, their potential applications, and the challenges they present.
1. Finding files which contains the word "quantum".
grep -l "quantum" *
2. Finding all the matching words – ignore case
grep -i "QuAnTuM" findme.txt
3. Displaying the files which contains the word "quantum"
grep -l "quantum" *

4. whereis command

The whereis command in Linux helps identify the location of binaries, source code, and manual pages associated with a given command. It offers information about the files linked to a specific command.

Syntax:

whereis command_name

Example:

whereis ls
whereis sudo
whereis apt-get
Note

The whereis command has limitations; it cannot locate files that are not associated with a manual page. This means that if a particular file is not documented in a manual page, the whereis command may not be able to find it.


5. which command

The which command is utilized for locating the executable files associated with a given command. It proves to be useful in identifying the specific location of a command's executable files.

Syntax:

which command_name

Example:

which ls
which sudo
which rmdir
which locate

6. ack command

The ack command serves as a more advanced version of the grep command and is widely used by developers for locating matching patterns within large codebases.

Syntax:

ack pattern_to_search

Example:

1. Find the word "quantum" in findme.txt file.
ack "quantum" findme.txt
2. Color the searched item.
ack --color "quantum" findme.txt
3. No color the searched item.
ack –no-color "quantum" findme.txt
4. Case Insensitive search
ack -i "QuAnTuM" findme.txt

Summary

In this tutorial, you have explored various searching commands in Linux, including find locate grep whereis which and ack commands. By mastering these commands, you will gain the ability to efficiently locate files and directories in Ubuntu, CentOS, and other Linux distributions.