CLI:Basics – Searching for Files and Text in Linux
Welcome back to the CLI:Basics series! In this post, we’re going to cover how to search for files, patterns, and specific content on your Linux system using core terminal tools. Whether you’re trying to find a lost file or narrow down log entries, these commands are essential.
We’ll go over the following:
wc
– Word count and data statslocate
– Quickly find filesgrep
– Search inside filesfind
– Precision file/directory search
Let’s get started.
📏 wc
– Word Count Utility
wc -l text.txt
The wc
command gives you stats about a file:
-l
→ number of lines-w
→ number of words-c
→ number of bytes-m
→ number of characters-L
→ length of the longest line
You’ll use wc
a lot when piping results and counting lines of output from other commands.
🔎 locate
– Find Files Quickly
Before using locate
, make sure the plocate
package is installed and updated:
sudo apt install plocate
sudo updatedb
To search for files:
locate bashrc
Useful options:
-c
→ Count results instead of showing them-i
→ Case-insensitive search--limit N
→ Show only the first N results
⚠️ Note:
locate
relies on a database and may not include files added since your lastupdatedb
.
🔍 grep
– Search Inside Files
grep ltl /etc/group
This searches for a pattern (ltl
) in the /etc/group
file. You can combine it with wc -l
using a pipe to count how many lines match:
grep -c ltl /etc/group
Other useful options:
-v
→ Invert match (exclude)-w
→ Match whole words only-i
→ Case-insensitive search
Example: Count all groups that don’t include the current user
grep -v ltl /etc/group | wc -l
This is a great example of a pipeline, where the output of one command (grep
) is passed as input to another (wc
).
🧠 Understanding Pipelines (Command Stacks)
command1 | command2
This setup is called a pipeline or “command stack.” It uses the pipe |
symbol to redirect output from one command as input into the next. You’ll use this everywhere in Linux once you start combining commands for more powerful operations.
🔎 find
– Search With Precision
Unlike locate
, find
scans the live filesystem. It’s slower but more accurate.
Basic example:
find . -name text.txt
This searches for a file named text.txt
in the current directory and all subdirectories.
Case-insensitive search:
find . -iname text.txt
Search a specific directory:
find /home -name text.txt
Find directories named log
system-wide (requires sudo):
sudo find / -type d -name log
Find all MP3 files in current directory and below:
find . -type f -name "*.mp3"
You can search by file type:
-type f
→ file-type d
→ directory
⚠️ Running
find /
withoutsudo
will generate many “permission denied” messages. Usesudo
when necessary to get cleaner, complete results.
Final Thoughts
These tools—wc
, locate
, grep
, and find
—give you powerful ways to search, count, and narrow results from the Linux terminal. They form the backbone of more complex workflows you’ll encounter later.
👀 Coming soon: More file manipulation commands, and eventually deeper topics like I/O redirection and scripting.
If you found this post helpful, check out the YouTube playlist and subscribe to catch the next video in the CLI:Basics series!