Linux commands are the backbone of shell scripting, system administration, and automation—whether you're a beginner or need to troubleshoot efficiently. With a few core commands, you can quickly find files, edit text, manage processes, and optimize system performance. No prior experience is needed, though familiarity with a terminal is helpful. Basic commands like `ls`, `cd`, and `pwd` form the foundation; more advanced tools like `grep`, `awk`, and `sed` enable powerful text processing and scripting.

What You'll Need

  • A Linux system (Ubuntu, Fedora, Debian, or any distro with a terminal) or a Linux virtual machine running in a cloud service (e.g., AWS Lightsail, DigitalOcean).
  • Access to the command line (e.g., via SSH if remote) or a pre-installed terminal emulator (default on Linux desktops).
  • Basic knowledge of keyboard shortcuts (e.g., tab completion, arrow keys for history).
  • A text editor (e.g., `nano`, `vim`, or `gedit`) is optional but useful for writing small scripts.

Step-by-Step: Essential Linux Commands

  1. Open the Terminal
    1. On Linux/macOS: Press Ctrl+Alt+T or search for "Terminal" in your applications menu.
    2. On Windows with WSL (Windows Subsystem for Linux): Open the integrated terminal or launch a distro-specific terminal (e.g., Ubuntu’s "Ubuntu Terminal").
    3. If using SSH: Connect via a terminal emulator (e.g., PuTTY, iTerm, or terminal.app) with the command: ssh username@hostname (Replace username and hostname with your details.)
  2. Navigate the Filesystem
    1. Move between directories:
      • cd .. – Go up one directory level.
      • cd /path/to/directory – Change to a specific directory (e.g., cd ~/Documents).
      • cd ~ – Switch to your home directory.
    2. List files and folders:
      • ls – Show current directory contents (default: long listing).
      • ls -l – Long format (permissions, owner, size, timestamp).
      • ls -a – Include hidden files (starting with .).
    3. Check your current directory: pwd (Prints the full path, e.g., /home/username/Desktop.)
  3. Search and Filter Files
    1. Find files by name (case-sensitive by default): find /path/to/search -name "filename" (Replace filename with a pattern like *.txt to match all .txt files.)
    2. Use grep to search text within files:
      • grep "keyword" filename.txt – Search for exact matches.
      • grep -i "keyword" file.log – Case-insensitive search.
      • grep -r "error" /var/log – Recursively search logs in subdirectories.
    3. View file contents:
      • cat file.txt – Display entire file (risky for large files).
      • less file.log – Scrollable view (press q to exit).
  4. Manage Files and Directories
    1. Create a directory: mkdir new_folder (Use mkdir -p parent/child to create nested folders without errors.)
    2. Move or rename files:
      • mv file.txt /backup – Move file to a directory.
      • mv old_name.txt new_name.txt – Rename a file.
    3. Delete files or empty a folder:
      • rm file.txt – Permanently delete a file.
      • rm -r directory/ – Delete a folder and its contents (use with caution!).
      • rmdir empty_folder – Remove an empty folder.
    4. Copy files: cp file.txt /backup/ (Add -r to copy directories recursively.)
  5. Process Management
    1. View running processes: top or htop (install with sudo apt install htop if needed). (Press q to exit.)
    2. Kill a process by PID (Process ID): kill (Use ps aux | grep "process_name" to find the PID first.)
    3. Start a service: sudo systemctl start service_name (Replace service_name with the actual service, e.g., nginx.)
  6. Text Editing with `nano`
    1. Open a file for editing: nano edit_me.txt
    2. Key shortcuts:
      • Ctrl+O – Save changes.
      • Ctrl+X – Exit (unless you have unsaved changes).
      • Ctrl+K – Cut selected text.
      • Ctrl+U – Paste text.

