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.
- 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
/home/omar/projects
ls
$ 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
$ cd ~ # Go to home directory
$ cd .. # Go up one level
$ cd - # Go to previous directory
$ cd # Go to home directory
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
$ mkdir -p path/to/nested/dir # Create parent directories
touch
$ touch file1.txt file2.txt file3.txt # Create multiple
cp
$ 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
$ mv file.txt /path/to/dest/ # Move
$ mv *.txt documents/ # Move all .txt files
rm
$ rm -r mydir/ # Delete directory recursively
$ rm -i file.txt # Interactive (ask confirmation)
$ rm -f file.txt # Force (no confirmation)
rm -rf / will delete EVERYTHING on your system.
Viewing & Searching Files
cat
$ cat file1.txt file2.txt # Display multiple files
$ cat file.txt | grep "search" # Search in file
less / more
$ more file.txt # Older pager, less is better
head / tail
$ head -n 20 file.txt # First 20 lines
$ tail file.txt # Last 10 lines
$ tail -f /var/log/syslog # Follow file (live updates)
grep
$ grep -i "error" file.txt # Case-insensitive
$ grep -r "TODO" ./ # Search recursively in directory
$ grep -n "error" file.txt # Show line numbers
find
$ 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
(- = file, d = directory)
(read, write, execute)
(read, execute)
(read only)
w (write) = 2
x (execute) = 1
Example: rwx = 4+2+1 = 7, r-x = 4+0+1 = 5, r-- = 4+0+0 = 4
chmod
$ 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
$ 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
Essential Directories Explained
📁 User & Home Directories
/home
Personal files for each user. Your files are in /home/username. This is
where you spend most of your time.
/root
Home directory for the root (administrator) user. Separate from /home for
security.
⚙️ System Binaries & Programs
/bin
Essential commands available to all users: ls, cp,
cat, bash. Needed for basic system operation.
/sbin
System administration commands: fsck, reboot,
iptables. Usually require root privileges.
/usr
Secondary hierarchy for user programs. Contains /usr/bin,
/usr/lib, /usr/share. Most applications install here.
/usr/bin
Non-essential user commands and applications. Most programs you install go here.
/usr/local
Software compiled and installed manually (not from package manager). Keeps system packages separate.
/opt
Third-party applications. Each application gets its own subdirectory (e.g.,
/opt/google).
📝 Configuration & Variable Data
/etc
System-wide configuration files. Text files you can edit: /etc/hosts,
/etc/fstab, /etc/ssh/sshd_config.
/var
Files that change frequently: logs (/var/log), databases, mail spools,
caches. Grows over time.
/tmp
Temporary files. Cleared on reboot. Anyone can write here. Use for temporary data only.
/srv
Data for services provided by the system: web servers (/srv/www), FTP
servers.
🔧 System, Hardware & Boot
/boot
Files needed to boot the system: kernel images, bootloader configuration. Critical for startup.
/dev
Device files representing hardware: /dev/sda (disk), /dev/tty
(terminal). Everything is a file in Linux.
/proc
Virtual filesystem with process and kernel info. Not real files—generated on the fly.
Example: /proc/cpuinfo.
/sys
Virtual filesystem exposing kernel and device information. Used for hardware configuration.
/lib
Shared libraries needed by binaries in /bin and /sbin.
Essential for system operation.
💾 Mount Points & Media
/mnt
Temporary mount point for filesystems. Manually mount drives here:
mount /dev/sdb1 /mnt.
/media
Auto-mount point for removable media: USB drives, CD-ROMs. System automatically mounts here.
/home → Your files
/etc → Configuration
/var/log → System
logs/tmp → Temp files
/usr/bin → Programs
/dev → DevicesProcess & System Management
ps
$ ps aux # All processes
$ ps aux | grep firefox # Find specific process
top / htop
$ htop # Better version (if installed)
kill
$ kill -9 1234 # Force kill
$ killall firefox # Kill all firefox processes
df
$ df -h /home # Specific filesystem
du
$ du -h --max-depth=1 # Size of subdirectories
free
Networking Commands
ping google.com
curl example.com
wget file.zip
ip addr
netstat -tuln
ss -tuln
Essential Keyboard Shortcuts
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)
↓ (Down Arrow)
← / →
history
$ 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
~/.bashrc
# Login shells (SSH, local login)
~/.bash_profile
# Reload config without restarting
$ source ~/.bashrc
Zsh Configuration
~/.zshrc
# Reload config
$ source ~/.zshrc
History Configuration
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
(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
- Press
Ctrl + R - Type part of the command you're looking for
- Press
Ctrl + Ragain to cycle through matches - Press
Enterto execute orEscto edit
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)
$ sudo !! # Becomes: sudo apt-get install nginx
!command
$ !git # Re-runs last git command
$ !cd # Re-runs last cd command
!$ (Last Argument)
$ vim !$ # Opens /var/log/syslog in vim
!* (All Arguments)
$ rm !* # Removes all three files
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
Ctrl + E
Ctrl + U
Ctrl + K
Ctrl + W
Alt + B
Alt + F
Ctrl + L
History Exceptions
- 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
$ 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
- Use
Ctrl+Rto search history instead of scrolling - Use
!!to quickly re-run with sudo - Use
Ctrl+AandCtrl+Eto jump around lines - Configure
HISTSIZEandHISTFILESIZEfor more history