Linux CLI
Navigation
pwd # print working directory
ls -la # list all, long format (permissions, size, dates)
cd /path/to/dir # change directory
cd ~ # home directory
cd - # previous directory
find . -name "*.log" # find files by name pattern
find . -type f -mtime -1 # files modified in last day
which python3 # locate an executable in PATH
File Operations
cp file.txt backup.txt # copy
cp -r src/ dest/ # copy directory recursively
mv old.txt new.txt # move / rename
rm file.txt # delete
rm -rf dir/ # delete directory recursively, no prompt — dangerous
mkdir -p a/b/c # create nested dirs
touch file.txt # create empty file / update timestamp
cat file.txt # print file contents
less file.txt # paginated view (q to quit)
head -n 20 file.txt # first 20 lines
tail -f app.log # follow a growing file (logs)
Permissions
ls -l file.txt # -rw-r--r-- 1 user group size date file.txt
chmod 644 file.txt # rw-r--r-- (owner rw, group/other r)
chmod +x script.sh # add execute for all
chmod u+x,g-w script.sh # symbolic: user +execute, group -write
chown user:group file.txt # change owner and group
sudo <command> # run as superuser
Permission triplet order: owner, group, other. Each is r (4) + w (2) + x (1) summed, e.g. 755 = rwxr-xr-x.
Text Processing
grep "error" app.log # search lines matching a pattern
grep -rn "TODO" src/ # recursive, with line numbers
grep -v "debug" app.log # invert match (exclude)
sed 's/foo/bar/g' file.txt # find/replace, all occurrences
sed -i 's/foo/bar/g' file.txt # edit in place
awk '{print $1, $3}' file.txt # print columns 1 and 3
awk -F',' '{print $2}' data.csv # custom field separator
sort file.txt | uniq -c | sort -rn # count unique lines, most frequent first
wc -l file.txt # line count
cut -d',' -f2 data.csv # extract field 2, comma-delimited
Process Management
ps aux # list all running processes
top # live process monitor (q to quit)
htop # nicer interactive version (if installed)
kill <pid> # send SIGTERM (graceful stop)
kill -9 <pid> # send SIGKILL (force stop)
pkill -f "node server.js" # kill by matching command
command & # run in background
jobs # list background jobs in this shell
fg %1 # bring job 1 to foreground
nohup command & # keep running after terminal closes
Networking Basics
curl https://api.example.com/data # HTTP request
curl -X POST -d '{"a":1}' -H "Content-Type: application/json" url
wget https://example.com/file.zip # download a file
ping example.com # test connectivity
dig example.com # DNS lookup
netstat -tulpn # listening ports (or `ss -tulpn`)
lsof -i :3000 # what's using port 3000
Package Management
# Debian/Ubuntu
sudo apt update && sudo apt install <pkg>
sudo apt remove <pkg>
# Fedora/RHEL
sudo dnf install <pkg>
# macOS (Homebrew)
brew install <pkg>
brew list
brew upgrade
Shell Scripting Basics
#!/usr/bin/env bash
set -euo pipefail # exit on error, unset var, or pipe failure — safer scripts
name="$1"
if [ -z "$name" ]; then
echo "usage: $0 <name>" >&2
exit 1
fi
for f in *.txt; do
echo "processing $f"
done
if [ -f "$name" ]; then
echo "$name exists"
fi
"$1", "$2" are positional args; "$@" is all args; $? is the last command’s exit code; always quote variable expansions to avoid word-splitting bugs.
Environment Variables
export API_KEY="secret" # set for this shell and child processes
echo $API_KEY # read
env # list all
unset API_KEY # remove
echo $PATH # search path for executables
Persist across sessions via ~/.bashrc / ~/.zshrc (interactive shells) or ~/.profile (login shells). Never commit real secrets to a shell rc file tracked in git.
SSH Basics
ssh user@host # connect
ssh -i ~/.ssh/id_ed25519 user@host # connect with a specific key
ssh-keygen -t ed25519 -C "you@example.com" # generate a new keypair
ssh-copy-id user@host # install your public key on a remote host
scp file.txt user@host:/remote/path # copy a file over SSH
scp -r dir/ user@host:/remote/path # copy a directory
~/.ssh/config lets you alias hosts (Host myserver / HostName ... / User ... / IdentityFile ...) so ssh myserver just works.
Disk Usage
df -h # disk free, human-readable, per filesystem
du -sh * # size of each item in cwd, human-readable
du -sh dir/ # total size of a directory
ncdu # interactive disk usage explorer (if installed)