|
| 1 | +# Ubuntu Grep Best Practices Cheatsheet |
| 2 | + |
| 3 | +## Basic Syntax |
| 4 | +```bash |
| 5 | +grep [options] pattern [file...] |
| 6 | +``` |
| 7 | + |
| 8 | +## Essential Options |
| 9 | +| Option | Description | Example | |
| 10 | +|--------|-------------|---------| |
| 11 | +| `-i` | Case insensitive search | `grep -i "error" log.txt` | |
| 12 | +| `-r` or `-R` | Recursive search | `grep -r "TODO" /path/to/dir` | |
| 13 | +| `-n` | Show line numbers | `grep -n "pattern" file.txt` | |
| 14 | +| `-w` | Match whole words only | `grep -w "class" *.java` | |
| 15 | +| `-l` | Show only filenames | `grep -l "pattern" *.txt` | |
| 16 | +| `-c` | Count matches | `grep -c "error" log.txt` | |
| 17 | +| `-v` | Invert match (show non-matching) | `grep -v "^#" config.txt` | |
| 18 | +| `-E` | Extended regex support | `grep -E "word1|word2" file.txt` | |
| 19 | + |
| 20 | +## Best Practices |
| 21 | + |
| 22 | +### 1. Using Context Options |
| 23 | +```bash |
| 24 | +# Show 3 lines before match |
| 25 | +grep -B 3 "error" log.txt |
| 26 | + |
| 27 | +# Show 3 lines after match |
| 28 | +grep -A 3 "error" log.txt |
| 29 | + |
| 30 | +# Show 3 lines before and after |
| 31 | +grep -C 3 "error" log.txt |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. Color Output |
| 35 | +```bash |
| 36 | +# Enable color output |
| 37 | +grep --color=auto "pattern" file.txt |
| 38 | + |
| 39 | +# Add to ~/.bashrc for permanent color: |
| 40 | +export GREP_OPTIONS='--color=auto' |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Excluding Directories |
| 44 | +```bash |
| 45 | +# Exclude specific directories |
| 46 | +grep -r "pattern" --exclude-dir={.git,node_modules,vendor} . |
| 47 | + |
| 48 | +# Exclude by pattern |
| 49 | +grep -r "pattern" --exclude-dir=".*" . |
| 50 | +``` |
| 51 | + |
| 52 | +### 4. File Type Filtering |
| 53 | +```bash |
| 54 | +# Search only specific file types |
| 55 | +grep -r "pattern" --include="*.php" . |
| 56 | + |
| 57 | +# Multiple file types |
| 58 | +grep -r "pattern" --include="*.{js,jsx,ts,tsx}" . |
| 59 | +``` |
| 60 | + |
| 61 | +### 5. Advanced Pattern Matching |
| 62 | + |
| 63 | +#### Regular Expressions |
| 64 | +```bash |
| 65 | +# Match start of line |
| 66 | +grep "^start" file.txt |
| 67 | + |
| 68 | +# Match end of line |
| 69 | +grep "end$" file.txt |
| 70 | + |
| 71 | +# Match any single character |
| 72 | +grep "h.t" file.txt |
| 73 | + |
| 74 | +# Match zero or more occurrences |
| 75 | +grep "wo*rd" file.txt |
| 76 | + |
| 77 | +# Match specific number of occurrences |
| 78 | +grep "o\{2\}" file.txt |
| 79 | +``` |
| 80 | + |
| 81 | +#### Extended Regular Expressions (-E or egrep) |
| 82 | +```bash |
| 83 | +# Match either pattern |
| 84 | +grep -E "pattern1|pattern2" file.txt |
| 85 | + |
| 86 | +# Match one or more occurrences |
| 87 | +grep -E "o+" file.txt |
| 88 | + |
| 89 | +# Match zero or one occurrence |
| 90 | +grep -E "colou?r" file.txt |
| 91 | +``` |
| 92 | + |
| 93 | +### 6. Performance Optimization |
| 94 | + |
| 95 | +#### For Large Files |
| 96 | +```bash |
| 97 | +# Stop after first match |
| 98 | +grep -m 1 "pattern" large_file.txt |
| 99 | + |
| 100 | +# Use binary grep for faster search |
| 101 | +grep -a "pattern" binary_file |
| 102 | + |
| 103 | +# Count matches without printing |
| 104 | +grep -c "pattern" large_file.txt |
| 105 | +``` |
| 106 | + |
| 107 | +#### For Multiple Files |
| 108 | +```bash |
| 109 | +# Process files in parallel |
| 110 | +parallel "grep 'pattern' {}" ::: *.txt |
| 111 | + |
| 112 | +# Use find with grep |
| 113 | +find . -type f -exec grep "pattern" {} + |
| 114 | +``` |
| 115 | + |
| 116 | +### 7. Error Handling |
| 117 | +```bash |
| 118 | +# Suppress error messages |
| 119 | +grep -s "pattern" /path/to/files/* |
| 120 | + |
| 121 | +# Show only error messages |
| 122 | +grep -q "pattern" file.txt 2>&1 >/dev/null |
| 123 | +``` |
| 124 | + |
| 125 | +### 8. Common Use Cases |
| 126 | + |
| 127 | +#### Finding IP Addresses |
| 128 | +```bash |
| 129 | +grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" log.txt |
| 130 | +``` |
| 131 | + |
| 132 | +#### Finding Email Addresses |
| 133 | +```bash |
| 134 | +grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" file.txt |
| 135 | +``` |
| 136 | + |
| 137 | +#### Finding URLs |
| 138 | +```bash |
| 139 | +grep -E "https?://[A-Za-z0-9./]+" file.txt |
| 140 | +``` |
| 141 | + |
| 142 | +## Tips for Daily Use |
| 143 | + |
| 144 | +1. **Use ripgrep (rg) for Better Performance** |
| 145 | + - Install: `sudo apt install ripgrep` |
| 146 | + - Usage: `rg "pattern"` |
| 147 | + |
| 148 | +2. **Create Aliases for Common Operations** |
| 149 | + ```bash |
| 150 | + # Add to ~/.bashrc |
| 151 | + alias grepjs='grep -r --include="*.js"' |
| 152 | + alias grepphp='grep -r --include="*.php"' |
| 153 | + alias greperr='grep -i -r "error|warning|critical"' |
| 154 | + ``` |
| 155 | + |
| 156 | +3. **Use Process Substitution** |
| 157 | + ```bash |
| 158 | + # Search in command output |
| 159 | + grep "pattern" <(command) |
| 160 | + ``` |
| 161 | + |
| 162 | +4. **Combine with Other Commands** |
| 163 | + ```bash |
| 164 | + # Find and grep |
| 165 | + find . -type f -name "*.log" -exec grep "error" {} + |
| 166 | + |
| 167 | + # Pipe with grep |
| 168 | + ps aux | grep "[n]ginx" |
| 169 | + ``` |
| 170 | + |
| 171 | +## Common Pitfalls to Avoid |
| 172 | + |
| 173 | +1. Don't use grep without -r for directory searches |
| 174 | +2. Avoid using GREP_OPTIONS (deprecated) |
| 175 | +3. Remember to escape special characters in patterns |
| 176 | +4. Don't forget to use quotes around patterns with spaces |
| 177 | +5. Be careful with -R vs -r (symbolic links) |
| 178 | + |
| 179 | +## Debugging Tips |
| 180 | + |
| 181 | +1. Use `-v` to see what's being excluded |
| 182 | +2. Use `--debug` to see detailed matching information |
| 183 | +3. Use `-h` to suppress filenames in multi-file searches |
| 184 | +4. Add `-n` to show line numbers for easier reference |
0 commit comments