#Essential Linux Commands
Master the essential commands that every DevOps engineer uses daily.
#📋 Table of Contents
- Navigation Commands
- File Operations
- Viewing Files
- Finding Files
- Disk and Storage
- Compression and Archives
- Networking Commands
- System Information
#Navigation Commands
bash
1# Print working directory
2pwd
3
4# List files
5ls # Basic list
6ls -l # Long format
7ls -la # Include hidden files
8ls -lh # Human-readable sizes
9ls -lt # Sort by modification time
10ls -lS # Sort by size
11
12# Change directory
13cd /var/log # Absolute path
14cd .. # Parent directory
15cd ~ # Home directory
16cd - # Previous directory
17
18# Create directory
19mkdir mydir
20mkdir -p path/to/nested/dir # Create parent directories
21
22# Remove directory
23rmdir emptydir
24rm -r directory # Remove recursively
25rm -rf directory # Force remove (careful!)#File Operations
bash
1# Create empty file
2touch file.txt
3
4# Copy files
5cp source.txt dest.txt
6cp -r sourcedir/ destdir/ # Copy directory recursively
7cp -p file.txt backup.txt # Preserve attributes
8
9# Move/rename files
10mv oldname.txt newname.txt
11mv file.txt /path/to/destination/
12
13# Remove files
14rm file.txt
15rm -f file.txt # Force (no prompt)
16rm -rf directory/ # Recursive force
17
18# Create symbolic link
19ln -s /path/to/original link_name
20
21# Create hard link
22ln /path/to/original link_name#Viewing Files
bash
1# View entire file
2cat file.txt
3
4# View with line numbers
5cat -n file.txt
6
7# View beginning of file
8head file.txt
9head -n 20 file.txt # First 20 lines
10
11# View end of file
12tail file.txt
13tail -n 20 file.txt # Last 20 lines
14tail -f /var/log/syslog # Follow (live updates)
15
16# Page through file
17less file.txt
18# Navigation: space=next page, b=back, /=search, q=quit
19
20# View file with syntax highlighting (if bat installed)
21bat file.txt#Finding Files
#find Command
bash
1# Find by name
2find /home -name "*.txt"
3find . -name "config*"
4
5# Find by type
6find /var -type f # Files only
7find /var -type d # Directories only
8
9# Find by size
10find /var -size +100M # Larger than 100MB
11find /tmp -size -1k # Smaller than 1KB
12
13# Find by time
14find /var/log -mtime -7 # Modified in last 7 days
15find /tmp -atime +30 # Accessed more than 30 days ago
16
17# Find and execute
18find . -name "*.log" -exec rm {} \;
19find . -name "*.sh" -exec chmod +x {} \;
20
21# Find with multiple conditions
22find . -name "*.txt" -size +1M -mtime -7#locate Command
bash
1# Fast file search (uses database)
2locate nginx.conf
3
4# Update database
5sudo updatedb#which and whereis
bash
1# Find executable location
2which python
3which nginx
4
5# Find binary, source, and man pages
6whereis nginx#Disk and Storage
bash
1# Disk usage of current directory
2du -sh .
3du -sh * # Each item in directory
4du -h --max-depth=1 # One level deep
5
6# Disk free space
7df -h # Human-readable
8df -h / # Specific filesystem
9
10# List block devices
11lsblk
12
13# Mount filesystem
14sudo mount /dev/sdb1 /mnt/data
15
16# Unmount
17sudo umount /mnt/data
18
19# Check disk health
20sudo smartctl -a /dev/sda#Compression and Archives
#tar (Tape Archive)
bash
1# Create archive
2tar -cvf archive.tar directory/
3
4# Create compressed archive (gzip)
5tar -czvf archive.tar.gz directory/
6
7# Create compressed archive (bzip2)
8tar -cjvf archive.tar.bz2 directory/
9
10# Extract archive
11tar -xvf archive.tar
12
13# Extract compressed archive
14tar -xzvf archive.tar.gz
15tar -xjvf archive.tar.bz2
16
17# Extract to specific directory
18tar -xzvf archive.tar.gz -C /path/to/destination
19
20# List contents without extracting
21tar -tvf archive.tar.gz#gzip and zip
bash
1# Compress file
2gzip file.txt # Creates file.txt.gz
3
4# Decompress
5gunzip file.txt.gz
6gzip -d file.txt.gz
7
8# Create zip archive
9zip -r archive.zip directory/
10
11# Extract zip
12unzip archive.zip
13unzip archive.zip -d /path/to/destination#Networking Commands
bash
1# Network interfaces
2ip addr
3ip a # Short form
4ifconfig # Legacy
5
6# Routing table
7ip route
8route -n # Legacy
9
10# DNS lookup
11nslookup google.com
12dig google.com
13host google.com
14
15# Test connectivity
16ping -c 4 google.com
17
18# Trace route
19traceroute google.com
20tracepath google.com
21
22# Open ports and connections
23ss -tuln # TCP/UDP listening ports
24netstat -tuln # Legacy
25
26# Check port connectivity
27nc -zv host 80
28telnet host 80
29
30# Download files
31curl -O https://example.com/file.txt
32wget https://example.com/file.txt
33
34# HTTP request
35curl https://api.example.com/data
36curl -X POST -d '{"key":"value"}' https://api.example.com/data#System Information
bash
1# System info
2uname -a # All system info
3hostname # Hostname
4hostnamectl # Detailed host info
5
6# OS version
7cat /etc/os-release
8lsb_release -a # If available
9
10# Kernel version
11uname -r
12
13# CPU info
14lscpu
15cat /proc/cpuinfo
16
17# Memory info
18free -h
19cat /proc/meminfo
20
21# Disk info
22lsblk
23fdisk -l
24
25# Running processes
26ps aux
27ps aux | grep nginx
28top # Interactive
29htop # Better interactive
30
31# System uptime
32uptime
33
34# Who is logged in
35who
36w
37
38# Last logins
39last
40lastlog#Quick Reference
| Task | Command |
|---|---|
| List files | ls -la |
| Go home | cd ~ |
| Copy file | cp source dest |
| Move file | mv source dest |
| Delete file | rm file |
| View file | cat / less / tail |
| Find file | find . -name "pattern" |
| Disk usage | du -sh * |
| Disk free | df -h |
| Compress | tar -czvf archive.tar.gz dir/ |
| Extract | tar -xzvf archive.tar.gz |
| Ping | ping -c 4 host |
| Port check | ss -tuln |
[!TIP] Pro Tip: Use aliases for frequently used commands. Add them to
~/.bashrc:bashalias ll='ls -la' alias ..='cd ..' alias ports='ss -tuln'