How to Use Linux Search Commands to Locate Files and Text

Chandana ReddyChandana Reddy
6 min read

Ever found yourself lost in a maze of directories, trying to track down a single file or snippet of text buried somewhere in your Linux system? You're not alone. Whether you're managing servers, writing scripts, or just organizing your personal projects, knowing how to search efficiently is a game-changer.

In this guide, we’ll walk through the most powerful Linux search commands—like find, grep, and locate—and show you how to use them to quickly pinpoint files, scan contents, and streamline your workflow. No fluff, no jargon—just practical examples, clear explanations, and tips you can apply right away.

Let’s dive in and turn your terminal into a search powerhouse. 🧠💻

grep command: - (Global Regular Expression Print): The grep command is used to search for a specific word or pattern in a file. => Syntax: grep “word” filename1 filename2 …
word → the text or pattern you want to search for.
filename → the file where the search should happen.

Open the file using vim, switch to insert mode, and enter some sample text. After entering the data, save and exit the file.
To search for a specific word (e.g., "linux") within the file, use: grep “linux” aws
This command will display all lines in the aws file that contain the word "linux".

To print including line numbers: grep -n “linux” aws

Linux commands are case-sensitive. If you search for "LINUX" (all uppercase), it won’t match "linux" (lowercase) unless you explicitly tell the command to ignore case.

So, to perform a case-insensitive search using grep, add the -i option: grep -in “linux” aws

To count how many lines contain the word "linux" in the file aws, use: grep -c "linux" aws

To Search for Multiple Words in a File: grep -in -e“linux” -e“and” -e“find” aws
e = used in grep to specify multiple patterns to match.

To search for a word in multiple files at once: grep -in “linux” aws azure
To Search for Multiple Words in Multiple Files: grep -in -e ”linux” -e “is” aws azure

Use case: Filtering User Information with grep
getent passwd → Displays a list of all users on the system. If you want to view information for only the root user, you can filter the output using grep → getent passwd | grep -i root

| → pipes the output of the first command into the second. (1st command o/p will be 2nd command’s input)
grep → filters lines containing the word "root"
-i → makes the search case-insensitive (optional here, since "root" is lowercase by default)

Find command: For example, suppose your system has three folders, and each folder contains some files. If you want to find out which folder contains a file named aws, you can use the find command.
Syntax: find path -name filename
Command: find . -name aws

dot represents the path or current directory.

→ If you want to search in folder (eg: linux): find linux -name aws

Step-by-Step Guide to Using the find Command:

Let’s create some directories: mkdir folder{1..4}

Now, create some files inside each folder.

If you want to find out which folder contains a file named amazon.

If you want to find out which folder contains a file named devops.

If you want to search in folder.

If flipkart is not there, we won’t be seeing any o/p.

→ Now, let’s create a folder inside another folder, and try to find a specific file or folder by name. By default, the find command returns both files and directories that match the search criteria.

This returns all the files and directories.

To Find Only a Directory: find . -type d -name zepto

To Find Only a File: find . -type f -name zepto

So far, all our searches have been based on the file or directory name.

Now, let’s learn how to search for files based on their permissions:

To give full permissions (read, write, and execute for user, group, and others) to a file named linux inside folder1, use:
chmod 777 folder1/linux
chmod 777 folder4/zepto

To get full permission files: find . -perm 777 -type f

find . -type f ! -perm 777 → Used to find all files in the current directory (and its subdirectories) whose permissions are not set to 777.

Now, let’s learn how to search for files based on their owners:

First, let's add some users.

Now, if you want to find files owned by the user tcs: find . -user tcs -type f

To list all files and folders that belong to the group “IT”: find . -group IT

find / -user <username> is used to find all files owned by a specific user starting from the root (/) directory.

find / -group groupname is used to find all files owned by a specific group starting from the root (/) directory.

find /proc/ -name filename is used to search for a file named filename within the /proc directory.

Locate Command: The locate command quickly searches for files and directories by name using a prebuilt database. If you use locate netflix command, it doesn’t work directly.

Note: The command locate netflix may not work directly if the locate database hasn't been updated or if the file doesn't exist.

→ Before using the locate command, you need to update the file database. The locate command searches through this database, which contains a list of files present on the system.

Update the file database: updatedb
Search for files related to “devops”: locate devops

🔍 locate vs find: Which is Better?

Use locate when:
→ You want fast results.
→ You’re searching for a file by name.
→ The file was present during the last database update.
→ command: locate filename
locate uses a prebuilt index (mlocate.db), so it’s extremely fast but not always up to date.
→ To update the database manually: sudo updatedb

✅ Use find when:
→ You need real-time, up-to-date search results
→ You want to search by name, size, type, permissions, owner, or modified time
→ You need to perform actions on the results (like delete, move, etc.)
→ command: find /path -name "filename"
find searches the actual filesystem, so it’s slower but accurate and more powerful.

\=: The locate command searches the entire system using a pre-built database.
\=: The find command searches only in the folder (path) you specify.

Note: If you see error message -bash: locate: command not found means that the locate command is not installed on your Linux system. The locate command is used to quickly find files by name on your system and is provided by the package mlocate.

Step 1: Install the locate Command, on CentOS/RHEL/Fedora:
command: sudo yum install mlocate
Step 2: Update the Database
command: sudo updatedb
Step 3: Locate Files
command: locate “filename”

FYI: The command cat /etc/os-release is used to display information about the Linux operating system you're currently running. It's a quick way to check your distro name, version, and other metadata.
Here you can see ID_LIKE “fedora”

🔚 Conclusion:

Searching for files and text efficiently is a vital skill for anyone working in a Linux environment. By understanding and using commands like find, locate, grep, and others, you can quickly navigate your system, troubleshoot issues, and manage data more effectively. Whether you're searching by name, content, or attributes, these tools give you the flexibility and power to streamline your workflow. With regular use, you'll find these commands becoming an essential part of your daily Linux toolkit.

Keep practicing, keep exploring, and may your searches always return exactly what you need—no wild goose chases required. If you have questions or thoughts, feel free to leave a comment or share this with fellow tech enthusiasts! Thanks for reading🙏

Until next time, keep calm and grep -i on. 🐧❤️

0
Subscribe to my newsletter

Read articles from Chandana Reddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chandana Reddy
Chandana Reddy

Hi, I'm Chandana—a curious soul navigating the world through study, reflection, and shared wisdom. My journey is rooted in self-education: exploring new ideas, skills, and perspectives that empower personal growth. I believe that learning isn’t limited to classrooms—it’s an everyday practice that transforms who we are and how we connect. Through writing, conversations, and community-building, I share insights and tools that help others learn with purpose and passion. I’m not just a student of life—I’m a contributor to its knowledge ecosystem.