Linux CLI HowTo 14 ๐ง How to pipelines and grep
1. How to Chain Commands Using Pipes
Steps:
- Start with your first command (e.g.,
lsto list files) - Add the pipe symbol
| - Add your second command that will receive the input
- Continue chaining as needed
Example: List all files, then filter for only text files:
ls | grep "\.txt$"
2. How to Redirect Command Output to a File
Steps:
- Use the
>operator instead of| - Specify the filename after the
> - The output will be saved to that file
Example: Save directory listing to a file:
ls > my_files.txt
3. How to Filter Text with Grep
Steps:
- Use
grepfollowed by your search pattern - Add the filename or pipe data to grep
- Optionally add flags for special behavior
Example: Find lines containing “error” in a log file:
grep "error" /var/log/syslog
4. How to Combine Grep with Pipes
Steps:
- Run a command that produces output (like
ls) - Pipe that output to grep using
| - Add grep options as needed
Example: Show only files ending with .pdf from directory listing:
ls | grep "\.pdf$"
5. How to Count Lines Using Pipeline
Steps:
- Use
cator another command to display content - Pipe that output to
wc -l - The result will show line count
Example: Count lines in a file:
cat document.txt | wc -l
6. How to Search for Patterns with Grep
Steps:
- Use
grepfollowed by pattern to search for - Add filename or pipe data as input
- Use regular expressions for advanced searching
Example: Find any word starting with “cat”:
grep "^cat" animals.txt
7. How to Search Case-Insensitive with Grep
Steps:
- Use
grep -ioption before your pattern - Add your search term
- Provide input source (file or pipe)
Example: Find “hello” regardless of case:
grep -i "hello" file.txt
8. How to Search Recursively with Grep
Steps:
- Use
grep -roption for recursive search - Add directory path to search in
- Specify pattern to find
Example: Find “password” in all files recursively:
grep -r "password" /home/user/
9. How to Show Only Matching Lines with Line Numbers
Steps:
- Use
grep -noption for line numbers - Add your search pattern
- Provide input file or pipe data
Example: Find and show line numbers of “config” in file:
grep -n "config" settings.ini
10. How to Count Matches with Grep
Steps:
- Use
grep -coption for counting - Add your pattern to search for
- Provide input source
Example: Count how many times “apple” appears in fruit list:
grep -c "apple" fruits.txt
11. How to Match Whole Words Only
Steps:
- Use
grep -woption - Add your word pattern
- Provide text source
Example: Find exact word “cat” not “catch”:
grep -w "cat" animals.txt
12. How to Search for Lines Starting with Pattern
Steps:
- Use
grep "^pattern"where ^ means start of line - Add your pattern after the caret
- Provide input file or pipe data
Example: Find all lines starting with “ERROR”:
grep "^ERROR" log.txt
13. How to Search for Lines Ending with Pattern
Steps:
- Use
grep "pattern$"where \$ means end of line - Add your pattern before the dollar sign
- Provide input source
Example: Find lines ending with “.txt”:
grep "\.txt$" files.txt
14. How to Search for Multiple Characters in One Position
Steps:
- Use square brackets
[abc]to match any of those characters - Add your pattern with brackets
- Provide input source
Example: Find lines with any vowel at start:
grep "^[aeiou]" words.txt
15. How to Search for Any Character Using Wildcards
Steps:
- Use
.in grep pattern to match any single character - Add your pattern structure
- Provide input source
Example: Find three-letter words starting with “b”:
grep "^b..$" words.txt
16. How to Combine Multiple Grep Options
Steps:
- Use multiple flags together like
grep -i -n - Add your pattern and input source
- Combine different behaviors as needed
Example: Case-insensitive search with line numbers:
grep -in "data" file.txt
17. How to Filter Files by Extension Using Pipeline
Steps:
- Use
lscommand to list files - Pipe output to
grepwith pattern matching extension - Show only specific file types
Example: List only Python files:
ls | grep "\.py$"
18. How to Process Large Files Efficiently
Steps:
- Use pipeline to process data in chunks
- Combine commands like
head,tail, orgrepwith pipes - For processing large files, use
headto limit input
Example: Check first 100 lines for pattern:
cat largefile.txt | head -100 | grep "target"
19. How to Use Grep with Regular Expressions in Pipeline
Steps:
- Create pipeline from command output
- Pipe to grep with regex patterns
- Apply advanced matching rules
Example: Find email addresses pattern:
cat contacts.txt | grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
20. How to Chain Complex Commands with Multiple Pipes
Steps:
- Start with initial command (e.g.,
find) - Add first pipe and filter command
- Add second pipe and another command
- Continue until final result
Example: Find .txt files, show their content, count lines:
find /home -name "*.txt" | xargs cat | wc -l
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!