`

File Systems

A structured way to store and manage files. Acts as a interface between the OS and the storage device.

Types of File Systems

File System Components

In Linux, the file system organizes files and directories into a hierarchical structure.

Directory Description
/ Root Directory: The top-level directory in the file system hierarchy.
/bin Binaries: Contains essential binary executables like ls, cp, mv.
/boot Boot Files: Contains boot loader files and the kernel image.
/dev Devices: Contains device files for hardware devices.
/etc Configuration Files: Contains system-wide configuration files. passwd, fstab
/home Home Directories: Contains user home directories.
/lib Libraries: Contains shared libraries used by binary executables in /bin and /sbin.
/media Removable Media: Mount point for removable media like USBs.
/mnt Mount Point: Temporary mount point, when manually mounting a storage.
/opt Optional: Contains optional software packages.
/proc Process Information: Virtual filesystem with information about the system processes and hardware.
/root Root User: Home directory for the root user.
/sbin System Binaries: Contains essential system binaries like init, shutdown.
/tmp Temporary Files: Contains temporary files created by applications.
/usr User Binaries: Contains user binaries, libraries, documentation.
/var Variable Files: Contains variable data like logs, mail, cache.

File and Directory Permissions

In Linux, file permissions define who can read, write or execute a file.

Each file and directory has 3 permissions for groups, User(u) -> owner, Group(g) -> group memebers that share access, Others(o) -> all teh other users.

Permissions are represented by 3 characters:

Permissions are represented by 3 sets of characters:

Commands for File and Permission Manipulation

Basic File Commands

ls

List files and directories.

ls -l # Long listing format
ls -a # List all files including hidden files
ls -lh # Human-readable file sizes

touch

Create an empty file.

touch file.txt
touch file1.txt file2.txt

cat

Concatenate and display file content.

cat file.txt
cat file1.txt file2.txt
cat > file.txt # Create a file and write content
cat >> file.txt # Append content to a file
cat file.txt | less # Display file content page by page

cp

Copy files and directories.

cp file.txt newfile.txt
cp -r dir1 dir2 # Copy directories recursively

mv

Move or rename files and directories.

mv file.txt newfile.txt
mv file.txt dir/ # Move file to a directory

rm

Remove files and directories.

rm file.txt
rm -r dir/ # Remove directories recursively

Viewing and Modifying Permission

chmod

Change file or directory permissions.

Numeric Mode: chmod 755 file.txt -> rwxr-xr-x Symbolic Mode: chmod u+r file.txt -> Adds read permission for the owner.

Symbolic Mode Syntax:

chmod u+x file.txt # Add execute permission for the owner
chmod g-w file.txt # Remove write permission for the group
chmod o=r file.txt # Set read permission for others
chmod 755 file.txt # Set permissions using numeric mode

Ownership Management

chown

Change file or directory owner.

chown user:group file.txt # Change owner and group
chown user file.txt # Change owner
chown :group file.txt # Change group

chgrp

Change file or directory group.

chgrp group file.txt # Change group
chgrp :group file.txt # Change group

umask

Sets the default file permissions when a file is created umask is reponsible to set its permissions.

How to read umask?

If the umask is set to 022, the default permissions for files will be 644 and for directories will be 755. Which is by default in most of the systems.

stat

Displays detailed information about a file, including its permissions.

stat file.txt
stat -c %a file.txt # Display only the permissions