Skip to content

Basic_commands

Chapter 5: Basic Linux Commands and Navigation

Section titled “Chapter 5: Basic Linux Commands and Navigation”

This chapter covers essential Linux commands for navigation, file operations, text processing, and system administration. These commands form the foundation that every Linux sysadmin must master.


CommandDescriptionExample
pwdPrint working directorypwd
cdChange directorycd /etc
lsList directory contentsls -la
treeDisplay directory treetree -L 2
Terminal window
cd / # Go to root
cd ~ # Go to home
cd - # Go to previous directory
cd .. # Go to parent directory
cd ../.. # Go up two levels
Terminal window
ls -lah # All files, long format, human readable
ls -lSr # Long, by size, reverse (smallest first)
ls -lt # Long, sorted by time (newest first)
ls -ld /dir # Show directory info, not contents
ls -i # Show inode numbers
ls -R # Recursive listing

Terminal window
# Create empty file
touch filename.txt
# Create multiple empty files
touch file1.txt file2.txt file3.txt
# Create directory
mkdir dirname
# Create nested directories
mkdir -p /path/to/nested/dir
# Create directory with specific permissions
mkdir -m 755 dirname
Terminal window
# Copy file
cp source.txt destination.txt
# Copy with preserve attributes
cp -p source.txt destination.txt
# Copy directory recursively
cp -r /source/dir /destination/
# Copy with archive mode (preserves everything)
cp -a /source/dir /destination/
# Move/Rename
mv oldname.txt newname.txt
mv file.txt /other/directory/
# Delete file
rm filename.txt
# Force delete without prompt
rm -f filename.txt
# Delete directory and contents
rm -rf dirname
# Delete empty directory
rmdir dirname
Terminal window
# Create tar archive
tar -cvf archive.tar /path/to/dir
# Create compressed tar
tar -czvf archive.tar.gz /path/to/dir
tar -cjvf archive.tar.bz2 /path/to/dir
tar -cJvf archive.tar.xz /path/to/dir
# Extract tar
tar -xvf archive.tar
tar -xzvf archive.tar.gz
tar -xjvf archive.tar.bz2
tar -xJvf archive.tar.xz
# List contents
tar -tvf archive.tar

Terminal window
cat file.txt # View entire file
cat -n file.txt # With line numbers
cat -b file.txt # Number non-empty lines
tac file.txt # View file in reverse
less file.txt # Paginated viewing (better than more)
# Less commands: /search, n(next), N(previous), q(quit), g(start), G(end)
more file.txt # Scroll forward only (legacy)
Terminal window
head -n 20 file.txt # First 20 lines
head -c 100 file.txt # First 100 bytes
tail -n 20 file.txt # Last 20 lines
tail -f file.txt # Follow file (live updates)
tail -f -n 100 file.txt # Follow with 100 lines buffer
tail -F file.txt # Follow even if file rotated

Terminal window
# Basic search
grep "pattern" file.txt
grep -i "pattern" file.txt # Case insensitive
grep -r "pattern" /dir/ # Recursive search
grep -n "pattern" file.txt # Show line numbers
grep -v "pattern" file.txt # Invert match (show non-matching)
grep -c "pattern" file.txt # Count matches
# Extended grep (regex)
grep -E "pattern1|pattern2" file.txt
egrep "pattern1|pattern2" file.txt
# Fixed string (no regex)
grep -F "literal.text" file.txt
fgrep "literal.text" file.txt
# Context options
grep -A 3 "pattern" file.txt # Show 3 lines after match
grep -B 3 "pattern" file.txt # Show 3 lines before
grep -C 3 "pattern" file.txt # Show 3 lines around
# Useful examples
ps aux | grep nginx # Find nginx process
grep -r --include="*.log" "ERROR" /var/log/
ls /usr/bin | grep ^l # Commands starting with 'l'
Terminal window
# Replace first occurrence in each line
sed 's/old/new/' file.txt
# Replace all occurrences
sed 's/old/new/g' file.txt
# In-place edit
sed -i 's/old/new/g' file.txt
# Replace on specific line
sed -i '5s/old/new/' file.txt
# Delete lines matching pattern
sed '/pattern/d' file.txt
# Print specific lines
sed -n '5,10p' file.txt # Print lines 5-10
sed -n '5p' file.txt # Print line 5
# Multiple commands
sed -e 's/old1/new1/' -e 's/old2/new2/' file.txt
Terminal window
# Print first column
awk '{print $1}' file.txt
# Print specific columns
awk '{print $1, $3}' file.txt
# Use custom delimiter
awk -F',' '{print $1}' file.csv
# Print lines where column matches
awk '$3 > 100' file.txt
# Built-in variables
awk 'BEGIN {FS=","} {print $1}' file.txt # Field separator
awk 'BEGIN {OFS=" - "} {print $1, $2}' # Output field separator
awk '{print NR, $0}' file.txt # NR = line number
# Conditional processing
awk '{if ($3 > 50) print $1, $3}' file.txt
# Summing values
awk '{sum += $3} END {print sum}' file.txt