Common Mistakes

  • Running commands as root without `sudo` - Always preface privileged commands with sudo (e.g., sudo rm file.txt). Forgotten sudo can lock you out of critical files or system settings. - Warning: Avoid using sudo for non-critical tasks unless absolutely necessary—it grants broad system access.
  • Deleting directories or files unintentionally - Use rm -i (interactive) or double-check with ls before running destructive commands like rm -r. - For safety, create backups with cp file.txt backup/ before deleting.
  • Using wildcards incorrectly - Commands like ls *.txt match all files ending in .txt. Wildcards (e.g., *, ?) are case-sensitive by default. Use ls -i to see file IDs for debugging.
  • Ignoring permission errors - If you see Permission denied, the file/directory lacks read/execute permissions. Fix with:
    • chmod +r file.txt – Grant read permissions.
    • chown user:group file.txt – Change ownership (requires sudo for system files).

Tips for Efficiency

  • Master tab completion - Press Tab to autocomplete filenames, commands, or arguments. Press Shift+Tab to see possible matches.
  • Use aliases for frequent commands - Create shortcuts in your shell config file (e.g., ~/.bashrc or ~/.zshrc): alias ll='ls -la' (Reload with source ~/.bashrc after editing.)
  • Learn shell history - View past commands with history. Edit history with !99 (run the 99th command) or !! (repeat the last command).
  • Pipe commands for data processing - Chain commands using the pipe (|):
    • ls | grep ".txt" – List files and filter only .txt files.
    • grep "error" system.log | less – Search logs and view results page-by-page.
  • Use aliases for common tasks - Example: Add this to your shell config to avoid typing sudo su: alias su='sudo su -'

Troubleshooting

  1. Command not found - If you see bash: command_not_found: command not found, the command doesn’t exist or isn’t installed. Install it with:
    • sudo apt install command_name (Debian/Ubuntu).
    • sudo dnf install command_name (Fedora/RHEL).
  2. Permission denied for critical files - If you need to modify system files (e.g., /etc/hosts), use sudo or contact your system administrator. Never edit files directly in /var/lib/ or /usr/ without permission.
  3. Terminal freezes or hangs - Restart the terminal or your system. If the issue persists, check for conflicting processes:
    • top or htop to identify resource-hogging apps.
    • Kill resource-intensive processes with kill -9 .
  4. Shell prompt is unreadable - Ensure your terminal supports UTF-8 encoding. Set locale for UTF-8 in your shell config: export LANG=en_US.UTF-8 (Add this to ~/.bashrc or ~/.zshrc.)

Frequently Asked Questions

How do I create a password-protected archive?

Use tar -czvf archive.tar.gz /path/to/folder to compress a folder into a .tar.gz file. Add password protection with: sudo tar -czvf - archive.tar.gz | gzip -d - | gpg -c - (The -c flag creates a new encrypted file; remove it to edit.)

What’s the difference between ls and dir?

On Linux, ls is the primary command for listing files. dir does not exist natively—it’s a Windows/CMD command. Some Linux distributions may include ls aliases for dir, but it’s not standard.

How do I search for a file by size?

Use find /path -type f -size +100M to find files larger than 100MB. To list sizes in human-readable format: find /path -type f -exec du -h {} \;

Why can’t I run sudo commands in my home directory?

Permissions for sudo are tied to system-wide files. If you’re locked out, try: sudo -i to enter the root shell (use sparingly). If that fails, reboot and check for Permission denied errors in logs (e.g., /var/log/auth.log).

How do I create a symbolic link?

ln -s /target/path /link/path creates a symbolic link. For example: ln -s /etc/hosts /home/user/hosts-backup (The link will update if the target changes.)

What’s the quickest way to count lines in a file?

wc -l file.txt counts lines. Add -w for words and -m for characters. For large files, use wc -l < file.txt to avoid loading the file into memory.

Linux commands empower you to automate tasks, debug systems, and optimize workflows—whether you’re managing servers, scripting scripts, or troubleshooting issues. Start with the basics, then explore advanced tools like awk and sed for text manipulation. Always prioritize safety (e.g., use sudo judiciously) and back up critical data before deletions.