Pattern Matching with grep
The grep utility searches for patterns within files or directory trees.
grep "error" system.log
grep -i "failed" app.log # Case-insensitive matching
grep -w "function" script.sh # Whole word matches only
grep -e "warning" -e "alert" debug.txt # Multiple pattern search
grep -n "TODO" src/ # Display line numbers
grep -v "test" results.txt # Inverted matching
grep -r "config" ./ # Recursive directory search
grep -l "main" *.c # List filenames with matches
grep -E "^[A-Z]" names.txt # Extended regular expressions
File Discovery with find
Locate files based on name, type, timestamp, and ownership criteria.
find /var/www -name "*.html"
find /etc -type f # Regular files only
find /home -type d # Directories only
find /usr -type l # Symbolic links only
# Time-based searches
find /tmp -amin -30 # Accessed within 30 minutes
find /config -cmin -60 # Changed within 1 hour
find /data -mmin -120 # Modified within 2 hours
find /logs -mtime -7 # Modified within 7 days
# Ownership-based searches
find /projects -user developer
find /shared -group engineering
Stream Editing with sed
Perform non-interactive text transformations on files and streams.
# Operation modes
sed -e 's/old/new/' input.txt # Execute and display output
sed -n 'p' data.csv # Suppress automatic printing
sed -i 's/foo/bar/' config.ini # In-place file editing
sed -i.bak 's/pattern/replace/' file.txt # Backup before editing
sed -f script.sed source.txt # Commands from external file
# Editing commands
sed -i '3i\Inserted line' doc.md # Insert before line 3
sed -i '5a\Appended line' notes.txt # Append after line 5
sed -i '2d' temp.log # Delete line 2
sed -i '4c\Replaced content' report.txt # Replace line 4
sed -i 's/error/fixed/g' app.log # Global substitution
sed -n '10p' largefile.txt # Print specific line
Word Counting with wc
Generate statistics on lines, words, bytes, and characters.
wc report.txt
# Output format: lines words bytes filename
wc -l source.py # Line count only
wc -w essay.md # Word count only
wc -c data.bin # Byte count only
wc -m unicode.txt # Character count only
Archive Management with tar
Create and extract archievs with optional compression.
tar -cvf archive.tar directory/ # Create verbose archive
tar -xvf archive.tar # Extract verbose archive
tar -czvf backup.tar.gz /path # Create gzipped archive
tar -xzvf backup.tar.gz # Extract gzipped archive
tar -tf archive.tar # List archive contents
# Note: The -f flag must precede the filename
Viewing File Extremities
Display the beginning or end of files with head and tail commands.
head -20 logfile.log # First 20 lines
tail -50 error.log # Last 50 lines
tail -f application.log # Real-time file monitoring