Linux Commands

mkdir

Create one or more directories; use -p to also create parent directories.

Examples:

mkdir new_folder    # create a single directory
mkdir -p ~/projects/app/src    # create parent dirs as needed
mkdir -m 755 backups    # set permissions on creation

touch

Create empty files or update their access/modification timestamps.

Examples:

touch file1.txt    # create or update timestamp
touch -a log.txt    # update access time only
touch -m -t 202501010000 report.md    # set mod time to 2025-01-01 00:00

cp

Copy files or directories. Flags: -r (recursive), -i (interactive), -v (verbose).

Examples:

cp file.txt backup.txt    # copy single file
cp -rv src/ dest/    # recursive + verbose
cp -i config.conf /etc/    # prompt before overwrite

mv

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

Examples:

mv old.txt new.txt    # rename file
mv -v doc.pdf ~/Documents/    # move with verbose output
mv -n temp/ archive/    # don’t overwrite existing

rm

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

Examples:

rm oldfile.txt    # delete single file
rm -rf unused_dir/    # recursive + force
rm -i *.bak    # prompt for each .bak file

pwd

Print the absolute path of the current working directory.

Example:

pwd    # e.g. /home/user/projects

ls

List directory contents. Flags: -l (long format), -a (all files), -h (human-readable sizes).

Examples:

ls    # simple listing
ls -l /etc    # detailed listing of /etc
ls -lah ~/projects    # all, long, human sizes
ls --color=auto ~/downloads    # colorize output

cat

Concatenate and display file contents. Flags: -n (number lines), -E (show $ at ends).

Examples:

cat file.txt    # display file
cat -n file.txt    # display with line numbers
cat a.txt b.txt > merged.txt    # combine files

grep

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

Examples:

grep "error" logfile.txt    # search for "error"
ps aux | grep -i ssh    # filter processes for "ssh"
grep -Rin "TODO" ~/project/    # recursive + numbers

find

Search for files/directories. Conditions: -name, -type, -mtime, -size, etc.

Examples:

find . -name "*.log"    # find .log files in cwd
find /var -type f -mtime -7    # files changed in last 7 days
find /tmp -size +100M    # files larger than 100 MB

chmod

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

Examples:

chmod 644 config.yaml    # owner read/write, group/other read
chmod u+x script.sh    # add execute for owner
chmod -R go-w public/    # remove write for group/other recursively

chown

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

Examples:

chown user:staff file.txt    # set owner and group
chown user file.txt    # only owner
chown :staff file.txt    # only group

sudo

Execute a command as another user (default: root). Prompts for your password unless you have a valid timestamp.

Examples:

sudo apt update    # run apt update as root
sudo systemctl restart nginx    # restart service
sudo -u www-data whoami    # run as user www-data
sudo visudo    # safely edit sudoers
sudo !!    # repeat last command with sudo
sudo bash    # open root shell

whoami

Print the current effective username.

Examples:

whoami    # e.g. "user"
sudo whoami    # prints "root" when run with sudo
echo "Current user: $(whoami)"    # inline usage

ifconfig

Configure network interfaces (legacy tool). Bring interfaces up/down, assign addresses.

Examples:

sudo ifconfig eth0 up    # enable interface
sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0    # assign IP
sudo ifconfig eth0 down    # disable interface

ip

Show/manipulate routing, devices, interfaces, and tunnels (modern replacement for ifconfig).

Examples:

ip addr show eth0    # show IP address
ip link set eth0 up    # enable interface
ip route add default via 192.168.1.1    # add default gateway

ping

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

Examples:

ping google.com    # unlimited until stopped
ping -c 4 8.8.8.8    # send 4 pings
ping -i 0.5 -W 1 example.com    # half-second interval, 1s timeout

ss

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

Examples:

ss -tuln    # TCP/UDP listening, numeric
ss -tulpn    # include process info
ss -s    # summary statistics

df

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

Examples:

df -h    # human-readable sizes
df -T /home    # show type for /home

du

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

Examples:

du -sh ~/projects    # summary of ~/projects
du -h --max-depth=1 /var    # depth=1 breakdown

head

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

Examples:

head file.txt    # first 10 lines
head -n 20 logfile.log    # first 20 lines
head -c 100 /var/log/syslog    # first 100 bytes

tail

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

Examples:

tail file.txt    # last 10 lines
tail -n 50 logfile.log    # last 50 lines
tail -f /var/log/syslog    # follow new entries

less

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

Examples:

less README.md    # view README
less +G /var/log/syslog    # start at end
less +/Error log.txt    # start at first “Error”

ps

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

Examples:

