Why the Command Line?

The Linux command line is powerful, fast, and universal. Whether you're managing servers, automating tasks, or developing software, mastering basic commands is essential. This guide covers everything you need to navigate and manage a Linux system effectively.

What We'll Cover:
  • Essential navigation and file management commands
  • The Linux directory hierarchy and what each directory does
  • File permissions, ownership, and how to manage them
  • Process management and system information commands
  • Shell history and productivity shortcuts
  • Practical examples for everyday tasks

Navigation & File System Basics

These are the commands you'll use every single day. Master these first.

pwd

Print Working Directory - Shows your current location
$ pwd
/home/omar/projects

ls

List directory contents
$ ls # List files
$ ls -l # Long format (permissions, size, date)
$ ls -la # Include hidden files (starting with .)
$ ls -lh # Human-readable sizes (KB, MB, GB)
$ ls -lt # Sort by modification time

cd

Change Directory - Navigate the file system
$ cd /etc # Go to /etc
$ cd ~ # Go to home directory
$ cd .. # Go up one level
$ cd - # Go to previous directory
$ cd # Go to home directory
Pro Tip: Use Tab for auto-completion. Type the first few letters of a file/directory and press Tab to complete it. Press Tab twice to see all options.

File & Directory Management

mkdir

Make Directory - Create new directories
$ mkdir myproject
$ mkdir -p path/to/nested/dir # Create parent directories

touch

Create empty file or update timestamp
$ touch newfile.txt
$ touch file1.txt file2.txt file3.txt # Create multiple

cp

Copy files and directories
$ cp file.txt backup.txt # Copy file
$ cp file.txt /path/to/dest/ # Copy to directory
$ cp -r mydir/ backup/ # Copy directory recursively
$ cp -i file.txt dest.txt # Interactive (ask before overwrite)

mv

Move or rename files and directories
$ mv oldname.txt newname.txt # Rename
$ mv file.txt /path/to/dest/ # Move
$ mv *.txt documents/ # Move all .txt files

rm

Remove files and directories
$ rm file.txt # Delete file
$ rm -r mydir/ # Delete directory recursively
$ rm -i file.txt # Interactive (ask confirmation)
$ rm -f file.txt # Force (no confirmation)
Warning: rm -rf / will delete EVERYTHING on your system.

Viewing & Searching Files

cat

Concatenate and display file contents
$ cat file.txt # Display entire file
$ cat file1.txt file2.txt # Display multiple files
$ cat file.txt | grep "search" # Search in file

less / more

View file contents page by page
$ less file.txt # Navigate with arrows, q to quit
$ more file.txt # Older pager, less is better

head / tail

View beginning or end of file
$ head file.txt # First 10 lines
$ head -n 20 file.txt # First 20 lines
$ tail file.txt # Last 10 lines
$ tail -f /var/log/syslog # Follow file (live updates)

grep

Search for patterns in files
$ grep "error" logfile.txt # Find "error" in file
$ grep -i "error" file.txt # Case-insensitive
$ grep -r "TODO" ./ # Search recursively in directory
$ grep -n "error" file.txt # Show line numbers

find

Search for files and directories
$ find . -name "*.txt" # Find all .txt files
$ find /home -type d -name "logs" # Find directories named "logs"
$ find . -size +100M # Find files larger than 100MB
$ find . -mtime -7 # Modified in last 7 days

File Permissions & Ownership

Linux uses a permission system to control who can read, write, or execute files.

Understanding Permissions

-rwxr-xr-- 1 omar omar 4096 Dec 6 08:00 file.txt
-
File type
(- = file, d = directory)
rwx
Owner permissions
(read, write, execute)
r-x
Group permissions
(read, execute)
r--
Others permissions
(read only)
Permission Values:
r (read) = 4
w (write) = 2
x (execute) = 1

Example: rwx = 4+2+1 = 7, r-x = 4+0+1 = 5, r-- = 4+0+0 = 4

chmod

Change file permissions
$ chmod 755 script.sh # rwxr-xr-x
$ chmod 644 file.txt # rw-r--r--
$ chmod +x script.sh # Add execute permission
$ chmod -w file.txt # Remove write permission
$ chmod -R 755 mydir/ # Recursive

chown

Change file owner and group
$ sudo chown omar file.txt # Change owner
$ sudo chown omar:users file.txt # Change owner and group
$ sudo chown -R omar:users mydir/ # Recursive

Linux Directory Hierarchy

