Mastering Linux File System: A Beginner-Friendly Guide

Soumo SarkarSoumo Sarkar
6 min read

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:

DirectoryPurpose
/Root directory, the starting point of the file system.
/binEssential user command binaries (like ls, cat).
/bootFiles needed for booting the system.
/devDevice files (like hard drives and USB devices).
/etcSystem configuration files.
/homeHome directories for users.
/libEssential shared libraries.
/mediaMount points for removable media.
/mntTemporary mount points.
/optOptional application software packages.
/procSystem information as virtual files.
/rootHome directory for the root user.
/sbinSystem administration binaries.
/tmpTemporary files.
/usrUser utilities and applications.
/varVariable 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 TypeSymbolDescription
Regular-Normal files (text, images, etc.)
DirectorydFolder containing files
Symbolic LinklShortcut to another file or directory
Character DevicecHardware devices that send or receive data one character at a time
Block DevicebHardware devices that read/write data in blocks
SocketsCommunication endpoint
Named PipepSpecial file for inter-process communication

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

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:

TaskCommand
List filesls (or ls -a to view hidden files)
Create directorymkdir dirname (use -p to create nested directories)
Remove filerm filename
Remove directoryrm -r dirname (or rmdir dirname for empty directory)
Copy filecp source destination
Move filemv source destination
View file contentcat filename (or less filename for paginated viewing)
Search for filesfind /path -name filename
Display current directorypwd

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!

0
Subscribe to my newsletter

Read articles from Soumo Sarkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Soumo Sarkar
Soumo Sarkar