Linux CLI cheat sheet
Search text
- grep — print lines matching a pattern
- Files:
grep "users" /etc/group
- From another command:
ps -ef | grep "root"
Field processing
- awk — select and format fields
- First field in passwd:
awk -F":" '{ print $1 }' /etc/passwd
- Reorder ls columns:
ls -l | awk '{ print $9, $1 }'
Stream edits
- sed — find and replace or delete lines
- Replace text in a file:
sed 's/Unix/UNIX/g' pressRel
- Replace delimiters in a stream:
grep "usr" /etc/passwd | sed 's/:/ /g'
- Delete first line:
ls -1 /tmp | sed '1d'
Counts
- wc — count words, lines, or bytes
- Words in a file:
wc -w verona.txt
- Lines from a command:
who | wc -l
Head and tail
- head — first N lines
- First 7 lines:
head -n 7 /etc/motd
- tail — last N lines
- Last 5 lines:
tail -n 5 build.log
- Last line from a command:
du . | tail -n 1
Sort and unique
- sort — sort lines
- Sort a file:
sort inventory
- Sort command output:
who | sort
- uniq — remove adjacent duplicates (use after sort)
- Deduplicate a sorted file:
uniq sortedRecords
- Deduplicate who output:
who | sort | uniq
Archive and transfer
- tar over SSH — stream archive to a remote host
- Create on local, extract on remote dir:
tar -czf - * | ssh target_user@target_domain tar -xzf - -C target_dir/
Handy combinations
- Top 10 most common log entries:
sort access.log | uniq -c | sort -nr | head -n 10