Linux organizes everything in a single directory tree starting from / (root). Understanding this structure is crucial for navigating and managing your system.

File System Tree

/ (root)
├── bin/ → Essential commands
├── boot/ → Boot files
├── dev/ → Device files
├── etc/ → Configuration
├── home/ → User directories
│ ├── omar/
│ ├── other users/
├── lib/ → System libraries
├── media/ → Removable media
├── mnt/ → Mount points
├── opt/ → Optional software
├── proc/ → Process info
├── root/ → Root user home
├── sbin/ → System binaries
├── srv/ → Service data
├── sys/ → System info
├── tmp/ → Temporary files
├── usr/ → User programs
│ ├── bin/
│ ├── lib/
│ ├── local/
│ └── share/
└── var/ → Variable data
    ├── log/
    ├── cache/
    └── tmp/

Essential Directories Explained

📁 User & Home Directories

/home

User Home Directories

Personal files for each user. Your files are in /home/username. This is where you spend most of your time.

/root

Root User Home

Home directory for the root (administrator) user. Separate from /home for security.

⚙️ System Binaries & Programs

/bin

Essential User Binaries

Essential commands available to all users: ls, cp, cat, bash. Needed for basic system operation.

/sbin

System Administration Binaries

System administration commands: fsck, reboot, iptables. Usually require root privileges.

/usr

User Programs & Data

Secondary hierarchy for user programs. Contains /usr/bin, /usr/lib, /usr/share. Most applications install here.

/usr/bin

User Commands

Non-essential user commands and applications. Most programs you install go here.

/usr/local

Locally Installed Software

Software compiled and installed manually (not from package manager). Keeps system packages separate.

/opt

Optional Add-on Software

Third-party applications. Each application gets its own subdirectory (e.g., /opt/google).

📝 Configuration & Variable Data

/etc

System Configuration

System-wide configuration files. Text files you can edit: /etc/hosts, /etc/fstab, /etc/ssh/sshd_config.

/var

Variable Data

Files that change frequently: logs (/var/log), databases, mail spools, caches. Grows over time.

/tmp

Temporary Files

Temporary files. Cleared on reboot. Anyone can write here. Use for temporary data only.

/srv

Service Data

Data for services provided by the system: web servers (/srv/www), FTP servers.

🔧 System, Hardware & Boot

/boot

Boot Loader Files

Files needed to boot the system: kernel images, bootloader configuration. Critical for startup.

/dev

Device Files

Device files representing hardware: /dev/sda (disk), /dev/tty (terminal). Everything is a file in Linux.

/proc

Process Information (Virtual)

Virtual filesystem with process and kernel info. Not real files—generated on the fly. Example: /proc/cpuinfo.

/sys

System Information (Virtual)

Virtual filesystem exposing kernel and device information. Used for hardware configuration.

/lib

System Libraries

Shared libraries needed by binaries in /bin and /sbin. Essential for system operation.

💾 Mount Points & Media

/mnt

Temporary Mount Point

Temporary mount point for filesystems. Manually mount drives here: mount /dev/sdb1 /mnt.

/media

Removable Media

Auto-mount point for removable media: USB drives, CD-ROMs. System automatically mounts here.

Most Important Directories:
/home → Your files
/etc → Configuration
/var/log → System logs
/tmp → Temp files
/usr/bin → Programs
/dev → Devices

Process & System Management

ps

View running processes
$ ps # Your processes
$ ps aux # All processes
$ ps aux | grep firefox # Find specific process

top / htop

Real-time process viewer
$ top # Interactive process viewer
$ htop # Better version (if installed)

kill

Terminate processes
$ kill 1234 # Terminate process with PID 1234
$ kill -9 1234 # Force kill
$ killall firefox # Kill all firefox processes

df

Disk space usage
$ df -h # Human-readable disk usage
$ df -h /home # Specific filesystem

du

Directory size
$ du -sh mydir/ # Total size of directory
$ du -h --max-depth=1 # Size of subdirectories

free

Memory usage
$ free -h # Human-readable memory usage

Networking Commands

ping google.com
Test network connectivity
curl example.com
Fetch web page
wget file.zip
Download file
ip addr
Show IP addresses
netstat -tuln
Show listening ports
ss -tuln
Modern netstat

Essential Keyboard Shortcuts

