ESC
Bash / Terminal Commands

Navigation

Command Options Example Output Description
ls -la, -lh, -R ls -la /etc file list List directory contents; -l long format, -a hidden files, -h human sizes
cd -, ~, .. cd ~/projects changes dir Change directory; cd - goes back, cd ~ goes home
pwd pwd /home/user Print working directory (full path)
tree -L n, -a tree -L 2 tree view Display directory tree; -L limits depth (may need install)
find -name -type -mtime find . -name '*.log' file paths Search for files/dirs matching criteria
locate -i locate nginx.conf file paths Fast file search using database index; run updatedb first

File Operations

Command Options Example Output Description
cp -r, -i, -u cp -r src/ dst/ copies file Copy files; -r for directories, -i prompt before overwrite
mv -i, -n mv old.txt new.txt moves/renames Move or rename files; -i prompt before overwrite
rm -r, -f, -i rm -rf dist/ deleted Remove files/dirs; -r recursive, -f force — use carefully
mkdir -p mkdir -p a/b/c new dir Create directory; -p creates parent dirs as needed
touch touch index.html empty file Create empty file or update file timestamp
ln -s ln -s /etc/nginx nginx symlink Create hard link; -s for symbolic link

Viewing Files

Command Options Example Output Description
cat -n, -A cat -n file.txt file content Print file; -n adds line numbers, -A shows non-printable chars
head -n N head -n 20 log.txt first N lines Show first lines of file (default 10)
tail -n N, -f tail -f app.log last N lines Show last lines; -f follows new content in real time
less /search, q less large.log paged view Page through file; / to search, q to quit, space to page
wc -l, -w, -c wc -l file.txt count Word count; -l lines, -w words, -c bytes/chars

Searching & Text Processing

Command Options Example Output Description
grep -r -i -n -v -E grep -rn 'error' logs/ matched lines Search text patterns; -r recursive, -i case-insensitive, -n line numbers
grep -v grep -v 'debug' app.log non-matching Invert match — show lines NOT containing pattern
grep -E grep -E 'warn|error' log matched lines Extended regex — use | for OR, + for one-or-more
awk '{print $N}' awk '{print $1}' f column value Pattern-action text processing; $1 first field, $NF last field
sed -i 's/a/b/g' sed -i 's/foo/bar/g' f modified text Stream editor; s/search/replace/g substitutes globally
sort -n -r -k N sort -n numbers.txt sorted output Sort lines; -n numeric, -r reverse, -k sort by column N
uniq -c, -d sort f | uniq -c unique lines Remove duplicate adjacent lines; usually piped after sort

Permissions

Command Options Example Output Description
chmod 755, 644, +x chmod +x script.sh perms changed Change file permissions; 755=rwxr-xr-x, 644=rw-r--r--
chown -R user:group chown -R www-data:www-data /var/www owner changed Change file owner and group; -R recursive
ls -la ls -la /var/www perms listing Show permissions, owner, group, size for all files
umask 022, 027 umask 022 default perms Set default permission mask for new files (022 → 644 files)
sudo -u user sudo systemctl restart nginx elevated cmd Run command as superuser; -u to run as different user

Process Management

Command Options Example Output Description
ps aux, -ef ps aux | grep nginx process list List processes; aux=all users, -ef=full format
top q, k, M top live view Interactive process monitor; q quit, k kill, M sort by memory
htop F3, F9 htop live view Enhanced top with colors; F3 search, F9 kill (may need install)
kill -9, -15 kill -9 1234 signal sent Send signal to PID; -15 SIGTERM (graceful), -9 SIGKILL (force)
killall -9, -u killall node signal sent Kill all processes by name
jobs -l jobs -l job list List background/stopped jobs in current shell
bg / fg %N fg %1 resumed bg sends job to background, fg brings to foreground

Network

