Basic commands

This guide covers essential Linux commands that every user should know.

πŸ“‚ File and Directory Navigation

Directory Navigation

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

Listing Files and Directories

ls                      # List files and directories
ls -l                   # Long format listing
ls -a                   # Show hidden files
ls -lh                  # Human-readable file sizes
ls -R                   # Recursive listing
ls -lt                  # Sort by modification time

File Operations

touch file.txt          # Create empty file
mkdir directory         # Create directory
mkdir -p dir1/dir2     # Create parent directories
rm file.txt            # Remove file
rm -r directory        # Remove directory and contents
rm -f file.txt         # Force remove file
cp source dest         # Copy file
mv source dest         # Move/rename file

πŸ“ File Viewing and Editing

View File Contents

cat file.txt           # Display entire file
less file.txt          # View file with pagination
head file.txt          # Show first 10 lines
head -n 20 file.txt    # Show first 20 lines
tail file.txt          # Show last 10 lines
tail -f file.txt       # Follow file updates

Text Processing

grep pattern file      # Search for pattern
grep -i pattern file   # Case-insensitive search
grep -r pattern dir    # Recursive search
wc file.txt           # Count lines, words, chars
sort file.txt         # Sort lines
uniq file.txt         # Remove duplicate lines

πŸ’» System Information

System Status

date                   # Show current date/time
uptime                 # System uptime
uname -a               # System information
df -h                  # Disk usage
free -h                # Memory usage
top                    # Process monitor
htop                   # Interactive process viewer

User Information

whoami                 # Current username
who                    # Logged-in users
w                      # Who is doing what
id                     # User/group IDs
last                   # Recent logins

πŸ”„ Process Management

Process Control

ps                     # Show processes
ps aux                 # Show all processes
kill PID               # Terminate process
killall process_name   # Kill all matching processes
pgrep pattern          # Find process ID
pkill pattern          # Kill matching processes

🌐 Network Commands

Network Information

ip addr                # Show IP addresses
ping host             # Test connectivity
netstat -tuln         # Show listening ports
ss -tuln              # Modern netstat
curl url              # Transfer data
wget url              # Download files

πŸ’‘ Tips and Tricks

  1. Command History

    history               # Show command history
    !!                   # Repeat last command
    !number              # Run command by history number
    ctrl + r             # Search command history
  2. Keyboard Shortcuts

  • Ctrl + C: Interrupt current process

  • Ctrl + Z: Suspend current process

  • Ctrl + D: Exit current shell

  • Ctrl + L: Clear screen

  • Ctrl + A: Go to beginning of line

  • Ctrl + E: Go to end of line

  1. Command Help

man command          # Manual pages
command --help       # Brief help
info command         # Detailed documentation
whatis command       # One-line description

🎯 Best Practices

  1. Safety First

    • Use -i flag with rm for interactive deletion

    • Make backups before major operations

    • Test commands with sample data first

  2. Efficiency

    • Use tab completion

    • Learn keyboard shortcuts

    • Create aliases for common commands

  3. Organization

    • Keep home directory organized

    • Use meaningful file names

    • Document complex commands

🚫 Common Mistakes to Avoid

  1. Running commands without understanding them

  2. Using rm -rf without careful consideration

  3. Running commands as root unnecessarily

  4. Forgetting to backup important data

  5. Not using command history effectively

Last updated

Was this helpful?