Skip to main content

Linux Fundamentals

Master the essential Linux commands and file system navigation that every developer needs. Linux File System

The Linux File System

Everything in Linux starts from the root directory /.

Key Directories

/           # Root - top of the hierarchy
/home       # User home directories
/etc        # Configuration files
/var        # Variable data (logs, websites)
/usr        # User programs and data
/bin        # Essential binaries
/sbin       # System binaries
/tmp        # Temporary files
/opt        # Optional software
/dev        # Device files

# Print working directory
pwd

# List files
ls              # Basic listing
ls -l           # Long format (permissions, size, date)
ls -a           # Show hidden files
ls -lh          # Human-readable sizes
ls -lah         # All options combined

# Change directory
cd /var/log     # Absolute path
cd logs         # Relative path
cd ..           # Parent directory
cd ~            # Home directory
cd -            # Previous directory

# Show directory tree
tree            # Install with: apt install tree
tree -L 2       # Limit depth to 2 levels

File Operations

Creating Files and Directories

# Create empty file
touch file.txt

# Create directory
mkdir mydir
mkdir -p path/to/nested/dir  # Create parent directories

# Create multiple directories
mkdir dir1 dir2 dir3

Copying and Moving

# Copy files
cp source.txt dest.txt
cp -r sourcedir/ destdir/    # Copy directory recursively
cp -i file.txt backup.txt    # Interactive (prompt before overwrite)

# Move/rename
mv oldname.txt newname.txt
mv file.txt /path/to/destination/
mv *.txt documents/          # Move all .txt files

Deleting

# Remove files
rm file.txt
rm -i file.txt    # Interactive
rm -f file.txt    # Force (no prompt)

# Remove directories
rm -r directory/
rm -rf directory/ # Force recursive (DANGEROUS!)

# Remove empty directory
rmdir emptydir/
Be extremely careful with rm -rf! There’s no recycle bin in Linux. Deleted files are gone forever.

Viewing File Contents

# Display entire file
cat file.txt

# Display with line numbers
cat -n file.txt

# Concatenate multiple files
cat file1.txt file2.txt > combined.txt

# Page through file
less file.txt    # Use arrows, q to quit
more file.txt    # Older pager

# First/last lines
head file.txt         # First 10 lines
head -n 20 file.txt   # First 20 lines
tail file.txt         # Last 10 lines
tail -n 50 file.txt   # Last 50 lines
tail -f /var/log/syslog  # Follow file updates (live)

# Word count
wc file.txt           # Lines, words, bytes
wc -l file.txt        # Just line count

Finding Files

Using find

# Find by name
find /path -name "*.txt"
find . -name "config.json"

# Find by type
find /var -type f    # Files only
find /var -type d    # Directories only

# Find by size
find . -size +100M   # Larger than 100MB
find . -size -1k     # Smaller than 1KB

# Find and execute
find . -name "*.log" -delete
find . -name "*.txt" -exec cat {} \;

# Find modified in last 7 days
find . -mtime -7

Using locate

# Fast file search (uses database)
locate filename

# Update database
sudo updatedb

# Case-insensitive search
locate -i filename

Using grep to search inside files

# Search for text in file
grep "error" logfile.txt

# Case-insensitive
grep -i "error" logfile.txt

# Recursive search in directory
grep -r "TODO" /path/to/code/

# Show line numbers
grep -n "error" logfile.txt

# Count matches
grep -c "error" logfile.txt

# Invert match (lines NOT containing pattern)
grep -v "success" logfile.txt

File Permissions Preview

# View permissions
ls -l

# Output format:
# -rw-r--r-- 1 user group 1234 Dec 2 10:00 file.txt
# │││││││││
# │││││││└└ Other permissions (read, write, execute)
# ││││││└└└ Group permissions
# │││└└└└└└ Owner permissions
# ││└─────── Number of links
# │└──────── File type (- = file, d = directory)
We’ll cover permissions in detail in the next module.

Practical Examples

Example 1: Organizing Files

# Create project structure
mkdir -p project/{src,docs,tests}
cd project

# Create files
touch src/main.py src/utils.py
touch docs/README.md
touch tests/test_main.py

# View structure
tree

Example 2: Finding Large Files

# Find files larger than 100MB
find /home -type f -size +100M -exec ls -lh {} \; 2>/dev/null

# Or use du
du -h /home | sort -rh | head -20

Example 3: Searching Logs

# Find errors in log file
grep -i "error" /var/log/syslog

# Count error occurrences
grep -c "error" /var/log/syslog

# Show errors with context
grep -C 3 "error" /var/log/syslog  # 3 lines before and after

Useful Shortcuts

# Tab completion
cd /var/l[TAB]  # Completes to /var/log

# Command history
history         # Show command history
!123            # Run command #123 from history
!!              # Run last command
!$              # Last argument of previous command

# Ctrl shortcuts
Ctrl+C          # Cancel current command
Ctrl+D          # Exit shell
Ctrl+L          # Clear screen
Ctrl+R          # Search command history
Ctrl+A          # Move to beginning of line
Ctrl+E          # Move to end of line

Wildcards and Patterns

# * matches any characters
ls *.txt        # All .txt files
rm file*        # All files starting with "file"

# ? matches single character
ls file?.txt    # file1.txt, fileA.txt, etc.

# [] matches character range
ls file[1-3].txt    # file1.txt, file2.txt, file3.txt
ls [A-Z]*.txt       # Files starting with capital letter

# {} for multiple patterns
cp file.{txt,md} backup/  # Copy file.txt and file.md

Redirection and Pipes

# Output redirection
echo "Hello" > file.txt     # Overwrite
echo "World" >> file.txt    # Append

# Input redirection
wc -l < file.txt

# Pipes (chain commands)
cat file.txt | grep "error" | wc -l
ls -l | less
ps aux | grep python

# Redirect stderr
command 2> error.log
command 2>&1  # Redirect stderr to stdout

Key Takeaways

  • Linux file system starts at / (root)
  • Use ls, cd, pwd for navigation
  • cp, mv, rm for file operations
  • cat, less, head, tail to view files
  • find and grep to search
  • Tab completion and history save time
  • Pipes (|) chain commands together

Next: Linux Permissions & Users →