Command Options Example Output Description
curl -o -L -X -H -d curl -O https://example.com/f response Transfer data via HTTP/FTP; -L follow redirects, -o save to file
wget -O -r -q wget -q https://example.com saved file Download files; -O rename output, -r recursive, -q quiet
ping -c N ping -c 4 google.com RTT ms Test network connectivity; -c limits packet count
ssh -i -p -L ssh user@host -p 2222 shell Secure shell; -i key file, -p port, -L local port forward
scp -r -P -i scp file user@host:~/ copied Secure copy over SSH; -r recursive, -P port (capital)
netstat -tuln netstat -tuln port list Show network connections; -t TCP, -u UDP, -l listening, -n numeric
ss -tuln ss -tuln port list Modern replacement for netstat; faster and more detailed

Archives & Compression

Command Options Example Output Description
tar -czf -czf out.tar.gz tar -czf arc.tar.gz dir/ archive Create gzip compressed tarball; c=create, z=gzip, f=filename
tar -xzf -xzf file.tar.gz tar -xzf arc.tar.gz -C /tmp extracted Extract gzip tarball; x=extract, -C destination directory
tar -tf tar -tf archive.tar.gz file list List contents of tarball without extracting
zip -r, -9 zip -r out.zip dir/ zip file Create zip archive; -r recursive, -9 max compression
unzip -d, -l unzip archive.zip -d out/ extracted Extract zip; -d destination, -l list contents without extracting
gzip / gunzip -k, -d gzip -k file.txt compressed gzip single file; -k keep original, gunzip to decompress

Environment & Shell

Command Options Example Output Description
env env | grep PATH var=value Print all environment variables; pipe with grep to filter
export export NODE_ENV=production var set Set environment variable for current session and child processes
echo $VAR echo $HOME value Print variable value; use ${VAR} for disambiguation in strings
which which node path Show full path of command executable
alias alias ll='ls -la' shortcut Create command shortcut; add to ~/.bashrc for persistence
source source ~/.bashrc reloaded Execute file in current shell context (also: . ~/.bashrc)
history !N, !! history | tail -20 cmd list Show command history; !! repeats last, !N runs Nth command

Git Quick Reference

Command Options Example Output Description
git init git init my-project repo created Initialize new Git repository in directory
git clone --depth 1 git clone url repo cloned Clone remote repository; --depth 1 for shallow clone
git status -s git status -s changes Show working tree status; -s short format
git add -A, -p git add -p staged Stage changes; -A all, -p interactive patch mode
git commit -m, --amend git commit -m 'msg' committed Commit staged changes; --amend modify last commit
git push -u origin git push -u origin main pushed Push to remote; -u sets upstream tracking branch
git pull --rebase git pull --rebase synced Fetch and integrate remote changes; --rebase instead of merge
git log --oneline --graph git log --oneline -10 history Show commit history; --oneline compact, --graph visual branch
git stash pop, list git stash pop restored Temporarily save uncommitted work; pop to restore
git diff --staged git diff --staged diff Show changes; --staged shows staged but uncommitted changes

Frequently Asked Questions

sh (Bourne Shell) is the original Unix shell, standardized by POSIX. bash (Bourne Again Shell) is its successor and superset, adding arrays, extended test syntax [[]], arithmetic expansion $(( )), history, tab completion, and more. On most Linux systems /bin/sh is a symlink to bash or dash (a POSIX-only lightweight shell). Scripts using #!/bin/sh should stick to POSIX features for portability; scripts using #!/bin/bash can use bash-specific extensions.

Three steps: (1) Add a shebang line as the very first line: #!/bin/bash. (2) Make it executable with chmod +x script.sh. (3) Run it with ./script.sh (the ./ is needed because the current directory is not in $PATH by default). To run it from anywhere, move it to a directory in your PATH: mv script.sh ~/bin/ and ensure ~/bin is in your PATH.

Bash has three standard streams: stdin (0), stdout (1), stderr (2). Redirect stdout to file: command > file.txt (overwrite) or command >> file.txt (append). Redirect stderr: command 2> errors.txt. Redirect both: command > output.txt 2>&1 or command &> output.txt. Discard output: command > /dev/null 2>&1. Pipe stdout to next command: command1 | command2.

sudo (superuser do) runs a command with root (administrator) privileges. Use it only when required — for example installing system packages, editing files in /etc, or managing services. Avoid running development tools, scripts you did not write, or anything that modifies your home directory as root. Never use sudo with npm install -g unless necessary, as it can corrupt package ownership. The principle of least privilege: use the minimum permissions needed.