Day 06


Topics to cover

01 Learning Grep

02 Learning Redirection commands

03 Learning Cut Commands

04 Learning Sed

05 Learning Find

06 Learning Uniq

🔍 grep Commands Cheat Sheet

  • 1️⃣ Basic Search: Search for the word “tree” in a file
grep "tree" /etc/tree.txt
  • 2️⃣ Case-insensitive Search: Search for “tree”, ignoring case
grep -i "tree" /etc/tree.txt
  • 3️⃣ Search with Line Numbers: Search for “tree” and display line numbers
grep -n "tree" /etc/tree.txt
  • 4️⃣ Count Matches: Count the number of lines that match “tree”
grep -c "tree" /etc/tree.txt
  • 5️⃣ Invert Match: Display lines that do NOT contain “tree”
grep -v "tree" /etc/tree.txt
  • 6️⃣ Show Matching Part Only: Show only the part of the line that matches “tree”
grep -o "tree" /etc/tree.txt
  • 7️⃣ Extended Regex: Search for “ram” or “ravan” using extended regex
grep -E "ram|ravan" /etc/ram.txt
  • 8️⃣ Whole Word Match: Match the word “tree” exactly (not part of another word)
grep -w "tree" /etc/tree.txt
  • 9️⃣ Display File Name Only: Show filenames that contain the word “tree”
grep -l "tree" *.txt
  • 🔟 Display File Name Only (No Match): Show filenames that do NOT contain the word “tree”
grep -L "tree" *.txt
  • 1️⃣1️⃣ Recursive Search: Search for “tree” in all files recursively within a directory
grep -r "tree" /path/to/dir/
  • 1️⃣2️⃣ Recursive Search (Case-insensitive): Search for “tree” recursively, ignoring case
grep -ir "tree" /path/to/dir/
  • 1️⃣3️⃣ Search for Multiple Patterns: Search for either “tree” or “forest” in a file
grep -E "tree|forest" /etc/trees.txt
  • 1️⃣4️⃣ Suppress Errors: Suppress error messages (e.g., for files that can’t be read)
grep "tree" * 2>/dev/null
  • 1️⃣5️⃣ Show Lines with Multiple Matches: Show lines that contain both “tree” and “forest”
grep "tree" /etc/trees.txt | grep "forest"
  • 1️⃣6️⃣ Use with Pipes: Filter results from another command, such as ps or dmesg
ps aux | grep "nginx"                # Find running processes related to nginx
dmesg | grep -i "error"              # Look for "error" in system logs
  • 1️⃣7️⃣ Use with xargs: Search for “ERROR” in all .log files listed in files.txt
cat files.txt | xargs grep "ERROR"

🔴 grep Redirection Commands

  • 1️⃣ Redirect Standard Output to a File (Overwrite)
grep "tree" /etc/tree.txt > output.txt
  • 2️⃣ Append Standard Output to a File
grep "tree" /etc/tree.txt >> output.txt
  • 3️⃣ Redirect Errors to a File (Overwrite)
grep "tree" /etc/tree.txt 2> error_log.txt
  • 4️⃣ Append Errors to a File
grep "tree" /etc/tree.txt 2>> error_log.txt
  • 5️⃣ Redirect Both Standard Output and Errors to the Same File
grep "tree" /etc/tree.txt > output.txt 2>&1
  • 6️⃣ Append Both Standard Output and Errors to the Same File
grep "tree" /etc/tree.txt >> output.txt 2>&1
  • 7️⃣ Suppress Errors and Redirect Output to a File
grep "tree" /etc/tree.txt > output.txt 2>/dev/null
  • 8️⃣ Redirect Both Output and Errors to null (Suppress All Output)
grep "tree" /etc/tree.txt > /dev/null 2>&1
  • 9️⃣ Redirect Both Errors and Output to a Separate File (If You Need Both)
grep "tree" /etc/tree.txt 1> output.txt 2> error_log.txt
  • 🔟 Redirect Only Errors to null (Suppress Errors)
grep "tree" /etc/tree.txt 2>/dev/null

✂️ cut Commands Cheat Sheet

  • 1️⃣ Cut by delimiter: Extract the first column from a comma-separated file
cut -d ',' -f 1 /path/to/file.csv
  • 2️⃣ Cut by delimiter and show multiple columns: Extract the first and second columns
cut -d ',' -f 1,2 /path/to/file.csv
  • 3️⃣ Cut by character position: Extract the first 5 characters from each line
cut -c 1-5 /path/to/file.txt
  • 4️⃣ Cut by byte position: Extract the first 5 bytes from each line
cut -b 1-5 /path/to/file.txt
  • 5️⃣ Remove the first column: Skip the first column and print the rest (tab-delimited)
cut -f 2- /path/to/file.txt
  • 6️⃣ Show specific columns by field delimiter: Extract the third and fourth columns from a pipe-separated file
cut -d '|' -f 3,4 /path/to/file.txt
  • 7️⃣ Cut with a specific field delimiter and show the 2nd to 4th columns
cut -d ':' -f 2-4 /etc/passwd
  • 8️⃣ Cut by character positions for the first 3 characters
cut -c 1-3 /path/to/file.txt
  • 9️⃣ Cut by delimiter and show the last field (if no delimiter specified, the whole line is considered one field)
cut -d ':' -f 7 /etc/passwd
  • 🔟 Cut with input from a pipeline: Extract the second column from the output of the ps command
