This tutorial provides a comprehensive guide to the Linux directory structure, focusing on the root directory (/) and common subdirectories like /home, /var, /etc, /bin, /usr, and /dev. Each step builds on the previous one, with practical examples and engaging exercises to reinforce learning.
Step 1: Introduction to the Root Directory (/)
The root directory (/) is the top-level directory in the Linux file system hierarchy. All other directories and files are organized under it. Think of it as the foundation of the file system.
- Key Point: Every file and directory in Linux starts from /.
- Real-World Example: Just like a tree has a single trunk from which branches grow, / is the trunk of the Linux file system.
# List the contents of the root directory
ls /Practice Exercise
Use the ls command to explore the root directory. Identify at least three subdirectories and write down their names. What do you think these directories might be used for?
Show Solution
# Example output of the ls / command
bin dev home lib media opt root sbin sys usr
boot etc init lib64 mnt proc run srv tmp var
# Comments:
# - /bin: Contains essential command binaries.
# - /home: Stores user home directories.
# - /etc: Holds system configuration files.Step 2: Exploring /home: User Home Directories
The /home directory contains personal directories for each user on the system. Each user has a subdirectory under /home named after their username.
- Key Point: /home is where users store their personal files and configurations.
- Real-World Example: Imagine /home as a neighborhood where each house (directory) belongs to a different user.
# Navigate to the /home directory
cd /home
# List the contents
lsPractice Exercise
Create a new directory under /home named projects. Inside it, create a file called notes.txt. Write a short note about your favorite Linux command in the file.
Show Solution
# Create the projects directory
mkdir /home/your_username/projects
# Create and edit the notes.txt file
echo 'My favorite Linux command is ls because it helps me explore directories.' > /home/your_username/projects/notes.txt
# Verify the file content
cat /home/your_username/projects/notes.txtStep 3: Understanding /etc: Configuration Files
The /etc directory contains system-wide configuration files and scripts. These files control how the system and applications behave.
- Key Point: /etc is crucial for system administration and customization.
- Real-World Example: Think of /etc as the control panel of your Linux system.
# List some configuration files in /etc
ls /etcPractice Exercise
Open the /etc/passwd file using the cat command. Identify the line corresponding to your user account. What information does it contain?
Show Solution
# View the /etc/passwd file
cat /etc/passwd
# Example output for a user:
your_username:x:1000:1000:Your Name,,,:/home/your_username:/bin/bash
# Comments:
# - 'your_username': Username
# - 'x': Password placeholder
# - '1000': User ID
# - '1000': Group ID
# - 'Your Name,,,': User description
# - '/home/your_username': Home directory
# - '/bin/bash': Default shellStep 4: Navigating /var: Variable Data
The /var directory stores variable data files, such as logs, databases, and temporary files. These files change frequently as the system runs.
- Key Point: /var is dynamic and often grows in size.
- Real-World Example: /var is like a journal that records system activities and stores temporary data.
# List the contents of /var
ls /varPractice Exercise
Check the size of the /var/log directory using the du command. Which log file is the largest? What might it be used for?
Show Solution
# Check the size of /var/log
du -sh /var/log
# Identify the largest file
ls -lh /var/log | grep -v ^d | sort -rh | head -n 1
# Comments:
# - The largest file might be 'syslog' or 'auth.log', which store system and authentication logs.Step 5: Exploring /bin and /usr: Essential and User Binaries
The /bin directory contains essential command binaries needed for system boot and repair. The /usr directory holds user-installed software and libraries.
- Key Point: /bin is for core system commands, while /usr is for additional software.
- Real-World Example: /bin is like a toolbox with basic tools, while /usr is a workshop with advanced tools.
# List the contents of /bin
ls /bin
# List the contents of /usr/bin
ls /usr/binPractice Exercise
Use the which command to find the location of the ls command. Is it in /bin or /usr/bin? Why do you think it's located there?
Show Solution
# Find the location of the ls command
which ls
# Example output:
/bin/ls
# Comments:
# - The `ls` command is in `/bin` because it's an essential command needed for basic system operations.Step 6: Understanding /dev: Device Files
The /dev directory contains device files that represent hardware components, such as disks, terminals, and peripherals.
- Key Point: /dev provides an interface to interact with hardware.
- Real-World Example: /dev is like a control panel for hardware devices.
# List the contents of /dev
ls /devPractice Exercise
Use the ls /dev command to identify the device file for your primary hard drive (usually sda). What other device files can you recognize?
Show Solution
# List device files
ls /dev
# Example output:
sda sda1 tty tty1
# Comments:
# - `sda`: Primary hard drive
# - `sda1`: First partition on the hard drive
# - `tty`: Terminal device
# - `tty1`: First virtual consoleStep 7: Putting It All Together: Navigating the Linux File System
Now that you understand the key directories, let's practice navigating the file system. Use commands like cd, ls, and pwd to explore and understand the relationships between directories.
- Key Point: Mastering navigation is essential for efficient Linux usage.
- Real-World Example: Navigating the file system is like exploring a city with a map.
# Navigate to /usr/bin and list its contents
cd /usr/bin
ls
# Return to the root directory
cd /
# Print the current directory
pwdPractice Exercise
Create a script called explore.sh that navigates through /, /home, /etc, and /var, lists their contents, and saves the output to a file called explore_log.txt. Run the script and review the log file.
Show Solution
# Create the explore.sh script
echo '#!/bin/bash
cd /
ls > explore_log.txt
cd /home
ls >> explore_log.txt
cd /etc
ls >> explore_log.txt
cd /var
ls >> explore_log.txt' > explore.sh
# Make the script executable
chmod +x explore.sh
# Run the script
./explore.sh
# View the log file
cat explore_log.txtSign in to take Cornell notes on this lesson — they save automatically and stay with your account.