Linux Tutorials

Linux Commands

mkdir

Create one or more directories. Use -p to also create parent directories automatically.

# Create a single directory
mkdir new_folder

# Create parent directories as needed
mkdir -p ~/projects/app/src

# Create with specific permissions
mkdir -m 755 backups

touch

Create empty files or update their access and modification timestamps.

# Create or update timestamp
touch file1.txt

# Update access time only
touch -a log.txt

# Set modification time to 2025-01-01 00:00
touch -m -t 202501010000 report.md

cp

Copy files or directories. Common flags: -r (recursive), -i (prompt), -v (verbose).

# Copy single file
cp file.txt backup.txt

# Recursive copy with verbose output
cp -rv src/ dest/

# Prompt before overwrite
cp -i config.conf /etc/

mv

Move or rename files and directories. Flags: -n (no-clobber), -i (prompt), -v (verbose).

# Rename a file
mv old.txt new.txt

# Move with verbose output
mv -v doc.pdf ~/Documents/

# Do not overwrite existing files
mv -n temp/ archive/

rm

Remove files or directories. Warning: irreversible! Flags: -r (recursive), -f (force), -i (prompt).

# Delete a single file
rm oldfile.txt

# Recursive + force delete
rm -rf unused_dir/

# Prompt before deleting .bak files
rm -i *.bak

pwd

Print the absolute path of the current working directory.

# Show current location
pwd    # e.g. /home/user/projects

ls

List directory contents. Common flags: -l (long), -a (all), -h (human sizes).

# Simple listing
ls

# Detailed listing of /etc
ls -l /etc

# All files with human-readable sizes
ls -lah ~/projects

# Colorized output
ls --color=auto ~/downloads

cat

Concatenate and display file contents. Useful to preview or combine files.

# Display a file
cat file.txt

# Display with line numbers
cat -n file.txt

# Combine multiple files
cat a.txt b.txt > merged.txt

grep

Search for patterns in files or input. Flags: -i (ignore case), -R (recursive), -n (show line numbers).

# Search for "error" in a file
grep "error" logfile.txt

# Filter running processes for "ssh" (ignore case)
ps aux | grep -i ssh

# Recursive search with line numbers
grep -Rin "TODO" ~/project/

find

Search for files and directories by name, type, time, size, and more.

# Find .log files in current directory tree
find . -name "*.log"

# Files modified in the last 7 days
find /var -type f -mtime -7

# Files larger than 100 MB
find /tmp -size +100M

chmod

Change file permissions. Modes can be octal (e.g. 755) or symbolic (e.g. u+rwx).

# Owner read/write, group/others read
chmod 644 config.yaml

# Add execute permission for owner
chmod u+x script.sh

# Remove write permission recursively for group/others
chmod -R go-w public/

chown

Change file owner and/or group. Syntax: owner:group.

# Set owner and group
chown user:staff file.txt

# Change only owner
chown user file.txt

# Change only group
chown :staff file.txt

sudo

Run a command as another user (default: root). Prompts for password unless timestamp valid.

# Update system packages as root
sudo apt update

# Restart a service
sudo systemctl restart nginx

# Run command as another user
sudo -u www-data whoami

# Open root shell
sudo bash

whoami

Print the current effective username.

# Show current user
whoami

# Show effective user with sudo
sudo whoami

ifconfig

Configure or display network interfaces (legacy tool).

# Enable an interface
sudo ifconfig eth0 up

# Assign IP address
sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0

# Disable interface
sudo ifconfig eth0 down

ip

Show or manipulate IP addresses, interfaces, routing tables (modern replacement for ifconfig).

# Show IP address of eth0
ip addr show eth0

# Bring interface up
ip link set eth0 up

# Add a default route
ip route add default via 192.168.1.1

ping

Send ICMP echo requests to test connectivity. Common flags: -c (count), -i (interval), -W (timeout).

# Ping a hostname until stopped
ping google.com

# Send 4 pings
ping -c 4 8.8.8.8

# Half-second interval, 1s timeout
ping -i 0.5 -W 1 example.com

ss

Dump socket statistics and display network connections (replacement for netstat).

# TCP/UDP listening ports
ss -tuln

# Include process info
ss -tulpn

# Summary statistics
ss -s

df

Report file system disk space usage. Flags: -h (human-readable), -T (show FS type).

# Human-readable disk usage
df -h

# Show filesystem type
df -T /home

du

Estimate file and directory space usage. Flags: -h (human), -s (summary).

# Summary of directory
du -sh ~/projects

# Breakdown of /var by depth=1
du -h --max-depth=1 /var

head

Display the first part of files. Flags: -n (lines), -c (bytes).

# First 10 lines
head file.txt

# First 20 lines
head -n 20 logfile.log

# First 100 bytes
head -c 100 /var/log/syslog

tail

Display the last part of files. Flags: -n (lines), -f (follow).

# Last 10 lines
tail file.txt

# Last 50 lines
tail -n 50 logfile.log

# Follow new log entries
tail -f /var/log/syslog

less

View file contents one screen at a time. Navigate with j/k, search with /.

# View a README file
less README.md

# Start at end of file
less +G /var/log/syslog

# Start at first occurrence of "Error"
less +/Error log.txt

ps

Report a snapshot of current processes. Common flags: aux (all users, detailed), -ef (full-format).

# Detailed list of processes
ps aux

# Filter processes for sshd
ps -ef | grep sshd

