Linux is the foundation of modern web infrastructure. Most servers in the world run on Linux, so knowing basic commands is essential for every web developer. In this article, we'll cover the most important commands you'll use daily.
File System Navigation
First of all, you need to learn how to navigate the file system. Here are the basic commands:
# Show current directory
pwd
# Change directory
cd /var/www/html
# Go up one level
cd ..
# Go to home directory
cd ~
# List directory contents
ls -la
Tip from an experienced admin: use Tab for path autocompletion - it saves a lot of time and prevents errors.
— Alex, DevOps engineer with 10 years of experience
Working with Files and Directories
Creating, copying, moving, and deleting files - basic operations you'll perform constantly:
# Create directory
mkdir my_project
mkdir -p path/to/nested/directory
# Create file
touch index.php
# Copy file
cp source.txt destination.txt
cp -r source_dir/ destination_dir/
# Move/rename
mv old_name.txt new_name.txt
# Delete file
rm file.txt
# Delete directory with contents
rm -rf directory/
rm -rf command deletes files permanently! Always double-check the path before executing.
Viewing File Contents
# Output entire file
cat config.php
# First 20 lines
head -20 access.log
# Last 50 lines
tail -50 error.log
# Follow file in real-time
tail -f /var/log/nginx/access.log
# Page-by-page view
less large_file.log
File Permissions
Understanding file permissions is critical for your server's security:
# Change file owner
chown www-data:www-data file.php
# Recursively change owner
chown -R www-data:www-data /var/www/html
# Set permissions
chmod 755 script.sh
chmod 644 config.php
# Recursively for directories
find /var/www -type d -exec chmod 755 {} \;
find /var/www -type f -exec chmod 644 {} \;
Finding Files and Text
# Find file by name
find /var/www -name "*.php"
# Find files modified in the last hour
find . -mmin -60
# Search text in files
grep -r "function getProducts" /var/www/html
# Search with line numbers
grep -rn "error" /var/log/
Process Management
Monitoring and managing processes helps maintain server health:
# Show all processes
ps aux
# Interactive process monitor
htop
# Find process by name
ps aux | grep nginx
# Kill process
kill 1234
kill -9 1234 # force
# Restart service
sudo systemctl restart nginx
sudo systemctl restart php8.2-fpm
Network Commands
# Check connection
ping google.com
# Check open ports
netstat -tulpn
ss -tulpn
# Download file
wget https://example.com/file.zip
curl -O https://example.com/file.zip
# Check DNS
nslookup example.com
dig example.com
Useful Combinations
The power of Linux is revealed in combining commands:
# Find large files
du -h /var/www | sort -rh | head -20
# Count lines of code in project
find . -name "*.php" | xargs wc -l
# Find and replace text in all files
find . -name "*.php" -exec sed -i 's/old_text/new_text/g' {} \;
# Archive project
tar -czvf backup.tar.gz /var/www/html
# Monitor disk usage
df -h
du -sh /var/www/*
The command line may seem complex at first, but with practice, it will become your most powerful tool. Start with basic commands and gradually expand your arsenal.
— Linus Torvalds
Conclusion
These commands cover 90% of a web developer's daily tasks. I recommend creating a cheat sheet and practicing daily. Over time, these commands will become second nature, and you'll be able to perform complex operations in seconds.