Linux Commands
Master the Command Line Interface
What is Linux?
Linux is an open-source Unix-like operating system kernel created by Linus Torvalds in 1991. The command-line interface (CLI) is the primary way to interact with Linux systems, especially servers. Commands are small programs that perform specific tasks - you can combine them using pipes and redirects to build powerful workflows. The shell (bash, zsh, etc.) interprets your commands and communicates with the kernel to execute them.
Think of Linux as a universal remote for your computer
Instead of clicking through menus and windows, you type precise commands that tell the computer exactly what to do. Like a universal remote controls any TV, Linux commands work across servers, containers, cloud instances, and your laptop. Once you learn the commands, you can control any Linux system in the world the same way.
Key Features
Everything is a File
Devices, processes, and system info are all represented as files you can read and manipulate.
Powerful Pipelines
Chain commands together with pipes (|) to process data in sophisticated ways.
Process Control
Run, monitor, schedule, and manage any process with fine-grained control.
Remote Access
Manage servers anywhere via SSH. Same commands work locally or remotely.
When to Use
- Managing servers, containers, and cloud instances
- Automating repetitive tasks with shell scripts
- Processing and analyzing log files and text data
- DevOps, CI/CD pipelines, and infrastructure management
- System administration and troubleshooting
- Software development and build processes
- Network configuration and diagnostics
- Security auditing and forensics
When Not to Use
- Simple tasks where a GUI is faster (e.g., casual photo editing)
- When you need real-time collaboration (use collaborative tools)
- For users who need accessibility features better served by GUIs
- Tasks requiring visual design (use design software)
- When a platform-specific tool is more appropriate (e.g., Windows admin)
Prerequisites
- Access to a Linux system (physical, VM, WSL, or cloud)
- Basic understanding of file systems and directories
- A terminal emulator application
Installation
wsl --installWindows Subsystem for Linux
wsl --install -d UbuntuInstall Ubuntu specifically
# Built-in, use Terminal.appmacOS is Unix-based
# Already installedOpen your terminal emulator
docker run -it ubuntu:latest bashPractice in a container
Verify installation: echo $SHELL && uname -a
Quick Start Steps
Open your terminal
Launch Terminal (Linux/macOS), Windows Terminal with WSL, or connect via SSH
# Press Ctrl+Alt+T on Linux, or open Terminal app
Explore your location
See where you are and what files exist
pwd # Print working directory ls -la # List all files with details cd ~ # Go to home directory
Create and manipulate files
Practice basic file operations
mkdir practice # Create a directory cd practice touch myfile.txt # Create empty file echo "Hello Linux" > myfile.txt cat myfile.txt # View file contents
Learn to get help
Every command has built-in documentation
man ls # Full manual for ls ls --help # Quick help which python # Find command location
ls -laList all files with permissions and details
cd <directory>Change to specified directory
pwdPrint current working directory
cp <src> <dest>Copy files or directories (-r for recursive)
mv <src> <dest>Move or rename files and directories
rm <file>Remove files (-r for directories, -f to force)
mkdir -p <dir>Create directory (and parents if needed)
cat <file>Display file contents
grep <pattern> <file>Search for patterns in files
find <path> -name <pattern>Find files by name or criteria
chmod <mode> <file>Change file permissions
sudo <command>Execute command as superuser
ps auxList all running processes
kill <pid>Terminate a process by ID
man <command>Display manual page for a command
Commands by Category
Pro Tips15
Master tab completion
workflowPress Tab to autocomplete commands, paths, and filenames. Double-tap Tab to see all options when multiple matches exist.
Search command history with Ctrl+R
workflowPress Ctrl+R and start typing to search through your command history. Press Ctrl+R again to cycle through matches.
Ctrl+R then type 'git' to find recent git commandsUse history shortcuts
workflow!! repeats the last command, !$ gives the last argument. sudo !! is incredibly useful when you forgot sudo.
sudo !! to run last command with sudoRetyping long commands manuallyUse xargs for bulk operations
performancePipe results to xargs to run commands on multiple items. Use -P for parallel execution.
find . -name '*.log' | xargs -P 4 gzipAlways quote variables in scripts
gotchaUnquoted variables break on filenames with spaces. Always use "$variable" instead of $variable.
rm "$file"rm $fileUse set -e in scripts for safety
best-practiceset -e makes scripts exit on first error. Combine with set -u (undefined var error) and set -o pipefail.
set -euo pipefail at script startUnderstand stdout vs stderr
gotcha1 is stdout, 2 is stderr. Redirect both: command > file 2>&1. Suppress errors: command 2>/dev/null
find / -name foo 2>/dev/nullUse less instead of cat for large files
workflowcat dumps everything to terminal. less lets you scroll, search (/pattern), and navigate efficiently.
less /var/log/syslogcat /var/log/syslogTest destructive commands with echo first
best-practiceBefore running rm or other dangerous commands, prepend echo to see what would happen.
echo rm *.log first, then rm *.logPrefer rsync over cp for large transfers
performancersync shows progress, can resume interrupted transfers, and only copies changed files on subsequent runs.
rsync -avh --progress source/ dest/cp -r source/ dest/Create aliases for frequent commands
automationAdd aliases to ~/.bashrc or ~/.zshrc for commands you use often. Example: alias ll='ls -la'
alias update='sudo apt update && sudo apt upgrade'Use screen or tmux for persistent sessions
workflowTerminal multiplexers keep sessions alive when you disconnect. Essential for long-running tasks on remote servers.
tmux new -s mysession for persistent workLearn essential Ctrl shortcuts
workflowCtrl+A (start of line), Ctrl+E (end), Ctrl+K (delete to end), Ctrl+U (delete to start), Ctrl+W (delete word), Ctrl+L (clear screen)
Check if command exists in scripts
best-practiceUse 'command -v' or 'which' to verify a command is available before using it in scripts.
command -v docker &>/dev/null || { echo "Docker not found"; exit 1; }Use LC_ALL=C for faster text processing
performanceSetting LC_ALL=C tells grep/sort to use ASCII-only matching, which is much faster for large files.
LC_ALL=C grep 'pattern' largefile.txtKey Facts10
In Linux, everything is a file
Devices (/dev/sda), processes (/proc/1234), system info (/sys) are all represented as files. This unified interface simplifies system interaction.
Permission bits: 4=read, 2=write, 1=execute
These add up for octal notation: 7=rwx, 6=rw-, 5=r-x, 4=r--, 0=---. Three digits represent user, group, others.
Exit code 0 means success, non-zero means failure
Check with $? after a command. Scripts should exit with appropriate codes. Use 'set -e' to fail on errors.
SIGTERM (15) is graceful stop, SIGKILL (9) is force kill
Always try SIGTERM first (kill PID). SIGKILL doesn't let processes clean up. SIGHUP (1) often reloads config.
File descriptors: 0=stdin, 1=stdout, 2=stderr
Redirect: > (stdout to file), 2> (stderr to file), &> (both), >> (append), < (stdin from file).
Files starting with . are hidden by default
Use ls -a to see them. Config files like .bashrc, .gitignore follow this convention.
Root (UID 0) has unlimited system access
Use sudo for privileged operations instead of logging in as root. This provides audit trails and limits exposure.
Symlinks point to paths, hardlinks point to inodes
Symlinks can cross filesystems and link to directories. Hardlinks share the same data blocks on disk.
Process states: R (running), S (sleeping), D (disk wait), Z (zombie)
Zombies are finished processes waiting for parent to read exit status. Too many zombies indicate a parent process issue.
PATH determines where shell looks for commands
Commands are found by searching directories in PATH order. Add custom paths: export PATH=$PATH:/custom/bin
Interview & Exam Practice7
A server is running out of disk space. You need to quickly identify what's consuming the most space.
What commands would you use to diagnose and resolve this?
Users report the application is slow. You SSH into the server and need to identify what's consuming resources.
What's your troubleshooting approach?
A web application can't write to its log directory. You see 'Permission denied' errors. The directory exists and is owned by root.
How do you fix this properly?
After deploying an update, the nginx service fails to start. systemctl status shows it's 'failed'.
What's the best approach to diagnose and fix this?
You have a large CSV file and need to extract all rows where the third column value is greater than 100, then sum those values.
Which tool(s) would you use?
You need to run a long backup script on a remote server, but you must disconnect SSH before it completes.
How do you ensure the process continues after disconnecting?
You're trying to start a web server but port 8080 is already in use. You need to find and stop whatever is using it.