Bash cheatsheet
Navigation
| Command | Description |
|---|---|
pwd | Print working directory |
ls -la | List all files with details |
cd ~ | Go to home directory |
cd - | Go to previous directory |
File Operations
| Command | Description |
|---|---|
cp -r src/ dest/ | Copy directory recursively |
mv file newname | Move or rename file |
rm -rf dir/ | Remove directory (careful!) |
touch file.txt | Create empty file |
mkdir -p a/b/c | Create nested directories |
Viewing & Searching
| Command | Description |
|---|---|
cat file | Print file contents |
less file | Scroll through file |
head -n 20 file | First 20 lines |
tail -f file | Follow file in real time |
grep -rn "pattern" . | Recursive search with line numbers |
find . -name "*.txt" | Find files by name |
Pipes & Redirection
| Command | Description |
|---|---|
cmd1 | cmd2 | Pipe output to next command |
cmd > file | Redirect output to file (overwrite) |
cmd >> file | Append output to file |
cmd 2>&1 | Redirect stderr to stdout |
cmd &> file | Redirect both stdout and stderr |
Process Management
| Command | Description |
|---|---|
ps aux | List all processes |
kill -9 PID | Force kill a process |
top / htop | Live process monitor |
cmd & | Run command in background |
jobs | List background jobs |
ctrl+z then bg | Suspend then background a job |
Permissions
| Command | Description |
|---|---|
chmod +x file | Make file executable |
chmod 755 file | Set specific permissions |
chown user:group file | Change ownership |
Archives
| Command | Description |
|---|---|
tar -czf out.tar.gz dir/ | Create gzip archive |
tar -xzf file.tar.gz | Extract gzip archive |
unzip file.zip | Extract zip |
Networking
| Command | Description |
|---|---|
curl -O URL | Download file |
wget URL | Download file |
ssh user@host | SSH into remote machine |
scp file user@host:path | Secure copy to remote |
Text Processing
| Command | Description |
|---|---|
sed 's/old/new/g' file | Find and replace |
awk '{print $1}' file | Print first column |
sort file | Sort lines |
uniq -c | Count unique lines |
wc -l file | Count lines |
cut -d',' -f1 | Cut by delimiter |
History & Shortcuts
| Command | Description |
|---|---|
!! | Repeat last command |
!$ | Last argument of previous command |
ctrl+r | Reverse search history |
history | grep cmd | Search command history |
alias ll='ls -la' | Create command alias |
Variables & Scripting
VAR="value" # Set variable
echo $VAR # Use variable
$() # Command substitution: echo $(date)
[[ -f file ]] # Test if file exists
for i in *.txt; do echo $i; done # Loop over files
Pro tips
- Use
man <command>for full documentation on anything ctrl+a/ctrl+ejumps to start/end of linectrl+lclears the screen (faster thanclear)- Prefix a command with a space to keep it out of history
Practice scenarios
Hands-on Bash scenarios on live Linux VMs: bash