Terminal window
uname -a # All system info
uname -r # Kernel release
uname -m # Machine hardware name
uname -n # Network node hostname
hostname # Hostname
hostname -I # IP addresses
whoami # Current user
date # Current date/time
date +%Y-%m-%d # Formatted date
uptime # System uptime
Terminal window
# CPU
lscpu # CPU architecture details
cat /proc/cpuinfo # Detailed CPU info
# Memory
free -h # Memory and swap usage
free -m # Memory in MB
cat /proc/meminfo # Detailed memory info
# Disk
df -h # Disk space usage (human readable)
df -i # Inode usage
du -sh /dir # Directory size
du -h --max-depth=1 # Size of subdirectories
# Block devices
lsblk # Block devices tree
blkid # UUIDs and types
fdisk -l # Partition table
# PCI/USB
lspci # PCI devices
lsusb # USB devices
lshw # Hardware info (comprehensive)
Terminal window
ps aux # All processes
ps aux | head -20 # Top 20 processes
ps -ef # Full format
ps -eo pid,ppid,cmd,%mem,%cpu # Custom columns
pidof nginx # PID of process
pgrep nginx # Find processes by name
# Real-time monitoring
top # Process monitor (legacy)
htop # Enhanced process viewer (recommended)
btop # Modern, GPU-accelerated
atop # Advanced system monitor
# Kill processes
kill PID # Graceful kill
kill -9 PID # Force kill
pkill nginx # Kill by name
killall nginx # Kill all with name

DescriptorNameDescription
0stdinStandard input
1stdoutStandard output
2stderrStandard error
Terminal window
command > output.txt # Overwrite stdout to file
command >> output.txt # Append stdout to file
command 2> error.txt # Redirect stderr only
command 2>&1 # Redirect stderr to stdout
command &> all.txt # Redirect both to same file
command 1>out.txt 2>err.txt # Separate files
# Discard output
command > /dev/null 2>&1
command &>/dev/null
# Read from file
command < input.txt
command << EOF
heredoc content
EOF
Terminal window
ls -la | grep txt # List and search
cat file | sort | uniq # Sort and remove duplicates
ps aux | grep nginx # Find nginx process
df -h | grep -v tmpfs # Exclude virtual filesystems
tail -f log | grep ERROR # Real-time error filtering
cat file | wc -l # Count lines
Terminal window
# Tee - split output to file and terminal
command | tee output.txt
# Pipe to root
echo "content" | sudo tee /etc/file
# xargs - build commands from input
find . -name "*.log" | xargs rm
ls | xargs -I {} echo "File: {}"
# Process substitution
diff <(cmd1) <(cmd2) # Compare command outputs
while read line; do echo $line; done < file.txt

