Summary
Essential command-line tricks, shortcuts, and productivity enhancers for working efficiently in the terminal. These techniques help reduce typing, improve workflow speed, and leverage the full power of the shell.
Key concepts
- History manipulation: Quickly recall and reuse previous commands
- Keyboard shortcuts: Navigate and edit command lines efficiently
- Piping and redirection: Chain commands and control output flow
- Process management: Control running processes and background jobs
- Text processing: Manipulate text streams with built-in tools
Details
History manipulation
Command history navigation:
!!- Repeat last command!$- Last argument of previous command!*- All arguments of previous command!n- Execute command number n from history!string- Execute most recent command starting with “string”^old^new- Replace “old” with “new” in previous command
History search:
Ctrl+R- Reverse search through command historyCtrl+Ragain - Cycle through matcheshistory | grep keyword- Search command history
Keyboard shortcuts
Navigation:
Ctrl+A- Jump to beginning of lineCtrl+E- Jump to end of lineCtrl+U- Clear line before cursorCtrl+K- Clear line after cursorAlt+B- Move back one wordAlt+F- Move forward one word
Editing:
Ctrl+W- Delete word before cursorAlt+D- Delete word after cursorCtrl+Y- Paste previously deleted textCtrl+L- Clear screen (same asclear)Ctrl+C- Cancel current commandCtrl+Z- Suspend current process
Piping and redirection
Output control:
command > file.txt- Redirect output, overwrite filecommand >> file.txt- Redirect output, append to filecommand 2> error.log- Redirect stderr onlycommand &> all.log- Redirect both stdout and stderrcommand 2>&1- Redirect stderr to stdout
Piping:
cmd1 | cmd2- Pipe output of cmd1 to input of cmd2cmd1 | tee file.txt- Show output and save to filecmd1 && cmd2- Run cmd2 only if cmd1 succeedscmd1 || cmd2- Run cmd2 only if cmd1 failscmd1; cmd2- Run both commands sequentially
Process management
Background jobs:
command &- Run command in backgroundjobs- List background jobsfg %n- Bring job n to foregroundbg %n- Resume job n in backgrounddisown %n- Detach job from current shell
Process control:
ps aux | grep process_name- Find process by namepgrep process_name- Get PID by process namepkill process_name- Kill process by namekill -9 PID- Force kill processtoporhtop- Interactive process viewer
Text processing
Quick text manipulation:
grep pattern file- Search for pattern in filesed 's/old/new/g' file- Replace text in fileawk '{print $1}' file- Print first columncut -d',' -f1 file.csv- Extract first field from CSVsort file | uniq- Sort and remove duplicateswc -l file- Count lines in file
Examples
Navigate to previous directory:
cd -Create directory and enter it:
mkdir new-project && cd $_Download file and show progress:
wget -c URL # or
curl -O URLFind and replace in multiple files:
grep -rl "old_text" . | xargs sed -i 's/old_text/new_text/g'Monitor log file in real-time:
tail -f /var/log/syslogDisk usage of directories, sorted:
du -h --max-depth=1 | sort -hrFind large files:
find / -type f -size +100M 2>/dev/nullExtract, transform, load pattern:
cat data.txt | grep "ERROR" | awk '{print $3}' | sort | uniq -cQuick backup of file before editing:
cp config.yml{,.backup}
# Creates config.yml.backupRun command with timeout:
timeout 10s commandResources
- The Linux Command Line (book)
- ExplainShell - Explain shell commands
- ShellCheck - Script analysis tool
- tldr pages - Simplified man pages