ps aux    # detailed list
ps -ef | grep sshd    # filter for sshd
ps aux --sort=-%mem    # sort by memory

kill

Send signals to processes. Common: -15 (SIGTERM), -9 (SIGKILL), -HUP (reload).

Examples:

kill 1234    # SIGTERM
kill -9 1234    # force kill
kill -HUP 1234    # reload config

free

Display memory usage. Flags: -h (human-readable), -m (MB).

Examples:

free -h    # human sizes
free -m    # MB units

uptime

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

Example:

uptime    # e.g. 12:34:56 up 5 days, 3:21, 2 users, load average: 0.00, 0.01, 0.05

uname

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

Examples:

uname -a    # complete info
uname -r    # kernel release
uname -m    # machine hardware name

tar

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

Examples:

tar -cvf archive.tar dir/    # create
tar -xvzf archive.tar.gz    # extract gzip
tar -tvf archive.tar    # list contents

systemctl

Control systemd services. Commands: start, stop, enable, status.

Examples:

systemctl status nginx    # check status
sudo systemctl restart ssh    # restart SSH
sudo systemctl enable docker    # enable at boot

journalctl

Query the systemd journal. Flags: -u (unit), -f (follow), --since, --until.

Examples:

journalctl -u ssh    # SSH logs
journalctl -f    # follow logs
journalctl --since "1 hour ago"    # last hour

rsync

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

Examples:

rsync -avz src/ dest/    # archive, verbose, compress
rsync -av --delete src/ remote:/backup/    # delete extraneous files
rsync -e ssh -az src/ user@host:/path/    # over SSH

traceroute

Trace the network path packets take to a host by sending probes with increasing TTL.

Examples:

traceroute google.com    # basic trace
traceroute -n 8.8.8.8    # skip DNS lookups
traceroute -I -p 80 example.com    # ICMP + port 80

tcpdump

Capture and display packets on network interfaces. Use BPF filters to narrow traffic.

Examples:

sudo tcpdump -i eth0    # live capture
sudo tcpdump -i eth0 port 443 -w https.pcap    # capture HTTPS
sudo tcpdump -nn -vv -i any    # no name resolution, verbose

netstat

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

Examples:

netstat -tuln    # listening TCP/UDP
netstat -tulpn    # include PIDs
netstat -r    # routing table
netstat -s    # protocol stats

apt

Manage packages on Debian/Ubuntu. Update package lists, install, remove, and upgrade packages.

Examples:

sudo apt update               # refresh package index
sudo apt install nginx        # install nginx
sudo apt remove nano          # remove nano editor
sudo apt upgrade              # upgrade all upgradable packages

wget

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

Examples:

wget http://example.com/file.zip         # simple download
wget -O custom.zip http://example.com/file.zip  # save as custom.zip
wget -c http://example.com/large.iso     # continue partial download
wget -r -l1 -nd http://site.com/files/   # recursive, depth=1, no dirs

dpkg

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

Examples:

dpkg -i package.deb           # install a local .deb package
dpkg -r package_name         # remove an installed package
dpkg -P package_name         # purge package and its config files
dpkg -l | grep nginx         # list installed packages matching “nginx”

curl

Transfer data from or to a server using various protocols.

Examples:

curl https://api.github.com          # GET request
curl -o image.png http://site.com/img.png   # save output
curl -I http://example.com           # fetch headers only
curl -X POST -d "key=val" http://api/endpoint  # POST data

man

Open the manual page for a command.

Examples:

man ls                  # show ls manual
man -k network          # search manuals for “network”
man 5 crontab           # show section 5 (file formats) for crontab

history

Display or manipulate the command history.

Examples:

history                 # list previous commands
!42                     # repeat command #42
history | grep ssh      # search history for “ssh”

alias

Create shortcuts for commands.

Examples:

alias ll='ls -lah'        # define ll
alias gs='git status'     # define gs
alias                       # list all aliases

ssh-keygen

Generate, manage and convert SSH key pairs.

Examples:

ssh-keygen                   # default RSA key
ssh-keygen -t ed25519        # generate Ed25519 key
ssh-keygen -f ~/.ssh/id_rsa -p  # change passphrase

dig

Query DNS name servers for information.

Examples:

dig example.com               # A record lookup
dig MX example.com            # MX records
dig @8.8.8.8 example.com      # use Google DNS

whois

Retrieve domain registration and ownership details.

Examples:

whois example.com             # domain info
whois google.com              # google’s registration

nc

Read and write data across network connections (netcat).

Examples:

nc -l 1234                    # listen on port 1234
nc host.example.com 80        # connect to port 80
echo "Hello" | nc host 9000   # send data