ps aux | cut -d ' ' -f 2
  • 1️⃣1️⃣ Cut by character range and input from pipeline: Extract the first 10 characters of each line in a file
cat /path/to/file.txt | cut -c 1-10
  • 1️⃣2️⃣ Remove the last column: Use cut to skip the last column of a tab-separated file
cut -f 1- --complement /path/to/file.txt

📝 sed Commands Cheat Sheet

  • 1️⃣ Basic Substitution: Replace “old” with “new” in a file
sed 's/old/new/' /path/to/file.txt
  • 2️⃣ Global Substitution: Replace all occurrences of “old” with “new” in a file
sed 's/old/new/g' /path/to/file.txt
  • 3️⃣ In-place Substitution: Replace “old” with “new” directly in the file (without creating a backup)
sed -i 's/old/new/g' /path/to/file.txt
  • 4️⃣ Substitution with Backup: Replace “old” with “new” and create a backup of the original file
sed -i.bak 's/old/new/g' /path/to/file.txt
  • 5️⃣ Delete Lines: Delete lines that match a pattern
sed '/pattern/d' /path/to/file.txt
  • 6️⃣ Print Lines: Print only the lines that match a pattern
sed -n '/pattern/p' /path/to/file.txt
  • 7️⃣ Replace Only on a Specific Line: Replace “old” with “new” only on line 3
sed '3s/old/new/' /path/to/file.txt
  • 8️⃣ Delete a Range of Lines: Delete lines 3 to 5 from a file
sed '3,5d' /path/to/file.txt
  • 9️⃣ Replace Multiple Lines: Replace lines from 3 to 5 with a new string
sed '3,5c\New content' /path/to/file.txt
  • 🔟 Insert Text Before a Line: Insert “new line” before line 2
sed '2i\new line' /path/to/file.txt
  • 1️⃣1️⃣ Append Text After a Line: Append “new line” after line 2
sed '2a\new line' /path/to/file.txt

🔍 find Commands Cheat Sheet

  • 1️⃣ Basic Search: Find all files with the .txt extension in the current directory
find . -name "*.txt"
  • 2️⃣ Search in a Specific Directory: Find all .log files in the /var/log directory
find /var/log -name "*.log"
  • 3️⃣ Search for Files Modified in the Last N Days: Find files modified in the last 7 days
find . -type f -mtime -7
  • 4️⃣ Search for Files Larger than N Bytes: Find files larger than 100MB
find . -type f -size +100M
  • 5️⃣ Search for Empty Files: Find all empty files
find . -type f -empty
  • 6️⃣ Find Files by Permissions: Find all files with 777 permissions
find . -type f -perm 0777
  • 7️⃣ Find Files Owned by a Specific User: Find all files owned by user “john”
find . -type f -user john
  • 8️⃣ Execute a Command on Found Files: Find all .log files and delete them
find . -name "*.log" -exec rm -f {} \;
  • 9️⃣ Search for Directories Only: Find all directories in the current directory
find . -type d
  • 🔟 Search for Files Modified in the Last N Minutes: Find files modified in the last 30 minutes
find . -type f -mmin -30
  • 1️⃣1️⃣ Search for Files by Group: Find all files that belong to group “staff”
find . -type f -group staff
  • 1️⃣2️⃣ Find Files by Name with Case Insensitivity: Find .jpg files, case-insensitive
find . -iname "*.jpg"
  • 1️⃣3️⃣ Limit Search Depth: Find files only in the top level of directories (depth 1)
find . -maxdepth 1 -type f
  • 1️⃣4️⃣ Search for Files Modified After a Specific Date: Find files modified after January 1, 2020
find . -type f -newermt "2020-01-01"

🦄 uniq Commands Cheat Sheet

  • 1️⃣ Remove Duplicate Lines: Remove consecutive duplicate lines in a file
uniq /path/to/file.txt
  • 2️⃣ Count Occurrences: Show the number of occurrences of each line in the file
uniq -c /path/to/file.txt
  • 3️⃣ Display Only Duplicates: Show only the lines that are repeated (duplicates)
uniq -d /path/to/file.txt
  • 4️⃣ Display Lines that Are Not Duplicated: Show lines that are not repeated in the file
uniq -u /path/to/file.txt
  • 5️⃣ Skip N Fields: Skip the first field when checking for duplicates (useful for space-separated fields)
uniq -f 1 /path/to/file.txt
  • 6️⃣ Ignore Case When Comparing: Remove duplicates, ignoring case
uniq -i /path/to/file.txt
  • 7️⃣ Compare N Characters: Compare the first N characters of each line for duplicates
uniq -w 5 /path/to/file.txt
  • 8️⃣ Use a Separator: Use a custom separator when displaying duplicates
uniq -s 2 /path/to/file.txt
  • 9️⃣ Redirect Output to a New File: Remove duplicates and store the result in a new file
uniq /path/to/file.txt > /path/to/output.txt
  • 🔟 Count Duplicates in Input: Pipe input to uniq and count occurrences
echo -e "apple\napple\nbanana" | uniq -c
  • 1️⃣1️⃣ Remove Duplicates from Sorted Data: Sort data and then remove duplicates
sort /path/to/file.txt | uniq
  • 1️⃣2️⃣ Use with Pipe: Count unique words in the output of a cat command
cat /path/to/file.txt | sort | uniq -c