🐧

Linux Commands Snippet

88 essential Linux sysadmin commands that are long, complex, and hard to remember. Copy, paste, and execute!

Showing 88 commands
📁

Find files by multiple criteria

files

Find files older than 30 days, larger than 100MB, and show details

find /path -type f -name '*.log' -mtime +30 -size +100M -exec ls -lh {} \;
🗑️

Find and delete old files

files

Delete files older than 30 days

find /var/log -type f -name '*.log' -mtime +30 -delete
💾

Find largest files in directory

files

Find top 20 largest files/directories sorted by size

du -h /path | sort -rh | head -20
📂

Find empty directories

files

Find all empty directories

find /path -type d -empty
📤

Secure copy with compression

network

SCP with compression, recursive, custom port

scp -C -r -P 2222 user@host:/remote/path /local/path
🔒

SSH with port forwarding

network

Local port forwarding: access remote port 80 via local port 8080

ssh -L 8080:localhost:80 -N -f user@remote-host
🔓

SSH reverse tunnel

network

Reverse tunnel: expose local port 3000 on remote port 9090

ssh -R 9090:localhost:3000 -N -f user@remote-host
🔍

TCP SYN port scan

network

Stealth SYN scan all ports showing only open ones

nmap -sS -p 1-65535 --open 192.168.1.1
🌐

Netcat port scan

network

Quick port scan using netcat on ports 1-1000

nc -zv -w2 192.168.1.1 1-1000
📡

TCP dump specific port

network

Capture 1000 packets on port 80 and save to file

tcpdump -i eth0 port 80 -w capture.pcap -c 1000
📊

Monitor network connections

network

Show most frequent established connections by IP

ss -tunapl | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn
🚨

Detect DDoS attack sources

network

Count connections by IP to identify potential DDoS attackers (shows top 20 IPs with most connections)

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -20
👂

Check process listening on port

process

Find which process is using port 8080

lsof -i :8080 | grep LISTEN
💀

Kill all processes by name

process

Kill all processes matching the pattern

pkill -f "process-name"
🔥

Show processes sorted by CPU

process

Show top 20 processes by CPU usage

ps aux --sort=-%cpu | head -20
🧠

Show processes sorted by memory

process

Show top 20 processes by memory usage

ps aux --sort=-%mem | head -20
🐢

Run process with low priority

process

Run with lowest CPU and I/O priority

nice -n 19 ionice -c 3 command

Change running process priority

process

Change CPU priority of existing process

renice -n 10 -p $(pgrep process-name)
🌙

Monitor process in background

process

Run command persistently after logout

nohup command > output.log 2>&1 & disown
📦

Create tar with progress

files

Create compressed tar with progress bar

tar -czf - /source | pv -s $(du -sb /source | awk '{print $1}') > backup.tar.gz
📂

Extract tar to specific directory

files

Extract tar skipping top-level directory

tar -xzf archive.tar.gz -C /destination --strip-components=1
🔄

Rsync with progress and delete

files

Sync directories, show progress, delete extraneous files

rsync -avh --progress --delete /source/ /destination/
🚀

Rsync over SSH with compression

files

Rsync via SSH with compression and custom port

rsync -avz --progress -e "ssh -p 2222" /local/ user@host:/remote/
📝

Sed replace with backup

text

Replace text with automatic backup creation

sed -i.bak 's/old-text/new-text/g' file.txt
✂️

Sed delete lines matching pattern

text

Delete lines containing specific pattern

sed -i '/pattern-to-delete/d' file.txt
🧮

Awk sum column values

text

Sum first column and print formatted result

awk '{sum+=$1} END {printf "Total: %.2f\n", sum}' file.txt
📊

Awk filter and print multiple columns

text

Filter CSV where column 3 > 100, format output

awk -F, '$3 > 100 {printf "%-20s %10.2f\n", $1, $3}' data.csv
🔍

Grep with context lines

text

Search with 3 lines of context and line numbers

grep -C 3 -n "search-term" file.txt
⚠️

Grep multiple patterns with OR

text

Search for multiple patterns (OR condition)

grep -E "(error|warning|critical)" log.txt
📈

Sort and count unique values

text

Count frequency and show top 20

sort file.txt | uniq -c | sort -rn | head -20

Xargs parallel execution

text

Process files in parallel with 4 jobs

find . -name '*.log' | xargs -P 4 -I {} gzip {}
👁️

Watch command with highlight

system

Watch command every second, highlight differences

watch -n 1 -d 'ps aux | grep process-name'
💿

Check disk IO statistics

system

Extended IO stats, 1-second intervals, 10 times

iostat -x 1 10
🧠

Check memory usage details

system

Detailed memory statistics breakdown

vmstat -s | head -20
🔥

Check CPU usage per core

system

CPU stats per core, 1-second interval, 5 times

mpstat -P ALL 1 5
📜

Check system load history

system

System activity report, CPU usage over time

sar -u 1 10
⚙️

Check kernel messages

system

Show recent kernel errors

dmesg | grep -i error | tail -20
📂

List open files by user

system

Count open files by specific user

lsof -u username | wc -l
🌐

Check network interface stats

network

Show detailed network interface statistics

ip -s link show dev eth0
👁️

Monitor file changes in real-time

files

Watch directory for file changes recursively

inotifywait -m -r -e create,modify,delete /path/to/watch
💾

Create swap file

system

Create and enable 2GB swap file

fallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
🚦

Limit process resources

system

Set file descriptor and process limits

ulimit -n 4096; ulimit -u 100; command
🔐

Change file permissions recursively

files

Set 644 for files, 755 for directories recursively