Terminal window
# Basic find
find /path -name "filename"
find /path -name "*.log"
# Case insensitive
find /path -iname "filename"
# By type
find /path -type f # Regular files
find /path -type d # Directories
find /path -type l # Symbolic links
# By time
find /path -mtime -7 # Modified in last 7 days
find /path -atime +30 # Accessed more than 30 days ago
find /path -ctime -1 # Changed in last day
# By size
find /path -size +100M # Larger than 100MB
find /path -size -1G # Smaller than 1GB
# By permissions
find /path -perm 644 # Exact permissions
find /path -perm -u+x # Execute bit set
# Execute commands on results
find /path -name "*.log" -exec rm {} \;
find /path -name "*.log" -delete
# Combining conditions
find /path -name "*.txt" -and -size +1M
find /path \( -name "*.log" -o -name "*.tmp" \)
Terminal window
# Install mlocate first
sudo pacman -S mlocate
sudo updatedb
# Quick search (uses database)
locate filename
locate -i filename # Case insensitive
# Show only existing files
locate -e filename
Terminal window
which python # Path to executable
whereis python # Binary, source, man pages
type -a bash # All locations of command

Terminal window
# Sync and update
sudo pacman -Syu # Full system upgrade
sudo pacman -Sy # Sync database only
# Install/Remove
sudo pacman -S package_name
sudo pacman -R package_name
sudo pacman -Rs package_name # Remove with dependencies
# Search
pacman -Ss keyword # Search packages
pacman -Qs keyword # Search installed
pacman -Qi package_name # Package info
pacman -Ql package_name # List files
# Clean cache
sudo pacman -Scc # Clean cache completely
sudo pacman -Sc # Remove old packages
Terminal window
yay -S package_name # Install from AUR
yay -Ss keyword # Search AUR
yay -Yc # Clean unused deps
yay -Ytd # Remove unneeded dependencies
yay -Syu --devel # Update including AUR
Terminal window
# Service control
sudo systemctl start service
sudo systemctl stop service
sudo systemctl restart service
sudo systemctl reload service
# Enable/Disable at boot
sudo systemctl enable service
sudo systemctl disable service
# Status
systemctl status service
systemctl is-enabled service
systemctl is-active service
# List services
systemctl list-units --type=service
systemctl list-unit-files

Terminal window
# Modes: Normal (default), Insert (i), Command (:), Visual (v)
# Navigation
h j k l # Left, down, up, right
w / b # Word forward/back
0 / $ # Line start/end
gg / G # File start/end
:123 # Go to line 123
# Editing
i # Insert mode
a # Append after cursor
o / O # New line below/above
dd # Delete line
dw # Delete word
yy # Yank (copy) line
p # Paste
u # Undo
Ctrl+r # Redo
# Search and replace
:/pattern # Search forward
:?pattern # Search backward
:%s/old/new/g # Replace all
:%s/old/new/gc # Replace with confirmation
# Save and quit
:w # Save
:q # Quit
:wq / :x # Save and quit
:q! # Quit without saving
Terminal window
# Simple editor for beginners
nano file.txt
# Shortcuts shown at bottom (^ = Ctrl)
^O # Save (WriteOut)
^X # Exit
^W # Where is (search)
^K # Cut line
^U # Uncut (paste)
^C # Show cursor position

Terminal window
# Exercise 1: Navigation
cd / && ls -la
cd /var/log && pwd
cd ~ && cd -
# Exercise 2: File operations
touch test.txt
cp test.txt test_copy.txt
mv test_copy.txt test_new.txt
rm test_new.txt
# Exercise 3: Find and delete old files
find /tmp -type f -mtime +7 -delete
# Exercise 4: System info
uname -r
free -h
df -h
# Exercise 5: Process monitoring
ps aux --sort=-%cpu | head -5
ps aux --sort=-%mem | head -5
# Exercise 6: Log analysis
tail -n 100 /var/log/pacman.log | grep -i error
# Exercise 7: Text processing
cat /etc/passwd | awk -F: '{print $1, $7}'

In this chapter, you learned:

  • ✅ File system navigation (cd, ls, pwd, tree)
  • ✅ File operations (touch, cp, mv, rm, mkdir, tar)
  • ✅ Viewing file contents (cat, less, head, tail)
  • ✅ Text processing (grep, sed, awk)
  • ✅ System information commands
  • ✅ Pipes and redirection
  • ✅ Finding files (find, locate)
  • ✅ Arch Linux specific commands (pacman, yay)
  • ✅ Basic text editors (vim, nano)

Chapter 6: User Management Commands


Last Updated: February 2026