Linux grep, find, and locate Commands

Linux grep, find, and locate Commands

Searching and filtering data efficiently is a core skill for every Linux user. This is Part 3 of our Linux commands series, where we focus on powerful search utilities like grep, find, and locate. These commands are widely used by system administrators, DevOps engineers, developers, and anyone working with large log files or file systems.

Commonly Used Commands

CommandDescription
grep "word" <file>Search for a word in a file
grep -n "word" <file>Search and display line numbers
grep -c "word" <file>Count the number of matches
grep -i "word" <file>Case-insensitive search
grep "word" file1 file2Search across multiple files
grep -l "word" *List filenames containing the word
grep -e "pattern1" -e "pattern2" <file>Search for multiple patterns
find . -name "filename"Find a file in the current directory
find /path -name "filename"Find a file in a specific directory
find . -type f -name "*.txt"Find all .txt files
find . -type d -name "folder"Find a directory by name
find . -perm 777Find files with 777 permissions
find . ! -perm 777Find files without 777 permissions
find . -user <username>Find files owned by a user
find . -group <groupname>Find files owned by a group
find . -emptyFind empty files and directories
find . -mtime 10Find files modified 10 days ago
find . -size 1KFind files of size 1 KB
find . -size +50MFind files larger than 50 MB
locate filenameFind files using the system database

Detailed Examples

Sample Folder Structure

demo/
├── file1.txt
├── file2.txt         (777 permissions)
├── empty.txt
├── one_kb.bin        (1 KB file)
├── folder1/
│   └── nested.txt
└── folder2/

file1.txt

Hello World
This is a test
Word appears here
Another word line

file2.txt

word is case sensitive
Another Test line
HELLO WORD

empty.txt

# empty file

folder1/nested.txt

# empty file

one_kb.bin

# binary file of size 1024 bytes

1. grep – Text Searching

# Search for a word
grep "word" file1.txt

Word appears here
Another word line
# Show line numbers
grep -n "word" file1.txt

3:Word appears here
4:Another word line
# Count matches
grep -c "word" file1.txt

2
# Case-insensitive search
grep -i "word" file2.txt

word is case sensitive
HELLO WORD
# Search in multiple files
grep "word" file1.txt file2.txt

file1.txt:Word appears here
file1.txt:Another word line
file2.txt:word is case sensitive
# List files containing the word
grep -l "word" *

file1.txt
file2.txt
# Search multiple patterns
grep -e "Hello" -e "Another" file1.txt

Hello World
Another word line

The grep command searches text for patterns. Common options include:

  • -i – Ignore case
  • -n – Show line numbers
  • -c – Count matches
  • -l – Display filenames only
  • -e – Match multiple patterns

2. find – File and Directory Search

# Find a file
find . -name "file1.txt"

./file1.txt
# Find files in a specific directory
find ./folder1 -name "nested.txt"

./folder1/nested.txt
# Find all text files
find . -type f -name "*.txt"

./file1.txt
./file2.txt
./empty.txt
./folder1/nested.txt
# Find directories
find . -type d -name "folder1"

./folder1
# Find files with 777 permissions
find . -perm 777

./file2.txt
# Find empty files and directories
find . -empty

./empty.txt
./folder1/nested.txt

The find command is extremely powerful and allows you to search files based on name, type, permissions, ownership, size, and modification time.

3. locate – Fast Database Search

# Update database and locate file
sudo updatedb
locate file1.txt

/home/user/demo/file1.txt

Be the first to comment

Leave a Reply

Your email address will not be published.


*