# Sort by memory usage
ps aux --sort=-%mem

kill

Send signals to processes. Common signals: -15 (terminate), -9 (force kill), -HUP (reload).

# Gracefully terminate a process
kill 1234

# Force kill a process
kill -9 1234

# Reload process configuration
kill -HUP 1234

free

Display memory and swap usage. Common flags: -h (human-readable), -m (MB).

# Human-readable sizes
free -h

# Show values in MB
free -m

uptime

Show how long the system has been running, number of users, and load averages.

# Show uptime
uptime    # e.g. 12:34:56 up 5 days, 2 users, load average: 0.01, 0.05, 0.07

uname

Print system information. Useful flags: -a (all), -r (kernel release), -m (architecture).

# Complete system info
uname -a

# Kernel release
uname -r

# Machine hardware architecture
uname -m

tar

Archive and extract files. Supports flags: -c (create), -x (extract), -z (gzip), -v (verbose), -f (file).

# Create an archive
tar -cvf archive.tar dir/

# Extract a gzip archive
tar -xvzf archive.tar.gz

# List contents
tar -tvf archive.tar

systemctl

Control and manage systemd services.

# Check service status
systemctl status nginx

# Restart SSH service
sudo systemctl restart ssh

# Enable Docker at boot
sudo systemctl enable docker

journalctl

Query and display logs from the systemd journal.

# Show SSH logs
journalctl -u ssh

# Follow new logs
journalctl -f

# Logs from the last hour
journalctl --since "1 hour ago"

rsync

Efficiently synchronize files/directories between locations. Common flags: -a (archive), -v (verbose), -z (compress).

# Sync with compression
rsync -avz src/ dest/

# Sync and delete extraneous files
rsync -av --delete src/ remote:/backup/

# Sync over SSH
rsync -e ssh -az src/ user@host:/path/

traceroute

Trace the path packets take to a host, showing each hop.

# Basic trace
traceroute google.com

# Skip DNS lookups
traceroute -n 8.8.8.8

# ICMP probes on port 80
traceroute -I -p 80 example.com

tcpdump

Capture and display packets on network interfaces. Supports filters and pcap output.

# Live capture on eth0
sudo tcpdump -i eth0

# Capture HTTPS traffic and write to file
sudo tcpdump -i eth0 port 443 -w https.pcap

# Verbose capture without name resolution
sudo tcpdump -nn -vv -i any

netstat

Print network connections, routing tables, and interface statistics (legacy tool).

# Listening TCP/UDP ports
netstat -tuln

# Show processes with sockets
netstat -tulpn

# Routing table
netstat -r

# Protocol statistics
netstat -s

apt

Package manager for Debian/Ubuntu. Install, update, remove, and upgrade software.

# Refresh package index
sudo apt update

# Install a package
sudo apt install nginx

# Remove a package
sudo apt remove nano

# Upgrade all upgradable packages
sudo apt upgrade

wget

Download files from the web using HTTP, HTTPS, or FTP.

# Download a file
wget http://example.com/file.zip

# Save with custom filename
wget -O custom.zip http://example.com/file.zip

# Continue partial download
wget -c http://example.com/large.iso

# Recursive download of directory
wget -r -l1 -nd http://site.com/files/

dpkg

Manage Debian packages (.deb) locally: install, remove, query, and purge.

# Install local package
dpkg -i package.deb

# Remove installed package
dpkg -r package_name

# Purge package and config files
dpkg -P package_name

# List installed packages matching "nginx"
dpkg -l | grep nginx

curl

Transfer data from or to a server using supported protocols (HTTP, HTTPS, FTP, etc.). Great for APIs.

# Simple GET request
curl https://api.github.com

# Save output to file
curl -o image.png http://site.com/img.png

# Fetch headers only
curl -I http://example.com

# Send POST request with data
curl -X POST -d "key=val" http://api/endpoint

man

Open the manual page for a command. Use sections (1=commands, 5=config files, 8=admin commands).

# Show manual for ls
man ls

# Search manuals for keyword "network"
man -k network

# Show section 5 (config files) for crontab
man 5 crontab

history

Display or manipulate the shell command history.

# List previous commands
history

# Repeat command #42
!42

# Search history for "ssh"
history | grep ssh

alias

Create shortcuts for commands in your shell (temporary unless added to .bashrc/.zshrc).

# Define ll as ls -lah
alias ll='ls -lah'

# Define gs as git status
alias gs='git status'

# Show all aliases
alias

ssh-keygen

Generate and manage SSH key pairs for authentication.

# Generate default RSA key
ssh-keygen

# Generate Ed25519 key
ssh-keygen -t ed25519

# Change passphrase for a key
ssh-keygen -f ~/.ssh/id_rsa -p

dig

Query DNS name servers for information about domains.

# A record lookup
dig example.com

# MX records
dig MX example.com

# Use Google DNS for query
dig @8.8.8.8 example.com

whois

Retrieve domain registration and ownership details.

# Basic domain info
whois example.com

# Check Google registration
whois google.com

nc

Read and write data across network connections (aka netcat). Great for testing ports and sockets.

# Listen on port 1234
nc -l 1234

# Connect to a host on port 80
nc host.example.com 80

# Send data
echo "Hello" | nc host 9000

top

Display running processes and system usage in real time (interactive).

# Show processes sorted by CPU usage
top

htop

Interactive process viewer with colors, tree view, and easier navigation (improved top).

# Start htop (may need sudo apt install htop)
htop