Commands for displaying, searching, editing, and processing text files.
Displays file contents or concatenates multiple files.
| Option | Description |
|---|---|
| -n | Display line numbers |
| -b | Display line numbers (except for blank lines) |
| -s | Squeeze multiple blank lines into one |
| -A | Display control and special characters (tabs, line breaks, etc.) |
Examples:
cat file.txt - Display file contentscat file1.txt file2.txt - Concatenate and display multiple filescat file1.txt file2.txt > combined.txt - Concatenate multiple files and save to a new file
Searches for text patterns in files.
| Option | Description |
|---|---|
| -i | Case-insensitive search |
| -v | Display lines that don't match the pattern |
| -n | Display line numbers |
| -r, -R | Search recursively through directories |
| -l | Display only filenames containing matches |
| -c | Display count of matching lines |
| -E | Use extended regular expressions |
| -A n | Display n lines after the match |
| -B n | Display n lines before the match |
| -C n | Display n lines before and after the match |
Examples:
grep "pattern" file.txt - Search for pattern in filegrep -i "pattern" file.txt - Case-insensitive searchgrep -r "pattern" directory/ - Search recursively through all files in directorygrep -E "pattern1|pattern2" file.txt - Search for multiple patterns (OR)grep -v "^#" config.txt | grep -v "^$" - Exclude comment lines and blank lines (using pipe)ps aux | grep "[f]irefox" - Search for firefox processes excluding the grep process itselfgrep -A 2 -B 1 "ERROR" log.txt - Display error lines with contextgrep -o "([0-9]\{1,3\}\.){3}[0-9]\{1,3\}" file.txt - Extract only IP addressesfind . -name "*.log" -exec grep -l "ERROR" {} \; | xargs wc -l - Count lines in log files containing errorshistory | grep "git commit" | sed 's/^[ 0-9]*//' - Extract only git commit commands from history
Performs text transformations like substitution, deletion, and insertion.
| Option/Syntax | Description |
|---|---|
| -i | Edit files in-place (default is to output to stdout) |
| -e | Specify multiple commands |
| -n | Suppress automatic printing (print only specific patterns) |
| s/pattern/replacement/ | Substitute pattern |
| s/pattern/replacement/g | Substitute all occurrences of pattern (g means global) |
| d | Delete lines matching pattern |
| p | Print lines matching pattern (usually used with -n) |
Examples:
sed 's/old/new/' file.txt - Replace first occurrence of "old" with "new" on each linesed 's/old/new/g' file.txt - Replace all occurrences of "old" with "new"sed -i 's/old/new/g' file.txt - Edit file in-placesed '/pattern/d' file.txt - Delete lines matching patternsed -n '/pattern/p' file.txt - Print only lines matching patternsed -i.bak 's/old/new/g' file.txt - Create backup before editing filesed '1,5s/old/new/g' file.txt - Replace "old" with "new" only in lines 1-5sed '/start/,/end/d' file.txt - Delete range of lines from "start" to "end" patternsed 's/[0-9]\{3\}-[0-9]\{4\}/XXX-XXXX/g' file.txt - Mask phone numberscat file.txt | sed 's/^[ \t]*//' - Remove leading whitespace from each lineecho "hello world" | sed 's/\b\(.\)/\u\1/g' - Capitalize first letter of each wordsed -e 's/old/new/g' -e 's/foo/bar/g' file.txt - Perform multiple substitutions at oncegrep "ERROR" log.txt | sed 's/.*ERROR: \(.*\)/\1/' - Extract only error messages
A programming language for text processing and data extraction.
| Syntax/Pattern | Description |
|---|---|
| '{print $1}' | Print first field (column) of each line |
| '{print $1, $3}' | Print first and third fields |
| -F | Specify field separator (default is whitespace) |
| '/pattern/ {action}' | Perform action on lines matching pattern |
| NR | Current line number |
| NF | Number of fields in current line |
Examples:
awk '{print $1}' file.txt - Print first field of each lineawk -F, '{print $1, $3}' file.csv - Print first and third fields from CSV fileawk '/pattern/ {print $0}' file.txt - Print lines matching patternawk '{sum += $1} END {print sum}' file.txt - Calculate and print sum of first fieldawk '{count[$1]++} END {for (word in count) print word, count[word]}' file.txt - Count occurrences of each wordawk -F, '{if ($3 > 100) print $1, $2, $3}' data.csv - Print lines where third field is greater than 100awk 'NR % 2 == 0' file.txt - Print only even-numbered linesawk 'length($0) > 80' file.txt - Print lines longer than 80 charactersawk 'BEGIN {FS=","; OFS="\t"} {print $1, $2, $3}' file.csv - Convert CSV to TSVps aux | awk '$3 > 10.0 {print $2, $3, $11}' - Print processes using more than 10% CPUcat /etc/passwd | awk -F: '{print "User: " $1 ", Home: " $6}' - Format user informationls -l | awk '{sum += $5} END {printf "Total size: %.2f MB\n", sum/1024/1024}' - Calculate total file size in MBgrep "ERROR" log.txt | awk -F: '{print $1}' | sort | uniq -c - Count errors by file
Display the beginning or end portions of files.
| Command/Option | Description |
|---|---|
| head file.txt | Display first 10 lines of file |
| head -n N file.txt | Display first N lines of file |
| tail file.txt | Display last 10 lines of file |
| tail -n N file.txt | Display last N lines of file |
| tail -f file.txt | Display last lines and follow file updates in real-time (useful for log monitoring) |
Examples:
head -n 5 file.txt - Display first 5 linestail -n 20 file.txt - Display last 20 linestail -f /var/log/syslog - Monitor system log in real-timehead -n 5 file.txt | tail -n 1 - Display only line 5tail -n +10 file.txt | head -n 5 - Display lines 10-14ls -l | head -n 5 - Display first 5 files in directoryfind . -type f -name "*.log" | head - Display first 10 log filesps aux | sort -nrk 3,3 | head -n 5 - Display top 5 CPU-consuming processestail -f log.txt | grep --color "ERROR" - Monitor log with highlighted errorstail -f /var/log/apache2/access.log /var/log/apache2/error.log - Monitor multiple log files simultaneouslyfind . -type f -mtime -1 | xargs wc -l | head - Display line counts of 10 most recently modified files
Sorts lines of text files.
| Option | Description |
|---|---|
| -r | Sort in reverse (descending) order |
| -n | Sort numerically |
| -k N | Sort by Nth field (column) |
| -t | Specify field separator |
| -u | Remove duplicates (output unique lines only) |
| -f | Case-insensitive sort |
Examples:
sort file.txt - Sort file alphabeticallysort -r file.txt - Sort in reverse ordersort -n numbers.txt - Sort numericallysort -k 2 -t, file.csv - Sort CSV file by second fieldsort -k 2n -t: file.txt - Sort numerically by second field (delimiter is :)sort -k 3,3 -k 1,1 file.txt - Sort by third field, then by first fieldsort -u file.txt - Sort and remove duplicatessort -h sizes.txt - Sort by human-readable sizes (K, M, G)du -h | sort -hr - Sort directory sizes in descending orderps aux | sort -nrk 3,3 | head -n 10 - Display top 10 CPU-consuming processescat /etc/passwd | sort -t: -k 3 -n - Sort users by UIDfind . -type f -name "*.log" | xargs ls -l | sort -k 5 -nr - Sort log files by size
Processes duplicate lines (typically used with sort).
| Option | Description |
|---|---|
| -c | Display count of occurrences |
| -d | Display only duplicate lines |
| -u | Display only unique lines (no duplicates) |
| -i | Case-insensitive comparison |
Examples:
sort file.txt | uniq - Sort and remove duplicate linessort file.txt | uniq -c - Count occurrences of each linesort file.txt | uniq -d - Display only duplicate linessort file.txt | uniq -u - Display only unique linessort file.txt | uniq -c | sort -nr - Display lines by frequency (most frequent first)cat access.log | cut -d' ' -f1 | sort | uniq -c | sort -nr - Count IP address access frequencycat file.txt | tr '[:upper:]' '[:lower:]' | sort | uniq - Case-insensitive duplicate removalhistory | cut -c8- | sort | uniq -c | sort -nr | head -n 10 - Display top 10 most used commandsfind . -type f -name "*.txt" | xargs cat | sort | uniq -c - Count occurrences of lines across all text filesgrep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' access.log | sort | uniq -c | sort -nr - Count IP address frequency in log file
Counts lines, words, and bytes in files.
| Option | Description |
|---|---|
| -l | Count lines only |
| -w | Count words only |
| -c | Count bytes only |
| -m | Count characters only |
Examples:
wc file.txt - Display line, word, and byte countswc -l file.txt - Display line count onlywc -w file.txt - Display word count onlywc -c file.txt - Display byte count onlywc -m file.txt - Display character count (correctly counts multibyte characters)find . -name "*.py" | xargs wc -l - Count lines in all Python filesfind . -name "*.py" | xargs wc -l | sort -nr - Sort Python files by line countgrep -v "^#" script.sh | grep -v "^$" | wc -l - Count non-comment, non-blank lines in scriptcat file.txt | tr -s ' ' '\n' | sort | uniq | wc -l - Count unique words in filels -la | wc -l - Count files in directory (including header line)for file in *.txt; do echo -n "$file: "; wc -l < "$file"; done - Display line count for each text filefind . -type f -exec cat {} \; | wc -l - Count total lines in all files in directory