Introduction to Linux Command Line
The Linux command line is a powerful tool for system administration. This comprehensive guide covers essential commands that every administrator should master.
File and Directory Operations
Navigation Commands
# Change directory
cd /path/to/directory
cd ~ # Go to home directory
cd .. # Go up one level
cd - # Go to previous directory
# Print working directory
pwd
# List files and directories
ls # Basic listing
ls -la # Detailed listing with hidden files
ls -lh # Human-readable file sizes
ls -ltr # Sort by time, reverse order
ls -R # Recursive listing
File Management
# Create files
touch filename.txt
touch file1.txt file2.txt file3.txt
# Create directories
mkdir directory_name
mkdir -p /path/to/nested/directory
# Copy files and directories
cp source.txt destination.txt
cp -r source_dir/ dest_dir/
cp -a source/ dest/ # Preserve attributes
# Move/rename files
mv oldname.txt newname.txt
mv file.txt /new/location/
# Remove files and directories
rm filename.txt
rm -f file.txt # Force remove
rm -r directory/ # Remove directory
rm -rf directory/ # Force remove directory (use carefully!)
# Find files
find /path -name "*.txt"
find . -type f -name "*.log"
find /var/log -mtime -7 # Modified in last 7 days
find . -size +100M # Files larger than 100MB
File Viewing and Editing
# View file contents
cat filename.txt
less filename.txt # Paginated view
more filename.txt
head -n 20 file.txt # First 20 lines
tail -n 50 file.txt # Last 50 lines
tail -f /var/log/syslog # Follow log file in real-time
# Search in files
grep "search_term" filename.txt
grep -r "pattern" /directory/
grep -i "case_insensitive" file.txt
grep -v "exclude_pattern" file.txt
# Count lines, words, characters
wc filename.txt
wc -l file.txt # Count lines only
System Information Commands
# System information
uname -a # All system info
hostnamectl # Host information
uptime # System uptime
whoami # Current user
who # Logged in users
w # Detailed user activity
# Hardware information
lscpu # CPU information
free -h # Memory usage
df -h # Disk usage
du -sh /path # Directory size
lsblk # Block devices
lspci # PCI devices
lsusb # USB devices
# Operating system info
cat /etc/os-release
lsb_release -a
Process Management
# View processes
ps aux # All processes
ps -ef # Full format listing
top # Real-time process viewer
htop # Enhanced process viewer
pgrep process_name # Find process by name
# Kill processes
kill PID
kill -9 PID # Force kill
killall process_name
pkill -f pattern
# Background and foreground
command & # Run in background
jobs # List background jobs
fg %1 # Bring job to foreground
bg %1 # Resume job in background
nohup command & # Run immune to hangups
User and Permission Management
# User management
useradd username
useradd -m -s /bin/bash username
usermod -aG sudo username
passwd username
userdel username
userdel -r username # Delete with home directory
# Group management
groupadd groupname
usermod -aG groupname username
groups username
gpasswd -d username groupname
# File permissions
chmod 755 filename
chmod u+x script.sh
chmod -R 644 /directory/
chown user:group filename
chown -R user:group /directory/
# View permissions
ls -l filename
stat filename
Package Management
Ubuntu/Debian (APT)
# Update package lists
apt update
# Upgrade packages
apt upgrade
apt full-upgrade
# Install packages
apt install package_name
apt install package1 package2 package3
# Remove packages
apt remove package_name
apt purge package_name
apt autoremove
# Search packages
apt search keyword
apt show package_name
# List installed packages
apt list --installed
dpkg -l
CentOS/RHEL (YUM/DNF)
# Update system
yum update
dnf update
# Install packages
yum install package_name
dnf install package_name
# Remove packages
yum remove package_name
dnf remove package_name
# Search packages
yum search keyword
dnf search keyword
# List packages
yum list installed
dnf list installed
Network Commands
# Network interfaces
ip addr show
ip link show
ifconfig
# Routing table
ip route show
route -n
netstat -rn
# Network connectivity
ping google.com
ping -c 5 8.8.8.8
traceroute google.com
mtr google.com
# DNS lookup
nslookup domain.com
dig domain.com
host domain.com
# Port scanning
netstat -tulpn
ss -tulpn
lsof -i :80
# Download files
wget https://example.com/file.zip
curl -O https://example.com/file.zip
curl -L https://bit.ly/shorturl
Service Management (systemd)
# Service control
systemctl start service_name
systemctl stop service_name
systemctl restart service_name
systemctl reload service_name
systemctl status service_name
# Enable/disable services
systemctl enable service_name
systemctl disable service_name
# List services
systemctl list-units --type=service
systemctl list-unit-files --type=service
# View logs
journalctl -u service_name
journalctl -f
journalctl --since "1 hour ago"
journalctl --since "2024-01-01"
Disk Operations
# Disk usage
df -h
du -sh /path/*
du -h --max-depth=1 /
# Mount/unmount
mount /dev/sdb1 /mnt/disk
umount /mnt/disk
mount -a # Mount all in fstab
# Partition management
fdisk -l
parted -l
lsblk -f
# Create filesystem
mkfs.ext4 /dev/sdb1
mkfs.xfs /dev/sdb1
# Check filesystem
fsck /dev/sdb1
e2fsck -f /dev/sdb1
Archiving and Compression
# tar archives
tar -czf archive.tar.gz /path/to/directory
tar -xzf archive.tar.gz
tar -tzf archive.tar.gz # List contents
tar -xzf archive.tar.gz -C /destination/
# zip archives
zip -r archive.zip directory/
unzip archive.zip
unzip -l archive.zip # List contents
# gzip compression
gzip file.txt
gunzip file.txt.gz
# bzip2 compression
bzip2 file.txt
bunzip2 file.txt.bz2
Text Processing
# sed - Stream editor
sed 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt
sed '/pattern/d' file.txt
# awk - Pattern scanning
awk '{print $1}' file.txt
awk -F: '{print $1,$3}' /etc/passwd
# cut - Extract columns
cut -d: -f1 /etc/passwd
cut -c1-10 file.txt
# sort and unique
sort file.txt
sort -r file.txt
sort -n file.txt
uniq file.txt
sort file.txt | uniq -c
Conclusion
These essential Linux commands form the foundation of system administration. Practice these commands regularly to become proficient. VCCLHOSTING provides comprehensive support for all Linux distributions on our servers.