Cheatsheet - command line
Cheatsheet - Command line
The Art of Command line
This guide is for both beginners and experienced users, focusing on breadth, specificity, and brevity. Every tip is essential in some situation or significantly saves time. Written for Linux, it includes sections for macOS and Windows. The focus is on interactive Bash, though many tips apply to other shells and scripting. It includes both standard Unix commands and those requiring special package installs.
Basics
- Bash Fundamentals: Learn basic Bash by typing
man bashand skimming it. Bash is powerful and always available. - Text Editors: Learn at least one text-based editor well, such as
nanofor simplicity orvimfor power users. - Documentation: Use
manfor official documentation andaproposto find man pages. Know about Bash built-ins and usehelpandhelp -dfor help. - Shell Features: Learn about redirection (
>,>>,<), pipes (|), and file glob expansion (*,?,[ ]). - Job Management: Be familiar with Bash job management (
&,ctrl-z,ctrl-c,jobs,fg,bg,kill). - SSH: Know
sshand passwordless authentication viassh-agentandssh-add. - File Management:
ls,less,head,tail,ln,chown,chmod,du,df,mount,fdisk,mkfs,lsblk. - Network Management:
iporifconfig,dig,traceroute,route. - Version Control: Use a version control system like
git. - Regular Expressions: Know regular expressions and
grepoptions (-i,-o,-v,-A,-B,-C). - Package Management: Use
apt-get,yum,dnf, orpacmanto install packages andpipfor Python-based tools.
Everyday Use
- Command Line Navigation:
- Tab for command completion.
- ctrl-r for command history search.
- ctrl-w to delete the last word.
- ctrl-u to delete from the cursor to the start of the line.
- alt-b and alt-f to move by word.
- ctrl-a to move to the start of the line.
- ctrl-e to the end.
- ctrl-k to kill to the end of the line.
- ctrl-l to clear the screen.
- Editing Commands: For editing long commands, use ctrl-x ctrl-e to open the current command in an editor.
- Command History: Use
historyto see recent commands and!nto execute again. - Process Management:
xargsfor powerful command-line processing.pstree -pfor process tree display.pgrepandpkillto find or signal processes by name.nohupordisownfor background processes to run forever.netstat -lntporss -platto see listening processes.
- Shell Customization:
aliasto create shortcuts for commands and save them in~/.bashrc.~/.bash_profilefor environment variables and login commands.- Synchronize configuration files with Git.
- Scripting Best Practices:
- Handle whitespace in variables and filenames carefully.
- Use
set -xfor debugging output. - Use strict modes (
set -e,set -u,set -o pipefail) for error handling.
- Advanced Shell Features:
- Use subshells for temporary directory changes.
- Use variable expansion, brace expansion, and process substitution.
- Session Management: Use
screenortmuxfor screen multiplexing and session persistence. - SSH Optimization: Optimize SSH configuration for better performance and security.
Processing Files and Data
- File Finding: Use
findandlocateto find files. - Advanced Searching: Use
ack,ag, orrgfor advanced searching. - Document Conversion: Use
pandocfor document conversion andxmlstarletfor XML. - Structured Data: Use
jqfor JSON andcsvkitfor CSV files. - Text Manipulation: Use
sortanduniqfor text file manipulation. - Data Munging: Use
awkandsedfor simple data munging. - File Copying: Use
rsyncfor fast and versatile file copying. - Progress Monitoring: Use
pvordd status=progressfor monitoring file processing. - Randomization: Use
shufto shuffle or select random lines from a file. - File Splitting: Use
splitandcsplitfor splitting files. - Encoding Conversion: Use
iconvoruconvfor text encoding conversion. - File Attributes/Permissions: Use
chattrfor file attributes andgetfacl/setfaclfor file permissions.
System Debugging
- Web Debugging: Use
curlandhttpiefor web debugging. - System Status:
top,htop,iostat, andiotopfor CPU and disk status.netstatandssfor network connection details.dstatandglancesfor system overviews.freeandvmstatfor memory status.
- Network Issues: Use
mtrfor network issues andncdufor disk usage. - Bandwidth Usage: Use
iftopornethogsfor bandwidth usage. - System Call Debugging: Use
straceandltracefor system call debugging. - Live Problem Debugging: Use
/procfor live problem debugging. - Historic Statistics: Use
sarfor historic system statistics. - Deeper Analysis: Use
stap,perf, andsysdigfor deeper system analyses.
One-liners
- Text File Operations: Use
sortanduniqfor set operations on text files. - JSON Processing: Use
jqfor JSON pretty-printing and comparison. - File Examination: Use
grepandheadto quickly examine file contents. - Summing Numbers: Use
awkfor summing numbers in a text file. - Recursive Listing: Use
findfor recursive file listing. - Tallying Values: Use
egrepandsortfor tallying values in a text file. - Continuous Monitoring: Use
watchto monitor changes continuously.
Obscure but Useful
- Arithmetic/Boolean:
expr - Macro Processing:
m4 - Repetitive Printing:
yes - Calendar:
cal - Running Commands:
env - Data Manipulation:
cut,paste,join - Text Formatting:
fmt,pr,fold - Tab/Space Conversion:
expand,unexpand - Line Numbering:
nl - Number Printing:
seq - Calculations:
bc - Integer Factorization:
factor - Encryption/Signing:
gpg - Network Debugging/Transfer:
nc,socat - Data Moving:
dd - File Type Identification:
file - Directory Display:
tree - File Info:
stat - Command Timing:
time - Command Execution Limits:
timeout - Semaphore Files:
lockfile - Log Management:
logrotate - Repeated Command Execution:
watch - File Change Monitoring:
when-changed - Reverse File Printing:
tac - File Comparison:
comm - Text Extraction:
strings - Character Translation:
tr - Text Encoding Conversion:
iconv,uconv - File Splitting:
split,csplit - Read/Write Same File:
sponge - Unit Conversions:
units - Password Generation:
apg - File Compression:
xz - Dynamic Library Info:
ldd - Symbol Extraction:
nm - Web Server Benchmarking:
ab,wrk - System Call Debugging:
strace - Network Debugging:
mtr - Concurrent Shell:
cssh - File Synchronization:
rsync - Packet Capture:
wireshark,tshark - Network Layer Grep:
ngrep - DNS Lookups:
host,dig - Process File Descriptors:
lsof - System Statistics:
dstat,glances - Disk Usage Stats:
iostat - CPU Usage Stats:
mpstat - Memory Usage Stats:
vmstat - Improved Top:
htop - Login History:
last - Logged-on Users:
w - User/Group Identity:
id - Historic System Stats:
sar - Network Utilization:
iftop,nethogs - Socket Statistics:
ss - Boot/System Error Messages:
dmesg - Kernel Parameters:
sysctl - Disk Manipulation:
hdparm - Block Device Listing:
lsblk - Hardware Info:
lshw,lscpu,lspci,lsusb,dmidecode - Kernel Module Info:
lsmod,modinfo - Fun:
fortune,ddate,sl
macOS only
- Package Management: Use
breworport. - Clipboard Operations: Use
pbcopyandpbpaste. - Terminal Configuration: Enable the Option key in Terminal for alt key functionality.
- Opening Files: Use
opento open files with desktop apps. - File Searching: Use
mdfindfor file searching andmdlsfor metadata listing. - BSD Unix Differences: Be aware of BSD Unix differences and use
sw_versfor release info.
Windows only
- Unix Tools: Use Cygwin, WSL, MinGW, or Cash for Unix tools.
- System Administration: Use
wmic. - Networking:
ping,ipconfig,tracert,netstat. - Windows Tasks: Use
Rundll32for various Windows tasks. - Command-line Window: Use
mintty. - Opening Files: Use
cygstartto open files. - Windows Registry: Use
regtoolfor Windows registry access. - Path Conversion: Use
cygpathfor path conversion.