Ctrl + C
Kill current process
Ctrl + Z
Suspend process (background)
Ctrl + D
Exit shell / EOF
Ctrl + L
Clear screen
Ctrl + A
Move to start of line
Ctrl + E
Move to end of line
Ctrl + U
Delete line before cursor
Ctrl + K
Delete line after cursor
Ctrl + R
Search command history
↑ / ↓
Navigate command history
Tab
Auto-complete
Tab Tab
Show all options

Shell History & Productivity

Mastering shell history is essential for becoming efficient at the command line. Instead of retyping commands, you can leverage your shell's history to save time and reduce errors.

Navigating Command History

Your shell keeps a history of all commands you've run. Access it using arrow keys:

↑ (Up Arrow)
Previous command
↓ (Down Arrow)
Next command
← / →
Edit current line
Tip: You can edit any command from history before running it. Use arrow keys to navigate, then type to modify. The edited command is added as a new entry.

history

View your complete command history
$ history # Show all commands
$ history 20 # Show last 20 commands
$ history | grep docker # Search history for 'docker'

Shell Configuration Files

Configure your shell's behavior by editing configuration files:

Bash Configuration

Most common shell configuration locations
# Interactive sessions (terminal)
~/.bashrc

# Login shells (SSH, local login)
~/.bash_profile

# Reload config without restarting
$ source ~/.bashrc

Zsh Configuration

For Zsh users (macOS default since Catalina)
# Zsh configuration file
~/.zshrc

# Reload config
$ source ~/.zshrc

History Configuration

Control history size and behavior
# Add to ~/.bashrc
export HISTSIZE=1000 # Commands in memory
export HISTFILESIZE=5000 # Commands saved to file

# Check history file location
$ echo $HISTFILE
/home/omar/.bash_history

Searching Command History

Don't scroll through hundreds of commands—search instead:

Interactive History Search

The fastest way to find previous commands
# Press Ctrl+R to start reverse search
(reverse-i-search)`ssh': ssh user@server.com

# Type to search, Ctrl+R for next match
# Enter to execute, Esc to edit
# Ctrl+Shift+R to search forward
Search Workflow:
  1. Press Ctrl + R
  2. Type part of the command you're looking for
  3. Press Ctrl + R again to cycle through matches
  4. Press Enter to execute or Esc to edit
Clear History: Use history -c to clear current session history, or rm ~/.bash_history to delete the entire history file (useful for privacy).

Executing Previous Commands with !

The ! (bang) operator provides powerful shortcuts for re-running commands:

!! (Bang Bang)

Re-run the last command
$ apt-get install nginx # Fails - needs sudo
$ sudo !! # Becomes: sudo apt-get install nginx

!command

Re-run the last command starting with 'command'
$ !ssh # Re-runs last ssh command
$ !git # Re-runs last git command
$ !cd # Re-runs last cd command

!$ (Last Argument)

Reuse the last argument from previous command
$ cat /var/log/syslog
$ vim !$ # Opens /var/log/syslog in vim

!* (All Arguments)

Reuse all arguments from previous command
$ ls file1.txt file2.txt file3.txt
$ rm !* # Removes all three files
⚠ Security Warning: Always verify what command will run before using sudo !!. Blindly running commands with elevated privileges can be dangerous. Make sure you understand why a command needs sudo and trust it completely.

Line Editing Shortcuts

Navigate and edit commands efficiently without reaching for the mouse:

Ctrl + A
Jump to beginning of line
Ctrl + E
Jump to end of line
Ctrl + U
Delete from cursor to start
Ctrl + K
Delete from cursor to end
Ctrl + W
Delete word before cursor
Alt + B
Move backward one word
Alt + F
Move forward one word
Ctrl + L
Clear screen (keeps command)

History Exceptions

Commands Not Saved to History:
  • Failed commands: Commands that exit with non-zero status (errors)
  • Commands starting with space: Useful for sensitive commands with passwords
  • Note: You can still access these with Up-Arrow immediately after running them

Practical Examples

Common Workflow

# Run a long command
$ docker run -d -p 8080:80 --name web nginx

# Oops, forgot sudo
$ sudo !!

# Later, need to run docker again
$ !docker

# Search for that nginx command
$ Ctrl+R nginx

# Jump to start, add 'sudo'
$ Ctrl+A sudo Ctrl+E
Key Takeaways:
  • Use Ctrl+R to search history instead of scrolling
  • Use !! to quickly re-run with sudo
  • Use Ctrl+A and Ctrl+E to jump around lines
  • Configure HISTSIZE and HISTFILESIZE for more history