Linux Cheat Sheet

🔍

1. Files & Directories

Commands for moving through the filesystem, creating, copying, moving, deleting, and viewing files and directories.

Navigation & Management

pwd

Shows the current directory path.

ls -l

Detailed list of files and directories.

ls -a

Lists all files, including hidden ones.

cd <dir>

Change directory (`..` moves up, `~` goes home).

mkdir <dir>

Creates a new directory.

cp <orig> <dest>

Copies files or directories (`-r` for recursive).

mv <orig> <dest>

Moves or renames files/directories.

rm <file>

Deletes files (`-r` for directories, `-f` to force).

touch <file>

Creates an empty file or updates its timestamp.

Viewing & Searching

cat <file>

Displays the entire content of a file.

less <file>

Displays paginated content (allows navigation).

more <file>

Displays paginated content (simple).

head -n 10 <file>

Displays the first 10 lines.

tail -n 10 <file>

Displays the last 10 lines.

tail -f <file>

Follows the file's growth in real-time (for logs).

find <path> -name "<pattern>"

Searches for files/directories (e.g., `find /var/log -name "*.log"`).

Archives

tar -czvf <out.tar.gz> <dir>

Creates a compressed archive (`c`reate, `z`ip, `v`erbose, `f`ile).

tar -xzvf <file.tar.gz>

Extracts a compressed archive (`x`tract).

zip / unzip

Compresses and decompresses `.zip` files.

2. Users & Permissions

Managing user accounts, groups, and access control for files and directories.

useradd -m -s /bin/bash <user>

Creates a user with a home directory (`-m`) and shell.

passwd <user>

Changes a user's password.

usermod -aG <group> <user>

Adds a user to a group (e.g., `usermod -aG sudo lucas`).

userdel -r <user>

Deletes a user and their home directory (`-r`).

sudo <cmd>

Executes a command with superuser privileges.

chmod <mode> <file>

Changes permissions (e.g., `755` or `+x`).

chown <user>:<group> <file>

Changes the owner and group.

chgrp <group> <file>

Changes only the group owner.

ls -l

Shows current permissions and owners.

3. Processes & Tasks

Commands to view, manage, and terminate running processes, as well as handle background tasks.

ps aux

Lists all running processes with details.

top

Interactive view of processes and CPU/Memory usage.

htop

Improved, interactive version of `top`.

kill <PID>

Sends a signal to terminate a process by its ID.

killall <name>

Terminates all processes by name (e.g., `killall apache2`).

pkill <name>

Terminates processes by name or pattern.

command &

Executes a command in the background.

jobs

Lists background jobs in the current session.

fg %<job>

Brings a background job to the foreground.

bg %<job>

Resumes a suspended job in the background.

nohup <cmd> &

Runs a process that ignores terminal close signals.

lsof -i:<port>

Lists which process is using a specific port (e.g., `lsof -i:80`).

4. System Services (systemd)

Controlling services (daemons) on modern systems using `systemd`.

systemctl start <service>

Starts a service.

systemctl stop <service>

Stops a service.

systemctl restart <service>

Restarts a service.

systemctl status <service>

Checks the status of a service.

systemctl enable <service>

Enables a service to start on boot.

systemctl disable <service>

Disables a service from starting on boot.

service <srv> <act>

Legacy command (SysVinit) or wrapper for systemctl.

5. System Monitoring & Performance

Tools to check CPU, memory, disk usage, and other system resources.

uptime

Shows system uptime and average load (1, 5, 15 min).

w

Shows logged-in users and their processes.

free -h

Shows RAM and swap usage (human-readable).

df -h

Reports used/available space on filesystems (human-readable).

du -sh <dir>

Shows total space used by a directory (human-readable).

vmstat 5

Shows stats (CPU, mem, I/O) updated every 5 sec.

iostat

Shows disk I/O performance metrics.

tail -f /var/log/syslog

Monitors a log file in real-time.

journalctl -xe

Shows systemd logs (with latest errors).

journalctl -f

Follows systemd logs in real-time (like `tail -f`).

6. Networking & Connectivity

Tools to diagnose network configuration, connectivity, DNS, and remote access.

ip addr

Shows network interface information (IPs, MAC).

ip route

Shows the routing table.

ifconfig

(Legacy) Shows network interfaces.

ping <host>

Checks connectivity and latency.

traceroute <host>

Traces the packet route to a host.

nslookup <domain>

Queries DNS (simple).

dig <domain>

Powerful tool for detailed DNS queries.

netstat -tuln

(Legacy) Shows listening ports (TCP/UDP) and connections.

ss -tuln

(Modern) Shows listening ports (replaces netstat).

ssh <user>@<host>

Opens a secure remote shell (SSH).

ssh -p <port> <user>@<host>

Connects SSH to a specific port.

ssh-keygen

Generates an SSH key pair (public/private).

ssh-copy-id <user>@<host>

Copies the public SSH key to a server for passwordless login.

scp <orig> <dest>

Securely copies files between hosts via SSH.

curl <URL>

Performs HTTP/HTTPS requests (test APIs, download).

wget <URL>

Downloads files from the web.

nc -zv <host> <port>

(netcat) Checks if a port is open on a host.

Choose a filename for each file. File paths must be unique. File: `Interactive Linux Cheat Sheet:linux_cheatsheet_spa.html` ```html
nc -l <port>

(netcat) Starts a listener on a port.

7. Scripting & Automation

Essential commands for text processing, chaining commands, and automating repetitive tasks.

Text Processing

grep "pattern" <file>

Searches for text matching a pattern (`-r` recursive).

sed 's/old/new/g' <file>

Edits text (e.g., replaces "old" with "new").

awk '{print $1,$3}' <file>

Processes text by columns (prints col 1 and 3).

sort

Sorts lines of text.

uniq

Filters or counts duplicate lines.

cut -d: -f1

Extracts fields (e.g., `-d:` delimiter, `-f1` field 1).

Pipelines & Tasks

command1 | command2

(Pipe) Sends output of `command1` to input of `command2`.

> <file>

Redirects standard output (overwrites file).

>> <file>

Redirects standard output (appends to file).

2> <file>

Redirects standard error (stderr).

cmd > out.txt 2> err.txt

Saves standard output to `out.txt` and errors to `err.txt`.

command | tee <file>

Duplicates output: shows on screen AND saves to a file.

alias gs='git status'

Creates a shortcut (alias) for a command. (Define in `~/.bashrc`).

chmod +x <script.sh>

Gives execution permission to a script.

./script.sh

Executes a script (that has permission).

crontab -e

Edits the current user's scheduled tasks (cron).

8. Package Management

Commands to install, update, and remove software on common distributions.

Debian/Ubuntu (apt)

apt-get update

Synchronizes the package list from repositories.

apt-get upgrade

Updates all installed packages.

apt-get install <package>

Installs a new package.

apt-get remove <package>

Uninstalls a package.

apt-cache search <text>

Searches for packages by name or description.

dpkg -i <file.deb>

Installs a local `.deb` package.

Red Hat/CentOS (yum/dnf)

yum update

Updates all system packages.

yum install <package>

Installs a new package.

yum remove <package>

Uninstalls a package.

yum search <text>

Searches for available packages.

dnf

Modern manager in RHEL/CentOS (replaces `yum`).

rpm -ivh <file.rpm>

Installs a local `.rpm` package.