Skip to main content

The Linux Commandline

The Linux command line (shell) is the primary interface for interacting with Linux systems — essential for administration, scripting, and DevOps workflows.

Shell Basics

The default shell on most modern systems is Bash (Bourne Again Shell). Zsh is also widely used (macOS default since Catalina).

echo $SHELL        # Show active shell
cat /etc/shells # List available shells
chsh -s /bin/zsh # Change default shell

pwd               # Print Working Directory
ls -lah # Directory listing: long format, all files, human-readable
cd /etc/nginx # Absolute path
cd ../.. # Two levels up
cd ~ # Home directory
cd - # Previous directory (toggle)

Key Directories (FHS)

PathContent
/etcSystem-wide configuration files
/var/logLog files
/procVirtual FS — kernel & process info
/sysVirtual FS — devices & kernel subsystems
/usr/binUser binaries
/usr/local/binManually installed software
/tmpTemporary files (cleared on reboot)

Files & Directories

touch file.txt            # Create file / update timestamp
mkdir -p /opt/app/config # Create directory including parent paths
cp -r src/ dst/ # Copy recursively
mv old.txt new.txt # Rename / move
rm -rf /tmp/testdir # Recursive delete (no confirmation!)
ln -s /opt/app/bin/tool /usr/local/bin/tool # Create symlink

Reading & Searching File Contents

cat /etc/hosts              # Output entire file
less /var/log/syslog # Page through content (q = quit)
tail -f /var/log/nginx/access.log # Live follow (e.g. logs)
head -n 20 file.txt # First 20 lines
grep -rn "error" /var/log/ # Recursive search with line numbers
grep -E "WARN|ERROR" app.log # Extended regex

Pipes & Redirections

# Write / append stdout to file
echo "config=true" > config.txt
echo "debug=false" >> config.txt

# Redirect stderr separately
command 2>/dev/null # Suppress errors
command > out.txt 2>&1 # Stdout + stderr into one file

# Pipes — pass output as input
ps aux | grep nginx | grep -v grep
cat /etc/passwd | awk -F: '{print $1}' | sort

Processes & System

ps aux                    # All running processes
top / htop # Interactive process monitor
kill -9 <PID> # Force kill process (SIGKILL)
pkill nginx # Kill processes by name
jobs # Background jobs in current session
nohup ./script.sh & # Run process in background, immune to SIGHUP

# System resources
df -h # Disk usage
du -sh /var/log/* # Directory sizes
free -h # RAM usage
uptime # Load average

Permissions

ls -l file.txt            # Show permissions: -rwxr-xr--
chmod 755 script.sh # Owner: rwx, Group: r-x, Others: r-x
chmod u+x,g-w file.txt # Set symbolically
chown www-data:www-data /var/www/html # Set owner:group

Octal Reference

OctalBinaryMeaning
7111rwx
6110rw-
5101r-x
4100r--
0000---

Useful Shortcuts (Bash)

ShortcutAction
Ctrl+CInterrupt process (SIGINT)
Ctrl+ZSuspend process
Ctrl+RSearch command history (reverse)
Ctrl+LClear terminal (like clear)
!!Repeat last command
!$Last argument of previous command
Alt+.Insert last argument