Linux Command Line
Updated: September 10, 2025Categories: Command Line, Linux
Printed from:
Linux Command Line Cheatsheet
Basic Navigation and File Operations
Directory and File Navigation
Bash
123456789101112131415# Print current working directory
pwd
# Change directory
cd /path/to/directory # Absolute path
cd .. # Go up one directory
cd ~ # Go to home directory
cd - # Go to previous directory
# List directory contents
ls # Basic listing
ls -l # Detailed list view
ls -a # Show hidden files
ls -lh # Human-readable file sizes
File and Directory Management
Bash
12345678910111213141516# Create directory
mkdir new_directory
mkdir -p /path/to/nested/directories # Create parent directories if they don't exist
# Remove directory
rmdir empty_directory # Only removes empty directories
rm -r directory # Recursively remove directory and its contents
# Copy files and directories
cp file1 destination/
cp -r directory destination/ # Recursive copy
# Move/Rename files and directories
mv oldname newname # Rename
mv file destination/ # Move file
File Permissions and Ownership
Chmod (Change Mode)
Bash
123456789101112# Permission modes
chmod 755 file # rwxr-xr-x (Owner: read/write/execute, Group/Others: read/execute)
chmod u+x file # Add execute permission for user
chmod go-w file # Remove write permission for group and others
# Common permission values
# 4 = read, 2 = write, 1 = execute
# 7 (4+2+1): read, write, execute
# 6 (4+2): read, write
# 5 (4+1): read, execute
# 3 (2+1): write, execute
Ownership
Bash
123456789# Change file owner
chown user:group file
# Change group ownership
chgrp group file
# Recursively change ownership
chown -R user:group directory
File Viewing and Editing
File Viewing
Bash
123456789101112# View entire file
cat file.txt
# View file with pagination
less file.txt
more file.txt
# View first/last lines
head -n 10 file.txt # First 10 lines
tail -n 5 file.txt # Last 5 lines
tail -f logfile.log # Follow log file in real-time
Text Editors
Bash
123456789101112131415# Nano (beginner-friendly)
nano file.txt # Open/create file
# Shortcuts: Ctrl+X (exit), Ctrl+S (save)
# Vim (powerful, steep learning curve)
vim file.txt # Open/create file
# Modes:
# - Normal mode (Esc)
# - Insert mode (i)
# - Command mode (:)
# Commands:
# :w (save)
# :q (quit)
# :wq (save and quit)
File Searching and Pattern Matching
Find Files
Bash
12345678910# Find by name
find /path -name "filename"
find . -type f -name "*.txt" # Find .txt files in current directory
# Find by size
find /path -size +10M # Files larger than 10MB
# Find by modification time
find /path -mtime -7 # Modified in last 7 days
Grep (Search Text)
Bash
123456789# Search in files
grep "pattern" file.txt
grep -r "pattern" directory/ # Recursive search
grep -i "pattern" file.txt # Case-insensitive
grep -n "pattern" file.txt # Show line numbers
# Complex pattern matching
egrep "(pattern1|pattern2)" file.txt # Regex OR
Process Management
Process Listing
Bash
1234567ps aux # List all processes
top # Real-time process viewer
htop # Interactive process viewer
# Find specific process
ps aux | grep process_name
Process Control
Bash
123456789101112# Terminate processes
kill PID # Terminate process by ID
killall process_name # Terminate all processes with name
# Background/Foreground
bg # Send stopped job to background
fg # Bring background job to foreground
jobs # List background jobs
# Run process immune to hangup
nohup command &
System Information
Bash
123456789101112uname -a # System information
whoami # Current user
id # User and group IDs
uptime # System uptime
# Disk and Memory
df -h # Disk space usage
du -sh directory # Directory size
free -h # Memory usage
lscpu # CPU information
lsblk # Block devices
Network Commands
Bash
12345678910111213ping google.com # Test network connectivity
wget url # Download files
curl url # Transfer data
# SSH and File Transfer
ssh user@host # Remote login
scp file user@host:path # Secure copy
rsync -avz source destination # Efficient file synchronization
# Network Statistics
netstat -tuln # Listening ports
ss -tuln # Socket statistics
Archive and Compression
Bash
123456789101112# Tar Archives
tar -cvf archive.tar files/ # Create tar archive
tar -xvf archive.tar # Extract tar archive
# Gzip Compression
gzip file # Compress
gunzip file.gz # Decompress
# Zip
zip archive.zip files/
unzip archive.zip
Text Processing
Bash
123456sort file.txt # Sort lines
uniq file.txt # Remove duplicate lines
wc -l file.txt # Count lines
cut -d: -f1 file # Cut first field using : delimiter
tr 'A-Z' 'a-z' < file # Convert to lowercase
Input/Output Redirection and Pipes
Bash
123456789# Redirection
command > output.txt # Overwrite file
command >> output.txt # Append to file
command < input.txt # Input from file
# Pipes
command1 | command2 # Output of command1 as input to command2
command | tee file.txt # Display and save output
Environment Variables and Shell Configuration
Bash
12345678# View/Set Variables
echo $PATH
export VARNAME=value
# Configuration Files
~/.bashrc # User-specific bash configuration
~/.bash_profile # Login shell configuration
Package Management
Debian/Ubuntu (apt)
Bash
12345apt update # Update package list
apt upgrade # Upgrade packages
apt install package # Install package
apt remove package # Remove package
Red Hat/CentOS (yum/dnf)
Bash
1234yum update # Update packages
yum install package # Install package
yum remove package # Remove package
Service Management
Bash
1234567# Systemd
systemctl start service
systemctl stop service
systemctl restart service
systemctl status service
systemctl enable service # Start on boot
Cron Jobs and Scheduling
Bash
123456# Edit crontab
crontab -e
# Example: Run script every day at 2:30 AM
30 2 * * * /path/to/script.sh
User and Group Management
Bash
123456789# User Management
useradd username
usermod -aG groupname username
userdel username
# Switch Users
su - username # Switch user
sudo command # Run command with elevated privileges
Troubleshooting Commands
Bash
1234dmesg # Kernel messages
journalctl -xe # System logs
strace command # Trace system calls
Useful Shortcuts
Bash
12345Ctrl+C # Terminate current process
Ctrl+Z # Suspend current process
Ctrl+R # Reverse history search
!! # Repeat last command
Shell Scripting Basics
Bash
1234567#!/bin/bash # Shebang line
variable="value" # Variable assignment
echo $variable # Print variable
if [ condition ]; then # Conditional
# commands
fi
Pro Tips
- Always use
man commandfor detailed documentation - Practice regularly to build muscle memory
- Learn to combine commands efficiently
- Keep security in mind when using powerful commands
Disclaimer: Always be careful with commands that modify system files or have destructive potential.
Continue Learning
Discover more cheatsheets to boost your productivity