Mastering Linux File System: A Beginner-Friendly Guide


Linux is a powerful and versatile operating system, widely known for its robust file system and security features. Whether you're a beginner or someone looking to refresh your understanding, this guide will break down the complexities of the Linux file system and help you become more confident in navigating it.
We’ll cover everything from the Linux directory structure to file permissions, users, groups, and more—all explained in simple terms.
1. Understanding the Linux Directory Structure
The Linux file system follows a tree-like structure that begins at the root (/
). Below are key directories and their purposes:
Directory | Purpose |
/ | Root directory, the starting point of the file system. |
/bin | Essential user command binaries (like ls , cat ). |
/boot | Files needed for booting the system. |
/dev | Device files (like hard drives and USB devices). |
/etc | System configuration files. |
/home | Home directories for users. |
/lib | Essential shared libraries. |
/media | Mount points for removable media. |
/mnt | Temporary mount points. |
/opt | Optional application software packages. |
/proc | System information as virtual files. |
/root | Home directory for the root user. |
/sbin | System administration binaries. |
/tmp | Temporary files. |
/usr | User utilities and applications. |
/var | Variable files like logs. |
By understanding these directories, you’ll know where to find system files and user data.
2. File Permissions in Linux
File permissions determine who can read, write, or execute a file. Each file has three sets of permissions:
Owner: The user who created the file.
Group: A collection of users.
Others: Everyone else.
To view file permissions, use the ls -l
command:
ls -l
Understanding the Output
Example:
-rw-r--r-- 1 user group 1024 Jan 01 12:00 example.txt
The breakdown:
-rw-r--r--
: File type and permissions.-
: Regular file.rw-
: Owner can read and write.r--
: Group can read.r--
: Others can read.
1
: Number of links.user
: File owner.group
: File group.1024
: File size in bytes.Jan 01 12:00
: Last modified date.example.txt
: File name.
Changing File Permissions
To change file permissions, use the chmod
command:
chmod 755 example.txt
Permission values:
Read (r) = 4
Write (w) = 2
Execute (x) = 1
Changing Ownership and Group
To change file ownership, use chown
:
sudo chown newuser:newgroup example.txt
Example: Suppose a file was created by user alice
, but user bob
needs to manage it. Changing ownership to bob
ensures that bob
can handle file operations:
sudo chown bob:bob example.txt
To change only the group ownership:
sudo chown :newgroup example.txt
This command ensures that all members of newgroup
can access the file.
Additionally, to recursively change ownership in a directory:
sudo chown -R newuser:newgroup /path/to/directory
This command updates ownership for all files and subdirectories.
3. Users and Groups in Linux
Managing Users
To add a user with adduser
:
sudo adduser username
Alternative command using useradd
:
sudo useradd -m -s /bin/bash username
-m
: Create a home directory.-s
: Specify the default shell.
To delete a user using deluser
:
sudo deluser username
Alternative command using userdel
:
sudo userdel -r username
-r
: Remove the user’s home directory.
Managing Groups
To create a group:
sudo groupadd groupname
To add a user to a group:
sudo usermod -aG groupname username
To view groups a user belongs to:
groups username
To remove a user from a group:
sudo gpasswd -d username groupname
4. Understanding File Types in Linux
Linux files are categorized into several types:
File Type | Symbol | Description |
Regular | - | Normal files (text, images, etc.) |
Directory | d | Folder containing files |
Symbolic Link | l | Shortcut to another file or directory |
Character Device | c | Hardware devices that send or receive data one character at a time |
Block Device | b | Hardware devices that read/write data in blocks |
Socket | s | Communication endpoint |
Named Pipe | p | Special file for inter-process communication |
Creating and Managing Links
Symbolic Links (Soft Links)
A symbolic link is like a shortcut that points to another file or directory.
To create a symbolic link:
ln -s /path/to/original /path/to/symlink
Example:
ln -s /var/www/html my_website_link
To remove a symbolic link:
rm my_website_link
Hard Links
A hard link creates another name for the same file on disk.
To create a hard link:
ln /path/to/original /path/to/hardlink
A key difference between hard and symbolic links is that hard links continue to function even if the original file is moved or deleted (as long as the hard link exists).
5. File and Directory Operations
Below are common commands for managing files and directories in Linux:
Task | Command |
List files | ls (or ls -a to view hidden files) |
Create directory | mkdir dirname (use -p to create nested directories) |
Remove file | rm filename |
Remove directory | rm -r dirname (or rmdir dirname for empty directory) |
Copy file | cp source destination |
Move file | mv source destination |
View file content | cat filename (or less filename for paginated viewing) |
Search for files | find /path -name filename |
Display current directory | pwd |
6. The Importance of sudo
Users
The sudo
command allows users to perform administrative tasks with elevated privileges.
Example:
sudo apt update
To become the root user:
sudo su
To add a user to the sudo
group (giving them admin privileges):
sudo usermod -aG sudo username
Why Use sudo
Instead of Root Directly?
Using sudo
is more secure than directly logging in as the root user because:
Logging:
sudo
actions are logged, making it easier to audit system changes.Temporary Access: Users only have elevated privileges when necessary.
Reduced Risks: Accidental commands can be avoided.
Sudoers File
The sudoers
file controls who can run sudo
. To edit it, use:
sudo visudo
This prevents syntax errors that could lock you out of administrative access.
Example entry in the sudoers
file:
username ALL=(ALL) NOPASSWD: ALL
This entry allows username
to run any command without entering a password.
Always be cautious when using
sudo
, as it can impact critical system files.
Final Thought
The Linux file system may seem complex initially, but with practice, you'll gain confidence in navigating directories, managing files, and understanding permissions.
Understanding the basics of users, groups, and file types will not only make you a proficient Linux user but also prepare you for more advanced tasks.
Start exploring today, and soon, Linux will feel like second nature!
Subscribe to my newsletter
Read articles from Soumo Sarkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