find /path -type f -exec chmod 644 {} \; && find /path -type d -exec chmod 755 {} \;
👤

Change ownership recursively

files

Change owner and group recursively

chown -R user:group /path
🔗

Create hard and symbolic links

files

Create both hard link and symbolic link

ln target hardlink; ln -s target symlink
💿

Mount disk image

system

Mount ISO file as loop device

mount -o loop disk.iso /mnt/iso; umount /mnt/iso
🔑

Generate secure random password

system

Generate 32-byte base64 encoded random string

openssl rand -base64 32
📝

Base64 encode/decode

text

Base64 encode and decode text

echo 'text' | base64; echo 'dGV4dA==' | base64 -d
🔢

Calculate MD5/SHA checksums

files

Generate MD5 and SHA256 checksums

md5sum file.txt; sha256sum file.txt
🌳

Create directory tree structure

files

Display directory structure (fallback to find if no tree)

tree -d -L 3 /path; find /path -maxdepth 3 -type d | sort
⚖️

Compare two files

text

Unified diff with color highlighting

diff -u file1.txt file2.txt | colordiff
✂️

Split large file

files

Split file into 100MB chunks and rejoin

split -b 100M largefile.tar.gz 'part-'; cat part-* > combined.tar.gz
📄

Stream edit large file

text

Extract lines 10001-19999 from large file efficiently

awk 'NR>10000 && NR<20000' huge.log > subset.log
⬇️

Download file with curl

download

Download file and save with original filename

curl -O https://example.com/file.zip

Download with resume support

download

Resume interrupted download

curl -C - -O https://example.com/large-file.iso
💾

Download with custom filename

download

Download and save with custom filename

curl -o myfile.zip https://example.com/file.zip
🔄

Download following redirects

download

Follow redirects and download

curl -L -o file.tar.gz https://example.com/download
🔐

Download with authentication

download

Download with HTTP Basic Auth

curl -u username:password -O https://example.com/protected/file.zip
📋

Download with headers

download

Download with custom HTTP headers

curl -H 'Authorization: Bearer TOKEN' -O https://api.example.com/data.json
📊

Download showing progress bar

download

Download with progress bar instead of default stats

curl -# -o file.iso https://example.com/file.iso

Download multiple files parallel

download

Download multiple files in parallel

curl -Z -O https://example.com/file1.zip -O https://example.com/file2.zip
📄

Check HTTP headers only

download

Fetch HTTP headers without downloading body

curl -I https://www.example.com
📤

POST data with curl

download

Send POST request with form data

curl -X POST -d 'name=value&foo=bar' https://api.example.com/endpoint
📡

POST JSON with curl

download

Send POST request with JSON data

curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://api.example.com/endpoint
🏥

Test API endpoint

download

Test API with response time and status code

curl -s -w '\nHTTP Code: %{http_code}\nTime: %{time_total}s\n' https://api.example.com/health
⬇️

Download with wget

download

Download file using wget

wget https://example.com/file.zip

Download with resume (wget)

download

Resume interrupted download with wget

wget -c https://example.com/large-file.iso
📁

Download to directory

download

Download file to specific directory

wget -P /path/to/downloads https://example.com/file.zip
🌐

Mirror website with wget

download

Create local mirror of website

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent https://example.com/
🐢

Download with bandwidth limit

download

Download with 200KB/s speed limit

wget --limit-rate=200k https://example.com/large-file.iso
📋

Batch download from file

download

Download all URLs listed in file

wget -i urls.txt
🌙

Download in background

download

Download in background with log file

wget -b https://example.com/large-file.iso -O /var/log/wget.log
👁️

View last N lines of log

logs

Monitor log file in real-time (follow mode)

tail -f /var/log/nginx/access.log
🔍

Search in log file

logs

Search for errors in syslog (case-insensitive)

grep -i 'error' /var/log/syslog | tail -50
🔢

Count log entries by pattern

logs

Count occurrences of pattern in log

grep -c '404' /var/log/nginx/access.log
📅

View logs by time range

logs

Extract log entries between two timestamps

awk '/2024-01-15 10:00/,/2024-01-15 11:00/' /var/log/syslog
📊

Analyze web access by IP

logs

Show top 20 IP addresses accessing website

awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20
🐢

Find slow requests

logs

Find requests taking more than 5 seconds

awk '$NF > 5 {print $0}' /var/log/nginx/access.log | tail -20
📈

Extract HTTP status codes

logs

Count HTTP response status codes

awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
🔐

Analyze failed SSH attempts

logs

Find IPs with most failed SSH login attempts

grep 'Failed password' /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -rn | head -10
👤

Audit user login activity

logs

Show recent user login/logout history

last | head -20

Check sudo commands used

logs

Show recent sudo commands executed

grep 'COMMAND=' /var/log/auth.log | tail -30
⚙️

Monitor kernel messages

logs

Show recent kernel errors

dmesg | grep -i error | tail -20
🐘

Analyze Apache error log

logs

Count PHP fatal errors in Apache log

grep -o 'PHP Fatal error.*' /var/log/apache2/error.log | sort | uniq -c | sort -rn
📦

Compress and rotate logs

logs

Compress log files older than 7 days

find /var/log -name '*.log' -mtime +7 -exec gzip {} \;
📡

Real-time journalctl monitoring

logs

Monitor nginx service logs in real-time

journalctl -u nginx -f
💾

Export journal logs

logs

Export last hour logs to file

journalctl --since '1 hour ago' --no-pager > /tmp/recent_logs.txt
🗄️

Analyze MySQL slow queries

logs

Analyze MySQL slow query log sorted by time

mysqldumpslow -s t /var/log/mysql/slow.log | head -20