Whether you’re navigating servers, managing files, or debugging applications, Unix commands are essential tools. Here’s a curated list of commands that will make you more productive.
File Navigation
pwd - Print Working Directory
Always know where you are:
1
2
|
pwd
# /home/user/projects
|
cd - Change Directory
1
2
3
4
|
cd /var/log # Absolute path
cd ../ # Parent directory
cd - # Previous directory
cd ~ # Home directory
|
ls - List Directory Contents
1
2
3
4
5
|
ls # Basic listing
ls -la # Long format, including hidden files
ls -lh # Human-readable file sizes
ls -lt # Sort by modification time
ls -lS # Sort by size
|
File Operations
cp - Copy Files
1
2
3
|
cp file.txt backup.txt # Copy file
cp -r directory/ backup/ # Copy directory recursively
cp -p file.txt backup.txt # Preserve permissions and timestamps
|
mv - Move or Rename
1
2
3
|
mv old.txt new.txt # Rename
mv file.txt /other/directory/ # Move
mv *.log /var/log/ # Move multiple files
|
rm - Remove Files
1
2
3
|
rm file.txt # Remove file
rm -r directory/ # Remove directory recursively
rm -i file.txt # Interactive (confirm before delete)
|
Warning: rm -rf is dangerous. Always double-check your path.
mkdir - Create Directories
1
2
|
mkdir new_folder # Create directory
mkdir -p path/to/nested/folder # Create nested directories
|
Viewing File Contents
cat - Concatenate and Display
1
2
3
|
cat file.txt # Display entire file
cat file1.txt file2.txt # Display multiple files
cat -n file.txt # Show line numbers
|
less - Page Through Files
1
2
|
less large_file.log # View file with pagination
# Use: j/k to scroll, /pattern to search, q to quit
|
head and tail
1
2
3
|
head -n 20 file.txt # First 20 lines
tail -n 20 file.txt # Last 20 lines
tail -f /var/log/app.log # Follow file (live updates)
|
Searching
grep - Search Text Patterns
1
2
3
4
5
6
|
grep "error" log.txt # Find lines containing "error"
grep -i "error" log.txt # Case insensitive
grep -r "TODO" ./src # Recursive search in directory
grep -n "function" script.js # Show line numbers
grep -v "debug" log.txt # Invert match (exclude lines)
grep -E "error|warning" log.txt # Extended regex (OR)
|
find - Search for Files
1
2
3
4
|
find . -name "*.js" # Find by name pattern
find . -type f -mtime -7 # Files modified in last 7 days
find . -size +100M # Files larger than 100MB
find . -name "*.tmp" -delete # Find and delete
|
Text Processing
sed - Stream Editor
1
2
3
4
|
sed 's/old/new/' file.txt # Replace first occurrence per line
sed 's/old/new/g' file.txt # Replace all occurrences
sed -i 's/old/new/g' file.txt # Edit file in place
sed -n '10,20p' file.txt # Print lines 10-20
|
awk - Pattern Processing
1
2
3
4
|
awk '{print $1}' file.txt # Print first column
awk -F',' '{print $2}' data.csv # Use comma as delimiter
awk '/error/ {print}' log.txt # Print lines matching pattern
awk '{sum+=$1} END {print sum}' # Sum first column
|
sort and uniq
1
2
3
4
5
|
sort file.txt # Sort alphabetically
sort -n numbers.txt # Sort numerically
sort -r file.txt # Reverse sort
sort file.txt | uniq # Remove duplicates
sort file.txt | uniq -c # Count occurrences
|
Process Management
ps - Process Status
1
2
|
ps aux # All processes, detailed
ps aux | grep nginx # Find specific process
|
top and htop
1
2
|
top # Real-time process viewer
htop # Enhanced version (if installed)
|
kill - Terminate Processes
1
2
3
|
kill 1234 # Send SIGTERM to PID 1234
kill -9 1234 # Force kill (SIGKILL)
killall nginx # Kill all processes by name
|
Disk and Memory
df - Disk Free
1
2
|
df -h # Disk usage, human-readable
df -h /home # Specific filesystem
|
du - Disk Usage
1
2
3
|
du -sh directory/ # Total size of directory
du -sh */ | sort -h # Size of subdirectories, sorted
du -ah . | sort -rh | head -20 # Top 20 largest files
|
free - Memory Usage
1
|
free -h # Memory usage, human-readable
|
Networking
curl - Transfer Data
1
2
3
4
|
curl https://api.example.com # GET request
curl -I https://example.com # Headers only
curl -X POST -d "data" url # POST request
curl -o file.zip https://url # Download to file
|
netstat and ss
1
2
|
netstat -tuln # Listening ports
ss -tuln # Modern alternative
|
ping and traceroute
1
2
|
ping google.com # Test connectivity
traceroute google.com # Trace network path
|
Permissions
chmod - Change Mode
1
2
3
|
chmod 755 script.sh # rwxr-xr-x
chmod +x script.sh # Add execute permission
chmod -R 644 directory/ # Recursive
|
chown - Change Owner
1
2
|
chown user:group file.txt # Change owner and group
chown -R user:group directory/ # Recursive
|
Compression
tar - Archive Files
1
2
3
4
|
tar -cvf archive.tar directory/ # Create archive
tar -xvf archive.tar # Extract archive
tar -czvf archive.tar.gz dir/ # Create gzipped archive
tar -xzvf archive.tar.gz # Extract gzipped archive
|
gzip and gunzip
1
2
|
gzip file.txt # Compress (creates file.txt.gz)
gunzip file.txt.gz # Decompress
|
Useful Combinations
Count lines of code
1
|
find . -name "*.py" | xargs wc -l
|
Find and replace in multiple files
1
|
find . -name "*.js" -exec sed -i 's/old/new/g' {} +
|
Monitor log file for errors
1
|
tail -f /var/log/app.log | grep --line-buffered "ERROR"
|
Disk usage by directory, sorted
1
|
du -sh */ 2>/dev/null | sort -hr
|
Conclusion
These commands form the foundation of Unix proficiency. Practice them regularly, and they’ll become second nature. The real power comes from combining them with pipes (|) and redirection (>, >>, <).
Comments