Please wait
Please wait
Comprehensive Linux command line reference covering file operations, system administration, networking, text processing, process management, and shell scripting. Essential Linux commands and examples for developers and system administrators.
Basic navigation commands for moving through directories
navigationNavigation: Commands to move through the directory structure and view current location
# Current directory
pwd # Print working directory
cd # Go to home directory
cd ~ # Go to home directory
cd / # Go to root directory
cd .. # Go up one directory
cd - # Go to previous directory# Directory listing
ls # List files in current directory
ls -l # Long format (detailed)
ls -a # Show hidden files
ls -la # Long format with hidden files
ls -lh # Human-readable file sizes
ls -lt # Sorted by modification time
ls -R # Recursive listing# Directory operations
mkdir dirname # Create directory
mkdir -p path/to/dir # Create nested directories
rmdir dirname # Remove empty directory
rm -rf dirname # Remove directory and contents (careful!)Commands to view and read file contents
file-operationsViewing Files: Commands to display file contents in different ways
# View entire file
cat filename # Display entire file
cat file1 file2 # Concatenate multiple files
cat > newfile # Create file (Ctrl+D to save)# View file page by page
less filename # View file (scroll with arrows, q to quit)
more filename # View file (space for next page, q to quit)
head filename # First 10 lines
head -n 20 filename # First 20 lines
tail filename # Last 10 lines
tail -n 20 filename # Last 20 lines# Real-time file monitoring
tail -f filename # Follow file (watch updates in real-time)
tail -F filename # Follow with retry (useful for log rotation)Commands to create and edit files
file-operationsCreating Files: Various methods to create new files
# Create empty file
touch filename # Create empty file or update timestamp
touch file1 file2 # Create multiple files
touch -t 202401011200 file # Set specific timestamp# Create file with content
echo "text" > file # Create/overwrite file
echo "text" >> file # Append to file
cat > file # Type content (Ctrl+D to save)
cat >> file # Append content (Ctrl+D to save)
printf "text\n" > file # Formatted output# Text editors
nano filename # Simple editor (Ctrl+X to exit)
vim filename # Vim editor (:wq to save and quit)
vi filename # Vi editor (same as vim)
gedit filename # GUI editor (if available)Commands to copy, move, and rename files
file-operationsCopying and Moving: Commands to duplicate and relocate files
# Copy files
cp source dest # Copy file
cp file1 file2 dir/ # Copy multiple files to directory
cp -r dir1 dir2 # Copy directory recursively
cp -p file dest # Preserve permissions and timestamps
cp -a source dest # Archive mode (preserve everything)# Move and rename
mv oldname newname # Rename file
mv file dir/ # Move file to directory
mv file1 file2 dir/ # Move multiple files
mv -i file dest # Interactive (prompt before overwrite)
mv -b file dest # Backup existing fileCommands to delete files and find files
file-operationsDeleting Files: Commands to remove files and directories
# Delete files
rm filename # Delete file
rm -f filename # Force delete (no prompt)
rm -i filename # Interactive (prompt before delete)
rm -r dirname # Delete directory recursively
rm -rf dirname # Force delete directory (dangerous!)# Find files
find . -name "*.txt" # Find files by name
find . -type f -name "*.log" # Find files only
find . -type d -name "dir" # Find directories only
find /home -user username # Find files by owner
find . -size +100M # Find files larger than 100MB
find . -mtime -7 # Find files modified in last 7 days# Locate command (faster, uses database)
locate filename # Find file (requires updatedb)
updatedb # Update locate database
locate -i "*.txt" # Case-insensitive searchUnderstanding and modifying file permissions
permissionsPermissions: Linux uses read (r), write (w), and execute (x) permissions for owner, group, and others
# View permissions
ls -l # Long format shows permissions
stat filename # Detailed file information
# Permission format: -rwxrwxrwx
# First char: file type (- = file, d = directory)
# Next 3: owner permissions (rwx)
# Next 3: group permissions (rwx)
# Last 3: others permissions (rwx)# Change permissions (numeric)
chmod 755 filename # rwxr-xr-x (owner: all, group/others: read+execute)
chmod 644 filename # rw-r--r-- (owner: read+write, others: read)
chmod 777 filename # rwxrwxrwx (all permissions for all)
chmod -R 755 dir/ # Recursive permission change# Change permissions (symbolic)
chmod u+x filename # Add execute for owner
chmod g-w filename # Remove write for group
chmod o+r filename # Add read for others
chmod a+x filename # Add execute for all (a = all)
chmod u=rwx,g=rx,o=r filename # Set specific permissions# Change ownership
chown user:group file # Change owner and group
chown user file # Change owner only
chown -R user:group dir/ # Recursive ownership change
chgrp group file # Change group onlySearching and manipulating text
text-processingText Search: grep searches for patterns in files
# grep - search for patterns
grep "pattern" file # Search for pattern in file
grep -i "pattern" file # Case-insensitive search
grep -r "pattern" dir/ # Recursive search
grep -n "pattern" file # Show line numbers
grep -v "pattern" file # Invert match (show non-matching lines)
grep -E "pattern" file # Extended regex# sed - stream editor
sed 's/old/new/g' file # Replace all occurrences
sed 's/old/new/' file # Replace first occurrence per line
sed -i 's/old/new/g' file # Edit file in place
sed '2d' file # Delete line 2
sed '2,5d' file # Delete lines 2-5
sed '/pattern/d' file # Delete lines matching pattern# awk - pattern scanning and processing
awk '{print $1}' file # Print first column
awk -F: '{print $1}' file # Use : as field separator
awk '/pattern/ {print}' file # Print lines matching pattern
awk '{sum+=$1} END {print sum}' file # Sum first columnExtracting, sorting, and filtering text
text-processingText Manipulation: Commands to extract, sort, and filter text data
# cut - extract columns
cut -d: -f1 file # Extract first field (colon delimiter)
cut -c1-10 file # Extract characters 1-10
cut -f1,3 file # Extract fields 1 and 3 (tab delimiter)# sort - sort lines
sort file # Sort alphabetically
sort -n file # Sort numerically
sort -r file # Reverse sort
sort -k2 file # Sort by second field
sort -u file # Unique sort (remove duplicates)# uniq - remove duplicates
uniq file # Remove consecutive duplicates
uniq -c file # Count occurrences
uniq -d file # Show only duplicates
uniq -u file # Show only unique lines# wc - word count
wc file # Lines, words, characters
wc -l file # Line count only
wc -w file # Word count only
wc -c file # Character count onlyCommands to view and monitor running processes
process-managementProcess Monitoring: Commands to view running processes and their status
# ps - process status
ps # Current user's processes
ps aux # All processes (detailed)
ps -ef # All processes (full format)
ps aux | grep process # Find specific process
ps -p PID # Process by PID# top - real-time process monitor
top # Interactive process viewer
top -u username # Processes for specific user
# Press 'q' to quit, 'k' to kill process
# Press 'M' to sort by memory, 'P' to sort by CPU# htop - enhanced top (if installed)
htop # Better interface than top
# F5: tree view, F6: sort, F9: kill process
# Other monitoring tools
pgrep process_name # Find process ID by name
pidof process_name # Find PID of process
pstree # Show process treeCommands to start, stop, and manage processes
process-managementProcess Control: Commands to start, stop, and manage processes
# Running processes
command & # Run in background
nohup command & # Run in background (survives logout)
command > output.log 2>&1 & # Redirect output and run in background# Killing processes
kill PID # Terminate process (SIGTERM)
kill -9 PID # Force kill (SIGKILL)
killall process_name # Kill all processes by name
pkill process_name # Kill processes by name pattern
kill -HUP PID # Reload process (SIGHUP)# Job control
jobs # List background jobs
fg %1 # Bring job 1 to foreground
bg %1 # Send job 1 to background
Ctrl+Z # Suspend current process
Ctrl+C # Terminate current processCommands to view system information and resources
system-infoSystem Info: Commands to check system status and resources
# System information
uname -a # All system information
uname -r # Kernel version
hostname # System hostname
uptime # System uptime and load
whoami # Current username# Resource usage
free -h # Memory usage (human-readable)
df -h # Disk space (human-readable)
du -h dir/ # Directory size (human-readable)
du -sh dir/ # Total directory size
iostat # I/O statistics (if installed)# CPU information
lscpu # CPU architecture information
cat /proc/cpuinfo # Detailed CPU info
nproc # Number of CPU cores
top # Real-time CPU usageBasic network commands for connectivity and information
networkingNetwork Basics: Commands to check connectivity and network configuration
# Network configuration
ifconfig # Network interfaces (deprecated, use ip)
ip addr # Show IP addresses
ip link # Show network interfaces
hostname -I # Show IP address# Connectivity testing
ping hostname # Test connectivity
ping -c 4 hostname # Send 4 packets
traceroute hostname # Trace route to host
mtr hostname # Continuous traceroute (if installed)# Network connections
netstat -tulpn # All listening ports
ss -tulpn # Modern replacement for netstat
lsof -i # List open network connections
lsof -i :80 # Connections on port 80Commands to download files and transfer data
networkingFile Transfer: Commands to download files and transfer data over network
# wget - download files
wget URL # Download file
wget -O filename URL # Save with specific name
wget -c URL # Continue interrupted download
wget -r URL # Recursive download# curl - transfer data
curl URL # Download and display
curl -O URL # Download file
curl -o filename URL # Save with specific name
curl -L URL # Follow redirects
curl -I URL # Show headers only# scp - secure copy
scp file user@host:/path # Copy to remote
scp user@host:/path file # Copy from remote
scp -r dir user@host:/path # Copy directory recursively
# rsync - efficient file transfer
rsync -av source dest # Archive mode, verbose
rsync -avz source dest # With compression
rsync -av --delete source dest # Mirror (delete extra files)Commands to compress and archive files
compressionCompression: Commands to compress and decompress files
# tar - archive files
tar -cf archive.tar files # Create archive
tar -xf archive.tar # Extract archive
tar -czf archive.tar.gz dir/ # Create gzipped archive
tar -xzf archive.tar.gz # Extract gzipped archive
tar -tf archive.tar # List archive contents# gzip/gunzip
gzip file # Compress file (creates file.gz)
gunzip file.gz # Decompress file
gzip -d file.gz # Decompress (alternative)
zcat file.gz # View compressed file# zip/unzip
zip archive.zip files # Create zip archive
unzip archive.zip # Extract zip archive
unzip -l archive.zip # List zip contents
zip -r archive.zip dir/ # Recursive zipAPT package manager commands
package-managementAPT: Advanced Package Tool for Debian/Ubuntu systems
# Update package lists
sudo apt update # Refresh package database
sudo apt upgrade # Upgrade all packages
sudo apt full-upgrade # Full system upgrade# Install packages
sudo apt install package # Install package
sudo apt install pkg1 pkg2 # Install multiple packages
sudo apt install -y package # Auto-confirm installation# Remove packages
sudo apt remove package # Remove package
sudo apt purge package # Remove package and config files
sudo apt autoremove # Remove unused dependencies# Search and info
apt search keyword # Search for packages
apt show package # Show package information
apt list --installed # List installed packages
apt list --upgradable # List upgradable packagesYUM and DNF package manager commands
package-managementYUM/DNF: Package managers for Red Hat-based systems
# Update packages
sudo yum update # Update all packages (YUM)
sudo dnf update # Update all packages (DNF)
sudo yum check-update # Check for updates# Install packages
sudo yum install package # Install package
sudo dnf install package # Install package (DNF)
sudo yum install -y package # Auto-confirm# Remove packages
sudo yum remove package # Remove package
sudo yum erase package # Remove package (alternative)
sudo dnf remove package # Remove package (DNF)# Search and info
yum search keyword # Search for packages
yum info package # Show package information
yum list installed # List installed packages
yum list available # List available packagesCommands to manage users and groups
user-managementUser Management: Commands to create, modify, and manage user accounts
# User operations
sudo useradd username # Create new user
sudo useradd -m username # Create user with home directory
sudo userdel username # Delete user
sudo userdel -r username # Delete user and home directory
sudo usermod -aG group user # Add user to group# Password management
passwd # Change own password
sudo passwd username # Change user's password
sudo passwd -l username # Lock user account
sudo passwd -u username # Unlock user account# Group operations
sudo groupadd groupname # Create new group
sudo groupdel groupname # Delete group
groups # Show current user's groups
id # Show user and group IDs
id username # Show user's IDsBasic shell scripting syntax and concepts
shell-scriptingScript Basics: Essential syntax for writing shell scripts
# Shebang and variables
#!/bin/bash # Shebang line
VAR="value" # Variable assignment
echo $VAR # Print variable
echo ${VAR} # Alternative syntax
readonly VAR="value" # Read-only variable# Special variables
$0 # Script name
$1, $2, $3... # Command line arguments
$# # Number of arguments
$@ # All arguments
$* # All arguments (as single string)
$? # Exit status of last command
$$ # Process ID# Conditional statements
if [ condition ]; then
commands
fi
if [ condition ]; then
commands
else
commands
fi
if [ condition ]; then
commands
elif [ condition ]; then
commands
else
commands
fiLooping constructs and function definitions
shell-scriptingLoops: Iteration constructs in shell scripts
# For loop
for i in 1 2 3; do
echo $i
done
for i in {1..10}; do
echo $i
done
for file in *.txt; do
echo $file
done# While loop
while [ condition ]; do
commands
done
# Until loop
until [ condition ]; do
commands
done# Functions
function_name() {
commands
return value
}
# Call function
function_name
# Function with parameters
myfunc() {
echo "First: $1"
echo "Second: $2"
}
myfunc arg1 arg2Redirecting input/output and using pipes
io-redirectionRedirection: Controlling where command input and output go
# Output redirection
command > file # Overwrite file
command >> file # Append to file
command 2> file # Redirect stderr
command > file 2>&1 # Redirect both stdout and stderr
command &> file # Redirect both (bash 4+)# Input redirection
command < file # Read from file
command << EOF # Here document
text
EOF
command <<< "text" # Here string# Pipes
command1 | command2 # Pipe output to next command
command1 | command2 | command3 # Chain pipes
ls -l | grep ".txt" # List files, filter .txt files
ps aux | grep process # Find processCommon environment variables and their usage
environmentEnvironment Variables: System and user environment variables
# View variables
env # Show all environment variables
printenv # Show all environment variables
echo $HOME # Show specific variable
echo $PATH # Show PATH variable
set # Show all variables (including shell)# Set variables
export VAR="value" # Set and export variable
VAR="value" # Set variable (current shell only)
export PATH=$PATH:/new/path # Append to PATH# Common variables
$HOME # Home directory
$PATH # Executable search path
$USER # Current username
$SHELL # Current shell
$PWD # Current directory
$OLDPWD # Previous directoryManaging system services with systemctl
servicessystemd: Modern system and service manager
# Service status
systemctl status service # Check service status
systemctl is-active service # Check if active
systemctl is-enabled service # Check if enabled# Control services
sudo systemctl start service # Start service
sudo systemctl stop service # Stop service
sudo systemctl restart service # Restart service
sudo systemctl reload service # Reload configuration# Enable/disable services
sudo systemctl enable service # Enable on boot
sudo systemctl disable service # Disable on boot
sudo systemctl enable --now service # Enable and start# List services
systemctl list-units --type=service # List all services
systemctl list-unit-files # List all unit files
systemctl --failed # List failed servicesViewing and monitoring system logs
loggingLog Files: System logs and how to view them
# Common log locations
/var/log/syslog # System log (Debian/Ubuntu)
/var/log/messages # System messages (RHEL/CentOS)
/var/log/auth.log # Authentication log
/var/log/kern.log # Kernel log
/var/log/apache2/ # Apache logs (if installed)
/var/log/nginx/ # Nginx logs (if installed)# journalctl - systemd journal
journalctl # View all logs
journalctl -u service # Logs for specific service
journalctl -f # Follow logs (like tail -f)
journalctl -n 50 # Last 50 entries
journalctl --since today # Since today
journalctl --since "1 hour ago"# View and filter logs
tail -f /var/log/syslog # Follow system log
grep "error" /var/log/syslog # Search for errors
less /var/log/syslog # View log file