Linux Basics

The Linux File System

Unlike Windows (which uses drive letters like C:\ and D:\), Linux uses a single unified directory tree. Everything — files, hardware devices, running processes — hangs from a single root node: /.


The Directory Tree

text
/
├── bin/          → Essential user binaries (ls, cp, mv, bash)
├── boot/         → Bootloader files, Linux kernel image
├── dev/          → Device files (ttyUSB0, i2c-1, sda)
├── etc/          → System-wide config files
├── home/         → Home directories for all users
│   └── rajath/   → Your personal workspace (~)
├── lib/          → Shared libraries for /bin and /sbin
├── media/        → Mount points for USB drives, SD cards
├── mnt/          → Temporary mount points
├── opt/          → Optional, self-contained software
├── proc/         → Virtual filesystem — live kernel and process info
├── root/         → Home directory for the root user
├── sbin/         → System binaries (only root needs these)
├── srv/          → Data for services (web servers, FTP)
├── sys/          → Kernel device and subsystem info
├── tmp/          → Temporary files (cleared on reboot)
├── usr/          → User-space applications and libraries
│   ├── bin/      → Non-essential user binaries (python3, git)
│   ├── lib/      → Libraries for /usr/bin
│   └── local/    → Locally compiled software (user-installed)
└── var/          → Variable data: logs, databases, mail queues
    └── log/      → System log files (syslog, dmesg, auth.log)

Key Directories for Embedded Developers

DirectoryWhy It Matters
/dev/ttyUSB0ESP32 or Arduino via USB-to-UART adapter
/dev/ttyACM0ST-Link, Arduino Uno native USB
/dev/i2c-1I2C bus device file on Raspberry Pi
/dev/spidev0.0SPI bus device file
/proc/cpuinfoCPU info — works on Pi to check core count, model
/sys/class/gpio/GPIO control without extra libraries
/var/log/syslogSystem log — where kernel USB events appear

pwd — Where Am I?

bash
pwd
# /home/rajath/projects

ls — What's Here?

bash
ls                    # Basic list
ls -l                 # Long format (permissions, size, owner, date)
ls -la                # Include hidden files (starting with .)
ls -lh                # Human-readable file sizes (KB, MB)
ls -lt                # Sort by modification time (newest first)
ls /dev/tty*          # List all serial port devices
ls *.c                # List only .c files in current directory

Reading ls -l output:

text
-rw-r--r--  1  rajath  rajath   4096  May 9 09:00  main.c
↑            ↑  ↑       ↑        ↑                   ↑
Type+Perms  Links Owner  Group   Size                Name

cd — Change Directory

bash
cd Documents              # Relative path (from current location)
cd /home/rajath/esp       # Absolute path (from root)
cd ~                      # Go to home directory
cd ..                     # Go up one level
cd ../..                  # Go up two levels
cd -                      # Go back to the previous directory

tree — Visual Directory View

bash
sudo apt install tree     # Install if not present

tree                      # Show current directory structure
tree -L 2                 # Limit depth to 2 levels
tree -a                   # Show hidden files too
tree /dev/                # Show device files

File and Directory Operations

Creating Files and Directories

bash
mkdir esp_projects                      # Create a single directory
mkdir -p ~/projects/esp32/firmware      # Create all parent directories too
touch main.c                            # Create an empty file
touch notes.txt sensor_log.csv          # Create multiple files at once

# Create a file with content
echo "Hello, ESP32!" > hello.txt        # Write to file (overwrites)
echo "Second line" >> hello.txt         # Append to file
cat hello.txt                           # Display file contents

Copying Files

bash
cp main.c backup_main.c                  # Copy file
cp -r esp_projects/ esp_projects_backup/ # Copy entire directory recursively
cp *.c src/                             # Copy all .c files into src/

Moving and Renaming

bash
mv old_name.c new_name.c                # Rename a file
mv firmware.bin /media/sdcard/          # Move to a different location
mv *.bin build/                         # Move all .bin files to build/

Deleting Files

No Recycle Bin in Linux

