Linux Command Line Mastery: Essential Commands Every User Should Know

Introduction

The Linux command line is a powerful interface that provides direct access to your system’s capabilities. This comprehensive guide covers essential commands, advanced techniques, and productivity tips to help you become proficient with the Linux terminal.

Getting Started with the Terminal

The terminal (also called shell or command line) is a text-based interface where you type commands to interact with your Linux system. Most Linux distributions use Bash (Bourne Again Shell) as the default shell.

Opening the Terminal

  • Keyboard shortcut: Ctrl+Alt+T (most distributions)
  • Applications menu: Search for “Terminal” or “Console”
  • Right-click desktop: “Open Terminal Here” (some environments)

Basic Navigation Commands

Directory Operations

# Show current directory
pwd

# List directory contents
ls
ls -l    # Detailed list
ls -la   # Include hidden files
ls -lh   # Human-readable file sizes

# Change directory
cd /path/to/directory
cd ~     # Go to home directory
cd ..    # Go up one directory
cd -     # Go to previous directory

# Create directories
mkdir new_directory
mkdir -p path/to/nested/directory

# Remove directories
rmdir empty_directory
rm -rf directory_with_contents

File Operations

# Create files
touch new_file.txt
echo "Hello World" > file.txt

# Copy files
cp source.txt destination.txt
cp -r source_dir/ destination_dir/

# Move/rename files
mv old_name.txt new_name.txt
mv file.txt /path/to/new/location/

# Remove files
rm file.txt
rm -f file.txt    # Force removal
rm -i file.txt    # Interactive removal

Text Processing Commands

Viewing File Contents

# Display entire file
cat file.txt

# Display file with line numbers
cat -n file.txt

# View file page by page
less file.txt
more file.txt

# Display first/last lines
head file.txt       # First 10 lines
head -n 5 file.txt  # First 5 lines
tail file.txt       # Last 10 lines
tail -f file.txt    # Follow file changes

Text Search and Manipulation

# Search for text
grep "pattern" file.txt
grep -i "pattern" file.txt    # Case insensitive
grep -r "pattern" directory/  # Recursive search
grep -n "pattern" file.txt    # Show line numbers

# Replace text
sed 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt  # In-place editing

# Sort and unique
sort file.txt
sort -n numbers.txt    # Numeric sort
uniq sorted_file.txt   # Remove duplicates

# Count lines, words, characters
wc file.txt
wc -l file.txt    # Count lines only

System Information Commands

System Status

# System information
uname -a          # System information
hostnamectl       # Hostname and system info
uptime           # System uptime and load
whoami           # Current username
id               # User and group IDs

# Process information
ps aux           # All running processes
top              # Real-time process viewer
htop             # Enhanced process viewer
jobs             # Current shell jobs

Hardware and Resources

# Memory usage
free -h          # Memory usage (human readable)
cat /proc/meminfo

# Disk usage
df -h            # Disk space usage
du -h directory/ # Directory size
du -sh *         # Size of each item

# CPU information
lscpu
cat /proc/cpuinfo

# Hardware information
lshw             # Detailed hardware info
lsblk            # Block devices
lsusb            # USB devices
lspci            # PCI devices

File Permissions and Ownership

Permission Commands

# View permissions
ls -l file.txt

# Change permissions
chmod 755 file.txt
chmod u+x script.sh
chmod go-w file.txt

# Change ownership
chown user:group file.txt
chgrp group file.txt

Network Commands

Network Information

# Network interfaces
ip addr show
ifconfig         # Legacy command

# Network connectivity
ping google.com
ping -c 4 8.8.8.8

# Network statistics
netstat -tuln    # Listening ports
ss -tuln         # Modern alternative to netstat

# Download files
wget https://example.com/file.zip
curl -O https://example.com/file.zip

Archive and Compression

Tar Archives

# Create archive
tar -cvf archive.tar directory/
tar -czvf archive.tar.gz directory/  # With compression

# Extract archive
tar -xvf archive.tar
tar -xzvf archive.tar.gz

# List archive contents
tar -tvf archive.tar

Other Compression Tools

# Zip files
zip -r archive.zip directory/
unzip archive.zip

# Gzip compression
gzip file.txt
gunzip file.txt.gz

Advanced Command Techniques

Pipes and Redirection

# Pipe output to another command
ls -l | grep ".txt"
ps aux | grep apache

