Bash / Linux Commands Cheat Sheet

Terminal command reference with file operations, pipes, networking, permissions, and shell scripting. Essential for Linux and macOS.

85 entries 10 sections

Navigation

Command Description Example
Print current working directory pwd → /home/user
Change directory cd /var/log
Go up one directory cd ../../
Go to home directory cd ~
Go to previous directory cd -
List directory contents ls -la
List all files with details ls -la /etc/
List with human-readable sizes ls -lhS (sort by size)
Display directory tree structure tree -L 2

Files

Command Description Example
Create empty file or update timestamp touch index.html
Copy file or directory cp -r folder/ backup/
Move or rename file mv old.txt new.txt
Remove file rm -rf temp/
Create directory mkdir -p path/to/dir
Remove empty directory rmdir old_folder
Create symbolic link ln -s /usr/bin/python3 /usr/bin/python
Display file contents cat config.yaml
View file with scrolling less /var/log/syslog
Show first N lines head -n 20 data.csv
Show last N lines tail -n 50 error.log
Follow file output (live) tail -f /var/log/nginx/access.log
Count lines, words, bytes wc -l data.csv (count lines)
Compare two files diff old.conf new.conf
Determine file type file mystery.bin

Search

Command Description Example
Search for pattern in file grep -rn 'TODO' src/
Case-insensitive search grep -i 'error' log.txt
Recursive search in directory grep -r 'import' src/
Invert match (exclude) grep -v '#' config.conf
Count matching lines grep -c 'ERROR' app.log
Find files by name find . -name '*.tsx'
Find files or directories find /tmp -type f -mtime +7
Execute command on results find . -name '*.log' -exec rm {} \;
Show path of command which python3
Fast file search (uses index) locate nginx.conf
Fast code search (silver searcher / ripgrep) rg 'useState' --type tsx

Text

Command Description Example
Find and replace text sed -i 's/http/https/g' urls.txt
Extract columns from text awk -F',' '{print $1, $3}' data.csv
Sort lines alphabetically sort -n numbers.txt (numeric sort)
Remove consecutive duplicates sort file.txt | uniq -c
Extract fields by delimiter cut -d':' -f1 /etc/passwd
Translate/replace characters echo 'hello' | tr 'a-z' 'A-Z'
Write to file AND stdout echo 'log' | tee -a output.log
Build command from stdin find . -name '*.tmp' | xargs rm

Permissions

Command Description Example
Change file permissions chmod +x script.sh
Change file ownership chown www-data:www-data /var/www
Run as superuser sudo apt update
Switch to another user su - root

Processes

Command Description Example
List all running processes ps aux | grep node
Interactive process viewer htop
Terminate process by PID kill -9 12345
Kill all processes by name killall node
Background / foreground a job Ctrl+Z then bg
Run command immune to hangup nohup node server.js &
List background jobs jobs -l
Run command in background npm run build &

Networking

Command Description Example
Transfer data from/to URL curl -X POST -d '{"name":"Jo"}' api.com
Download file from URL wget https://example.com/file.zip
Test network connectivity ping -c 4 google.com
Secure shell connection ssh deploy@192.168.1.10
Secure copy over SSH scp file.zip user@server:/tmp/
Show listening ports netstat -tlnp | grep 3000
Socket statistics (modern netstat) ss -tlnp
Show network interfaces ip addr show

System

Command Description Example
Show disk space usage df -h
Show directory size du -sh node_modules/
Show memory usage free -h
Show system information uname -r (kernel version)
Show how long system has been running uptime
Show current username whoami → root
Show command history history | grep ssh
Create command shortcut alias ll='ls -la'
Print environment variable echo $PATH
Set environment variable export NODE_ENV=production
Show all environment variables env | grep PATH

Pipes

Command Description Example
Pipe output to next command cat log.txt | grep ERROR | wc -l
Redirect output to file (overwrite) echo 'hello' > output.txt
Append output to file echo 'line' >> log.txt
Redirect errors to file npm build 2> errors.log
Redirect both stdout and stderr npm run build &> build.log
Use file as input mysql db < dump.sql

Compression

Command Description Example
Create gzipped archive tar -czf backup.tar.gz /home/user
Extract gzipped archive tar -xzf backup.tar.gz -C /tmp/
Create zip archive zip -r project.zip src/
Extract zip archive unzip project.zip -d output/
Compress/decompress single file gzip large_file.log

Frequently asked questions

What's the difference between Bash and Zsh?

Bash is the default shell on most Linux distros. Zsh (default on macOS since Catalina) adds features like better tab completion, spelling correction, shared history, and plugins (oh-my-zsh). Most Bash commands work in Zsh. For scripts, stick with #!/bin/bash for portability.

How do I make a script executable?

Add #!/bin/bash as the first line (shebang), then run chmod +x script.sh. Now you can run it with ./script.sh. Without the shebang, you'd need to run it as bash script.sh explicitly.

What does chmod 755 mean?

Permissions are in three groups: owner, group, others. Each digit is a sum: 4=read, 2=write, 1=execute. So 755 means: owner=7 (rwx), group=5 (r-x), others=5 (r-x). 644 is common for files (rw-r--r--), 755 for directories and scripts.

How do I run a command in the background?

Append & to the command: long_task &. Use Ctrl+Z to suspend a running command, then bg to resume it in background. Use nohup command & if you want it to survive terminal closure. Check with jobs or ps.

What's the difference between > and >>?

> overwrites the file (creates if it doesn't exist). >> appends to the file. Use > for fresh output and >> for logging. For stderr, use 2> (overwrite) or 2>> (append). &> redirects both stdout and stderr.

How do I find and kill a process using a port?

Use lsof -i :3000 or ss -tlnp | grep 3000 to find the PID. Then kill PID or kill -9 PID (force). One-liner: kill $(lsof -t -i:3000). On macOS, you might need sudo.

Go from reference to real skills

Cheat sheets are great for quick lookups. Our in-depth courses take you from the fundamentals to professional-level mastery.

Browse all courses