rm deletes permanently. There is no undo. Always double-check what you are deleting, especially when using wildcards or -rf.

bash
rm old_file.c                           # Delete a single file
rm *.log                                # Delete all .log files
rm -r old_project/                      # Delete a directory and all contents
rm -rf build/                           # Force delete without confirmation
rmdir empty_dir/                        # Remove only if the directory is empty

Viewing Files

bash
cat main.c                              # Print entire file to terminal
less main.c                             # Page through a long file (q to quit)
head -20 sensor_log.csv                 # First 20 lines
tail -50 /var/log/syslog                # Last 50 lines
tail -f /var/log/syslog                 # Live tail (follow new lines as they arrive)
wc -l main.c                            # Count lines in a file
wc -w main.c                            # Count words

Linking Files

bash
# Soft (symbolic) link — a shortcut that points to another file
ln -s /home/rajath/esp/esp-idf idf
ls -l idf    # idf -> /home/rajath/esp/esp-idf

# Hard link — two names for the same file on disk
ln original.c hardlink.c

Searching for Files and Content

find — Search by Name, Type, Size, Date

bash
# Find all .c files from current directory downward
find . -name "*.c"

# Find all files modified in the last 24 hours
find . -mtime -1

# Find files larger than 100 MB
find / -size +100M 2>/dev/null

# Find and delete all .o (object) files
find . -name "*.o" -delete

# Find all directories named "build"
find . -type d -name "build"

# Find files owned by a specific user
find /home -user rajath

# Find with exec — run a command on each result
find . -name "*.log" -exec rm {} \;

grep — Search Inside Files

bash
# Search for "error" in a file
grep "error" sensor_log.txt

# Case-insensitive search
grep -i "error" sensor_log.txt

# Show line numbers
grep -n "void main" main.c

# Search recursively in all files in a directory
grep -r "GPIO_PIN" ./src/

# Show lines that do NOT match (inverse match)
grep -v "debug" app.log

# Show context: 3 lines before and after each match
grep -C 3 "segfault" /var/log/syslog

# Count matching lines
grep -c "error" app.log

# Search for multiple patterns
grep -E "error|warning|fail" app.log

# Pipe output to grep
dmesg | grep "USB"
cat /var/log/syslog | grep "ttyUSB"

locate — Fast File Name Search (uses indexed database)

bash
sudo apt install mlocate
sudo updatedb               # Build the index
locate esp-idf              # Instantly find all files with "esp-idf" in the name

Disk and Storage

bash
df -h                       # Disk space usage for all mounted filesystems
df -h /home                 # Disk space for a specific path
du -sh ~/projects           # Size of a directory
du -sh * | sort -h          # Size of each item, sorted smallest to largest

# Mount and unmount USB drives
lsblk                       # List block devices (disks, partitions)
sudo mount /dev/sdb1 /mnt/usb    # Mount USB drive
sudo umount /mnt/usb             # Unmount safely

Paths: Absolute vs. Relative

TypeExampleMeaning
Absolute/home/rajath/esp/main.cFull path from root — works from anywhere
Relative./esp/main.cRelative to current directory
Home shortcut~/esp/main.cExpands to /home/<username>/esp/main.c
Parent../sibling/main.cOne level up, then into sibling directory

Special Files in /dev

Linux represents hardware as files. This is fundamental to how embedded tools communicate with devices.

bash
ls /dev/tty*                # All terminal and serial devices
ls -la /dev/ttyUSB0         # ESP32 via USB-UART (check permissions)
ls -la /dev/ttyACM0         # Arduino, ST-Link

# Check if you are in the dialout group (required to access serial ports)
groups $USER

# Add yourself to the dialout group (log out and back in after this)
sudo usermod -aG dialout $USER

When you plug in an ESP32 or ST-Link, the kernel creates a device file automatically. You can see the kernel message with:

bash
dmesg | tail -10
# [ 4523.192] usb 1-1.2: new full-speed USB device number 4 using xhci_hcd
# [ 4523.350] usb 1-1.2: cp210x converter now attached to ttyUSB0
Previous
What is Linux?