Searching Files & Directory on CentOS, Ubuntu & Linux
☰ In this chapter, you will learn
- How to find a files in CentOS, Ubuntu, and other Linux Distros?
- 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 smith4. Find files by size:
Find all the files inside home directory which is larger than 10 MB.
find /home/ -size +10M5. Find the files by modification date:
Finding all the files which is created or modified within 7 days.
find /home/ -mtime -72. 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] filenameExample:
1. Find a File location:
locate findme.txt2. Update the locate database:
sudo updatedb3. Case Insensitive Search:
locate -i filename.txt4. Display the number of matches:
locate -c filename.txtNote
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" filenameExample:
Here, I am using a "findme.txt" file which contains the following demo content.
1. Finding files which contains the word "quantum".
grep -l "quantum" *2. Finding all the matching words – ignore case
grep -i "QuAnTuM" findme.txt3. 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_nameExample:
whereis lswhereis sudowhereis apt-getNote
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_nameExample:
which lswhich sudowhich rmdirwhich locate6. 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_searchExample:
1. Find the word "quantum" in findme.txt file.
ack "quantum" findme.txt2. Color the searched item.
ack --color "quantum" findme.txt3. No color the searched item.
ack –no-color "quantum" findme.txt4. Case Insensitive search
ack -i "QuAnTuM" findme.txtSummary
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.