Efficiently finding and processing data is a core skill for any Linux user. This is the third part of our Linux command series. We focus on commands such as grep, find, and locate. These commands are essential for system administrators, DevOps, developers, and anyone working with large datasets or log files.
Commands Table
Command | Description |
---|---|
grep "word" <file> | Search for a word in a file |
grep -n "word" <file> | Search and show line numbers |
grep -c "word" <file> | Count occurrences of a word |
grep -i "word" <file> | Case-insensitive search |
grep "word" file1 file2 | Search in multiple files |
grep -l "word" * | List files containing a word |
grep -e "pattern1" -e "pattern2" <file> | Search multiple patterns |
find . -name "filename" | Find file in current directory |
find /path/ -name "filename" | Find file in specific directory |
find . -type f -name "*.txt" | Find files with .txt extension |
find . -type d -name "foldername" | Find folder by name |
find . -perm 777 | Find files with 777 permissions |
find . ! -perm 777 | Find files not having 777 permissions |
find . -user <username> | Find files owned by user |
find . -group <groupname> | Find files owned by group |
find . -empty | Find empty files/folders |
find . -mtime 10 | Find files modified 10 days ago |
find . -size 1K | Find files of size 1 KB |
find . -size +50M | Find files larger than 50 MB |
locate filename | Find file using system database |
Here is the detailed examples for above commands
Folder Structure and its content
demo/ ├── file1.txt ├── file2.txt (777 - Full Permissions) ├── empty.txt ├── one_kb.bin (Size 1 KB) ├── 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
# this file is empty
folder1/nested.txt
# this file is empty
one_kb.bin
# binary data size of 1024 bytes
1. grep - Text Search
# Search for a word in a file 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 a match grep -l "word" * ------------------ file1.txt file2.txt
# Search multiple patterns grep -e "Hello" -e "Another" file1.txt ------------------ Hello World Another word line
grep
searches text for patterns:
-i
: Ignore case-n
: Show line numbers-c
: Count matches-l
: List only filenames-e
: Specify multiple patterns
2. find - File Search
# Find file in current directory find . -name "file1.txt" ------------------ ./file1.txt
# Find file in specific directory find ./folder1 -name "nested.txt" ------------------ ./folder1/nested.txt
# Find all .txt files find . -type f -name "*.txt" ------------------ ./file1.txt ./file2.txt ./empty.txt ./folder1/nested.txt
# Find a folder by name find . -type d -name "folder1" ------------------ ./folder1
# Find files with 777 permissions find . -perm 777 ------------------ ./file2.txt
# Find files not having 777 permissions find . ! -perm 777 ------------------ ./file1.txt ./empty.txt ./folder1/nested.txt
# Find files owned by a user find . -user <username> ------------------ ./file1.txt ./file2.txt ./empty.txt ./folder1/nested.txt
# Find files owned by a group find . -group <groupname> ------------------ ./file1.txt ./file2.txt ./empty.txt ./folder1/nested.txt
# Find empty files and folders find . -empty ------------------ ./empty.txt ./folder1/nested.txt
# Find files modified 10 days ago find . -mtime 10 ------------------ No Result
# Find files of size 1 KB find . -size 1k ------------------ ./one_kb.bin
# Find files larger than 50 MB find . -size +50M ------------------ No Result
find
locates files and directories by name, type, permissions, ownership, size, and modification time. Very powerful for system cleanup and search.
3. locate - Database-backed File Search
# Locate a file by name sudo updatedb locate file1.txt ------------------ /home/user/demo/file1.txt