# Redirect output
command > output.txt      # Overwrite file
command >> output.txt     # Append to file
command 2> errors.txt     # Redirect errors
command &> all_output.txt # Redirect all output

# Input redirection
command < input.txt

Command Chaining

# Run commands sequentially
command1; command2; command3

# Run if previous succeeds
command1 && command2

# Run if previous fails
command1 || command2

# Background processes
long_command &

Text Editors

Nano (Beginner-friendly)

# Edit file
nano file.txt

# Common shortcuts:
# Ctrl+O - Save
# Ctrl+X - Exit
# Ctrl+G - Help

Vim (Advanced)

# Edit file
vim file.txt

# Basic vim commands:
# i - Insert mode
# Esc - Command mode
# :w - Save
# :q - Quit
# :wq - Save and quit

Package Management

Debian/Ubuntu (APT)

# Update package lists
sudo apt update

# Upgrade packages
sudo apt upgrade

# Install package
sudo apt install package_name

# Remove package
sudo apt remove package_name

# Search packages
apt search keyword

Red Hat/CentOS (YUM/DNF)

# Install package
sudo yum install package_name
sudo dnf install package_name

# Update packages
sudo yum update
sudo dnf update

# Remove package
sudo yum remove package_name

Environment Variables

Working with Variables

# View environment variables
env
printenv

# View specific variable
echo $PATH
echo $HOME

# Set temporary variable
export MY_VAR="value"

# Add to PATH
export PATH="$PATH:/new/path"

History and Shortcuts

Command History

# View command history
history

# Execute previous command
!!

# Execute specific history item
!123

# Search history
Ctrl+R (then type search term)

# Clear history
history -c

Useful Shortcuts

# Terminal shortcuts:
# Ctrl+C - Interrupt command
# Ctrl+Z - Suspend command
# Ctrl+D - End of input/logout
# Ctrl+L - Clear screen
# Tab - Auto-complete
# Ctrl+A - Beginning of line
# Ctrl+E - End of line

Finding Files and Directories

Find Command

# Find by name
find /path -name "filename"
find . -name "*.txt"

# Find by type
find /path -type f    # Files only
find /path -type d    # Directories only

# Find by size
find /path -size +100M    # Larger than 100MB
find /path -size -1M      # Smaller than 1MB

# Find by modification time
find /path -mtime -7      # Modified in last 7 days
find /path -mtime +30     # Modified more than 30 days ago

Locate Command

# Update locate database
sudo updatedb

# Find files quickly
locate filename
locate "*.pdf"

System Services

Systemd Commands

# Service management
sudo systemctl start service_name
sudo systemctl stop service_name
sudo systemctl restart service_name
sudo systemctl reload service_name

# Service status
systemctl status service_name
systemctl is-active service_name

# Enable/disable services
sudo systemctl enable service_name
sudo systemctl disable service_name

# List services
systemctl list-units --type=service

Productivity Tips

Aliases

# Create temporary alias
alias ll="ls -la"
alias grep="grep --color=auto"

# Add permanent aliases to ~/.bashrc
echo 'alias ll="ls -la"' >> ~/.bashrc
source ~/.bashrc

Command Substitution

# Use command output as argument
echo "Today is $(date)"
cd $(dirname $0)

# Backticks (older syntax)
echo "Files: `ls | wc -l`"

Security Commands

User Management

# Switch user
su username
sudo -u username command

# View logged-in users
who
w
last

# Password management
passwd           # Change own password
sudo passwd user # Change user's password

Troubleshooting Commands

System Diagnosis

# Check system logs
journalctl -f           # Follow system log
journalctl -u service   # Service-specific logs
dmesg                   # Kernel messages

# Check file systems
fsck /dev/sda1          # Check file system
mount                   # Show mounted filesystems
lsof                    # List open files

Best Practices

  1. Use tab completion to avoid typos and save time
  2. Read man pages for detailed command information
  3. Be careful with rm – there’s no recycle bin
  4. Use sudo responsibly – understand what you’re doing
  5. Keep backups before making system changes
  6. Learn keyboard shortcuts for efficiency
  7. Practice regularly to build muscle memory

Conclusion

Mastering the Linux command line takes practice, but these essential commands provide a solid foundation. Start with basic file operations, gradually incorporate advanced techniques, and remember that the man pages (man command_name) are your best resource for detailed information.

The command line offers power, flexibility, and efficiency that graphical interfaces often cannot match. With consistent practice, you’ll find yourself becoming more productive and confident in managing Linux systems.

Add Comment