Linux Command Line Interface Navigation Guide
Table of Contents
1. [Introduction](#introduction) 2. [Terminal Basics](#terminal-basics) 3. [File System Structure](#file-system-structure) 4. [Essential Navigation Commands](#essential-navigation-commands) 5. [File and Directory Operations](#file-and-directory-operations) 6. [File Permissions and Ownership](#file-permissions-and-ownership) 7. [Text Processing Commands](#text-processing-commands) 8. [System Information Commands](#system-information-commands) 9. [Process Management](#process-management) 10. [Network Commands](#network-commands) 11. [Command Line Tips and Tricks](#command-line-tips-and-tricks) 12. [Advanced Features](#advanced-features)
Introduction
The Linux Command Line Interface (CLI) is a powerful text-based interface that allows users to interact with the operating system through commands. Unlike graphical user interfaces, the CLI provides direct access to system functions and offers greater control, automation capabilities, and efficiency for system administration and development tasks.
The CLI operates through a shell, which is a program that interprets and executes commands. The most common shell in Linux distributions is Bash (Bourne Again Shell), though other shells like Zsh, Fish, and Dash are also available.
Terminal Basics
Understanding the Shell Prompt
The shell prompt is the text displayed in the terminal that indicates the system is ready to accept commands. A typical prompt format looks like:
`bash
username@hostname:current_directory$
`
| Component | Description | Example | |-----------|-------------|---------| | username | Current logged-in user | john | | hostname | Computer name | server01 | | current_directory | Present working directory | /home/john | | $ or # | User privilege indicator | $ (regular user), # (root) |
Terminal Shortcuts
| Shortcut | Action | |----------|--------| | Ctrl + C | Interrupt/cancel current command | | Ctrl + D | Exit terminal or end input | | Ctrl + L | Clear terminal screen | | Ctrl + A | Move cursor to beginning of line | | Ctrl + E | Move cursor to end of line | | Ctrl + U | Delete from cursor to beginning of line | | Ctrl + K | Delete from cursor to end of line | | Tab | Auto-complete commands and filenames | | Up/Down Arrow | Navigate command history |
File System Structure
Linux follows a hierarchical file system structure starting from the root directory (/). Understanding this structure is crucial for effective navigation.
Standard Directory Structure
| Directory | Purpose | Description | |-----------|---------|-------------| | / | Root directory | Top-level directory of the file system | | /bin | Essential binaries | Basic system commands available to all users | | /boot | Boot files | Files needed for system boot process | | /dev | Device files | Hardware device representations | | /etc | Configuration files | System-wide configuration files | | /home | User directories | Personal directories for regular users | | /lib | Libraries | Shared libraries needed by system programs | | /media | Removable media | Mount points for removable devices | | /mnt | Mount points | Temporary mount points for file systems | | /opt | Optional software | Third-party software packages | | /proc | Process information | Virtual file system with system information | | /root | Root user home | Home directory for root user | | /run | Runtime data | Runtime system information | | /sbin | System binaries | System administration commands | | /tmp | Temporary files | Temporary files deleted on reboot | | /usr | User programs | User applications and utilities | | /var | Variable data | Log files, databases, and other changing data |
Essential Navigation Commands
pwd - Print Working Directory
The pwd command displays the current directory path.
`bash
pwd
`
Output example:
`
/home/username/documents
`
Notes: - Always shows absolute path from root directory - Useful for confirming current location in file system - No options commonly used with this command
ls - List Directory Contents
The ls command lists files and directories in the current or specified directory.
Basic syntax:
`bash
ls [options] [directory]
`
Common options:
| Option | Description | Example |
|--------|-------------|---------|
| -l | Long format listing | ls -l |
| -a | Show hidden files | ls -a |
| -h | Human readable sizes | ls -lh |
| -t | Sort by modification time | ls -lt |
| -r | Reverse order | ls -lr |
| -R | Recursive listing | ls -R |
| -d | List directories themselves | ls -ld /home |
Examples:
`bash
Basic listing
lsLong format with hidden files
ls -laHuman readable sizes sorted by time
ls -lhtList specific directory
ls /etcRecursive listing of subdirectories
ls -R /home/user/documents`Sample output of ls -la:
`
total 24
drwxr-xr-x 4 user user 4096 Nov 15 10:30 .
drwxr-xr-x 12 user user 4096 Nov 15 09:15 ..
-rw-r--r-- 1 user user 220 Nov 10 08:30 .bashrc
drwxr-xr-x 2 user user 4096 Nov 15 10:30 documents
-rw-r--r-- 1 user user 1024 Nov 15 10:25 file.txt
`
cd - Change Directory
The cd command changes the current working directory.
Basic syntax:
`bash
cd [directory]
`
Special directory references:
| Symbol | Meaning | Example |
|--------|---------|---------|
| . | Current directory | cd . |
| .. | Parent directory | cd .. |
| ~ | Home directory | cd ~ |
| - | Previous directory | cd - |
| / | Root directory | cd / |
Examples:
`bash
Change to home directory
cd ~ cdMove to parent directory
cd ..Move to specific directory
cd /etc/apache2Return to previous directory
cd -Move multiple levels up
cd ../../..Change to subdirectory
cd documents/projects`Notes:
- Without arguments, cd returns to home directory
- Supports both absolute and relative paths
- Tab completion works with directory names
- Use quotes for directories with spaces: cd "My Documents"
File and Directory Operations
mkdir - Create Directories
Creates new directories in the file system.
Basic syntax:
`bash
mkdir [options] directory_name
`
Common options:
| Option | Description | Example |
|--------|-------------|---------|
| -p | Create parent directories | mkdir -p path/to/new/dir |
| -m | Set permissions | mkdir -m 755 newdir |
| -v | Verbose output | mkdir -v newdir |
Examples:
`bash
Create single directory
mkdir documentsCreate multiple directories
mkdir dir1 dir2 dir3Create nested directory structure
mkdir -p projects/web/html/cssCreate with specific permissions
mkdir -m 755 public_folderVerbose creation
mkdir -v new_project`rmdir and rm - Remove Directories and Files
rmdir removes empty directories only:
`bash
Remove empty directory
rmdir empty_folderRemove multiple empty directories
rmdir dir1 dir2 dir3`rm removes files and directories:
Basic syntax:
`bash
rm [options] file_or_directory
`
Common options:
| Option | Description | Example |
|--------|-------------|---------|
| -r | Recursive (for directories) | rm -r directory |
| -f | Force removal | rm -f file |
| -i | Interactive confirmation | rm -i file |
| -v | Verbose output | rm -v file |
Examples:
`bash
Remove single file
rm file.txtRemove multiple files
rm file1.txt file2.txt file3.txtRemove directory and contents
rm -r old_projectForce remove without confirmation
rm -rf temp_folderInteractive removal
rm -i important_file.txt`Warning: Be extremely careful with rm -rf as it permanently deletes files and directories without confirmation.
cp - Copy Files and Directories
Copies files and directories from source to destination.
Basic syntax:
`bash
cp [options] source destination
`
Common options:
| Option | Description | Example |
|--------|-------------|---------|
| -r | Recursive copy | cp -r source_dir dest_dir |
| -i | Interactive (prompt before overwrite) | cp -i file1 file2 |
| -v | Verbose output | cp -v file1 file2 |
| -p | Preserve attributes | cp -p file1 file2 |
| -u | Update (copy only newer files) | cp -u file1 file2 |
Examples:
`bash
Copy single file
cp document.txt backup.txtCopy file to different directory
cp report.pdf /home/user/documents/Copy directory recursively
cp -r project_folder /backup/Copy with preserved permissions
cp -p config.conf config.conf.backupCopy multiple files to directory
cp file1.txt file2.txt file3.txt /destination/`mv - Move/Rename Files and Directories
Moves files and directories or renames them.
Basic syntax:
`bash
mv [options] source destination
`
Examples:
`bash
Rename file
mv oldname.txt newname.txtMove file to different directory
mv document.pdf /home/user/documents/Move and rename simultaneously
mv old_report.txt /archive/final_report.txtMove multiple files
mv file1.txt file2.txt file3.txt /destination/Rename directory
mv old_folder_name new_folder_name`File Permissions and Ownership
Understanding File Permissions
Linux uses a permission system to control access to files and directories. Permissions are displayed in the long format listing (ls -l).
Permission format:
`
-rwxrwxrwx
`
| Position | Meaning | |----------|---------| | 1st character | File type (- file, d directory, l link) | | 2-4 characters | Owner permissions | | 5-7 characters | Group permissions | | 8-10 characters | Other permissions |
Permission types:
| Symbol | Permission | Octal Value | |--------|------------|-------------| | r | Read | 4 | | w | Write | 2 | | x | Execute | 1 | | - | No permission | 0 |
chmod - Change File Permissions
Basic syntax:
`bash
chmod [options] permissions file/directory
`
Octal notation examples:
| Octal | Binary | Permissions | Description | |-------|--------|-------------|-------------| | 755 | 111 101 101 | rwxr-xr-x | Owner: rwx, Group: r-x, Other: r-x | | 644 | 110 100 100 | rw-r--r-- | Owner: rw-, Group: r--, Other: r-- | | 600 | 110 000 000 | rw------- | Owner: rw-, Group: ---, Other: --- | | 777 | 111 111 111 | rwxrwxrwx | All permissions for everyone |
Examples:
`bash
Set specific permissions using octal
chmod 755 script.shMake file executable for owner
chmod u+x program.pyRemove write permission for group and others
chmod go-w sensitive_file.txtSet read and write for owner, read only for others
chmod 644 document.txtRecursive permission change
chmod -R 755 /var/www/html/`chown - Change File Ownership
Basic syntax:
`bash
chown [options] owner:group file/directory
`
Examples:
`bash
Change owner only
chown newuser file.txtChange owner and group
chown newuser:newgroup file.txtChange group only
chown :newgroup file.txtRecursive ownership change
chown -R apache:apache /var/www/html/`Text Processing Commands
cat - Display File Contents
Displays the entire content of files.
Basic syntax:
`bash
cat [options] file1 [file2 ...]
`
Examples:
`bash
Display single file
cat document.txtDisplay multiple files
cat file1.txt file2.txtNumber lines
cat -n document.txtShow non-printing characters
cat -A document.txt`less and more - Page Through Files
less (preferred) and more allow viewing large files page by page.
`bash
View file with less
less large_file.txtView file with more
more large_file.txt`Navigation keys in less:
| Key | Action | |-----|--------| | Space | Next page | | b | Previous page | | / | Search forward | | ? | Search backward | | n | Next search result | | N | Previous search result | | q | Quit |
head and tail - Display File Parts
head shows the beginning of files, tail shows the end.
Examples:
`bash
Show first 10 lines (default)
head file.txtShow first 20 lines
head -n 20 file.txtShow last 10 lines
tail file.txtShow last 5 lines
tail -n 5 file.txtFollow file changes (useful for logs)
tail -f /var/log/syslog`grep - Search Text Patterns
Searches for patterns in files using regular expressions.
Basic syntax:
`bash
grep [options] pattern file(s)
`
Common options:
| Option | Description | Example |
|--------|-------------|---------|
| -i | Ignore case | grep -i "error" log.txt |
| -n | Show line numbers | grep -n "pattern" file.txt |
| -r | Recursive search | grep -r "function" /code/ |
| -v | Invert match | grep -v "debug" log.txt |
| -c | Count matches | grep -c "error" log.txt |
Examples:
`bash
Simple pattern search
grep "error" system.logCase insensitive search
grep -i "warning" *.logSearch with line numbers
grep -n "TODO" source.pyRecursive search in directory
grep -r "configuration" /etc/Search for whole words
grep -w "test" file.txtMultiple patterns
grep -E "error|warning|critical" system.log`System Information Commands
System Status Commands
| Command | Description | Example |
|---------|-------------|---------|
| whoami | Current username | whoami |
| id | User and group IDs | id |
| uname | System information | uname -a |
| hostname | System hostname | hostname |
| uptime | System uptime | uptime |
| date | Current date and time | date |
| cal | Calendar | cal |
| df | Disk space usage | df -h |
| du | Directory space usage | du -sh /home |
| free | Memory usage | free -h |
Detailed Command Examples
df - Disk Free Space:
`bash
Human readable format
df -hShow specific filesystem
df -h /homeShow inodes
df -i`Sample output:
`
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 12G 7.2G 63% /
/dev/sda2 100G 45G 50G 48% /home
`
du - Disk Usage:
`bash
Summary of current directory
du -sh .Show subdirectory sizes
du -h --max-depth=1Largest files and directories
du -ah | sort -rh | head -20`free - Memory Information:
`bash
Human readable memory info
free -hShow in megabytes
free -m`Process Management
Process Viewing Commands
| Command | Description | Usage |
|---------|-------------|-------|
| ps | Show running processes | ps aux |
| top | Real-time process viewer | top |
| htop | Enhanced process viewer | htop |
| pgrep | Find process IDs | pgrep firefox |
| pidof | Find process IDs by name | pidof apache2 |
ps - Process Status
Common options:
`bash
Show all processes
ps auxShow processes for current user
ps uxShow process tree
ps auxfShow specific processes
ps aux | grep apache`Sample output:
`
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 225316 9012 ? Ss Nov14 0:02 /sbin/init
user 1234 2.1 5.2 892456 85234 ? Sl 10:30 1:25 firefox
`
Process Control
| Command | Description | Example |
|---------|-------------|---------|
| kill | Terminate process by PID | kill 1234 |
| killall | Terminate process by name | killall firefox |
| pkill | Kill processes by pattern | pkill -f "python script" |
| nohup | Run command immune to hangups | nohup command & |
| bg | Put job in background | bg %1 |
| fg | Bring job to foreground | fg %1 |
| jobs | List active jobs | jobs |
Kill signals:
| Signal | Number | Description | |--------|--------|-------------| | TERM | 15 | Graceful termination (default) | | KILL | 9 | Force kill | | HUP | 1 | Hang up | | INT | 2 | Interrupt (Ctrl+C) | | STOP | 19 | Stop process | | CONT | 18 | Continue process |
Examples:
`bash
Graceful termination
kill 1234Force kill
kill -9 1234Kill all instances
killall -9 firefoxBackground process
command &Start process immune to hangup
nohup long_running_script.sh &`Network Commands
Network Information Commands
| Command | Description | Example |
|---------|-------------|---------|
| ping | Test network connectivity | ping google.com |
| wget | Download files | wget http://example.com/file.zip |
| curl | Transfer data from servers | curl -O http://example.com/file.txt |
| ssh | Secure shell connection | ssh user@server.com |
| scp | Secure copy over network | scp file.txt user@server:/path/ |
| netstat | Network connections | netstat -tuln |
| ss | Socket statistics | ss -tuln |
Examples:
`bash
Test connectivity
ping -c 4 google.comDownload file with wget
wget -O local_name.zip http://example.com/file.zipHTTP request with curl
curl -X GET https://api.example.com/dataSSH connection
ssh -p 2222 user@192.168.1.100Copy file to remote server
scp document.pdf user@server.com:/home/user/Show listening ports
netstat -tuln | grep LISTEN`Command Line Tips and Tricks
Command History
| Command | Description | |---------|-------------| | history | Show command history | | !! | Repeat last command | | !n | Repeat command number n | | !string | Repeat last command starting with string | | Ctrl+R | Search command history |
Command Chaining
| Operator | Description | Example |
|----------|-------------|---------|
| ; | Run commands sequentially | cmd1; cmd2; cmd3 |
| && | Run next if previous succeeds | make && make install |
| \|\| | Run next if previous fails | cmd1 \|\| cmd2 |
| & | Run in background | long_command & |
| \| | Pipe output to next command | ls -l \| grep txt |
Redirection
| Operator | Description | Example |
|----------|-------------|---------|
| > | Redirect output (overwrite) | ls > file_list.txt |
| >> | Redirect output (append) | echo "text" >> file.txt |
| < | Redirect input | sort < unsorted.txt |
| 2> | Redirect errors | command 2> errors.txt |
| &> | Redirect both output and errors | command &> all_output.txt |
Wildcards and Globbing
| Pattern | Description | Example |
|---------|-------------|---------|
| | Match any characters | ls .txt |
| ? | Match single character | ls file?.txt |
| [abc] | Match any of a, b, c | ls file[123].txt |
| [a-z] | Match range | ls [a-m]*.txt |
| {a,b,c} | Match alternatives | cp file.{txt,pdf} /backup/ |
Advanced Features
Environment Variables
Viewing and setting variables:
`bash
Show all environment variables
envShow specific variable
echo $HOME echo $PATHSet temporary variable
export MY_VAR="value"Add to PATH
export PATH=$PATH:/new/pathMake permanent (add to ~/.bashrc)
echo 'export MY_VAR="value"' >> ~/.bashrc`Important environment variables:
| Variable | Description | |----------|-------------| | HOME | User's home directory | | PATH | Executable search path | | USER | Current username | | SHELL | Current shell | | PWD | Present working directory | | EDITOR | Default text editor |
Command Substitution
Execute commands within other commands:
`bash
Using backticks (deprecated)
echo "Today isdate"Using $() (preferred)
echo "Today is $(date)"Complex examples
files_count=$(ls | wc -l) echo "There are $files_count files"Nested substitution
echo "Disk usage: $(df -h $(pwd) | tail -1 | awk '{print $5}')"`Aliases
Create shortcuts for commonly used commands:
`bash
Create temporary alias
alias ll='ls -la' alias la='ls -A' alias l='ls -CF'View current aliases
aliasRemove alias
unalias llMake permanent (add to ~/.bashrc)
echo "alias ll='ls -la'" >> ~/.bashrc`Useful alias examples:
`bash
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias mkdir='mkdir -pv'
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias ps='ps auxf'
alias ping='ping -c 5'
alias cls='clear'
`
Functions
Create reusable command sequences:
`bash
Simple function
mcd() { mkdir -p "$1" && cd "$1" }Function with multiple commands
backup() { local source="$1" local dest="$HOME/backups/$(basename "$source")-$(date +%Y%m%d)" cp -r "$source" "$dest" echo "Backup created: $dest" }Use functions
mcd new_project backup /important/directory`This comprehensive guide covers the essential aspects of navigating and using the Linux command line interface. Regular practice with these commands and concepts will build proficiency and confidence in using the CLI effectively for system administration, development, and daily computing tasks.