DEV Community

Cover image for 🐧 Deep Dive into the Linux File System: A Beginner-Friendly Guide
DevOps Man
DevOps Man

Posted on

1

🐧 Deep Dive into the Linux File System: A Beginner-Friendly Guide

Linux is known for its powerful file system and command-line tools. Whether you're an aspiring DevOps engineer or just curious about Linux internals, understanding the file system is foundational.

In this guide, we'll explore everything from the Linux file hierarchy to file manipulation commands.


πŸ“ 1. What is the Linux File System?

The Linux file system is a structured arrangement for storing and organizing data. Everything in Linux is treated as a file, whether it’s a document, directory, or hardware device.


🌲 2. File Hierarchy System

Linux follows a hierarchical file structure starting from the root directory /.
/
β”œβ”€β”€ bin/ β†’ Essential user binaries
β”œβ”€β”€ boot/ β†’ Boot loader files
β”œβ”€β”€ dev/ β†’ Device files
β”œβ”€β”€ etc/ β†’ Configuration files
β”œβ”€β”€ home/ β†’ User directories
β”œβ”€β”€ lib/ β†’ Essential shared libraries
β”œβ”€β”€ tmp/ β†’ Temporary files
β”œβ”€β”€ usr/ β†’ User programs
β”œβ”€β”€ var/ β†’ Variable data (logs, spool)


πŸ›£οΈ 3. Absolute vs Relative Path

πŸ“Œ Absolute Path
An absolute path always starts from the root directory / and points to a file or directory regardless of the current working directory.

/home/username/docs/file.txt
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Relative Path
A relative path starts from the current directory (denoted by .) and is relative to where you are in the filesystem.

./docs/file.txt
Enter fullscreen mode Exit fullscreen mode

πŸ”— 4. Hard Links, Inodes, and Symbolic Links

πŸ“¦ Inodes:
Every file has an inode storing metadata (permissions, size, timestamps, etc.)

πŸ” Hard Link:
Creates another name pointing to the same inode.

ln original.txt hardlink.txt
Enter fullscreen mode Exit fullscreen mode

πŸ”— Symbolic (Soft) Link:
Points to the file path, not the inode.

ln -s original.txt symlink.txt
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ 5. Useful File System Commands

πŸ“„ ls – List Files

ls -l          # Long listing
ls -a          # Include hidden files
Enter fullscreen mode Exit fullscreen mode

πŸ•’ atime, mtime, ctime

  • atime: Last accessed

  • mtime: Last modified

  • ctime: Last changed (metadata)

Check using:

stat filename
Enter fullscreen mode Exit fullscreen mode

Update time using:

touch filename
Enter fullscreen mode Exit fullscreen mode

Check date/time:

date
Enter fullscreen mode Exit fullscreen mode

πŸ”ƒ Sorting Files

ls -lt      # Sort by modified time
ls -lSr     # Sort by size, reverse
Enter fullscreen mode Exit fullscreen mode

🐈 File Viewing: cat, less, tail, head, watch

cat file.txt
less file.txt
tail -f file.txt        # Follow file
head -n 10 file.txt
watch -n 2 ls -l        # Refresh every 2s
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Create Files & Dirs: touch, mkdir

touch newfile.txt
mkdir newfolder
Enter fullscreen mode Exit fullscreen mode

βœ‚οΈ File Ops: cp, mv, rm, shred

cp file1.txt backup/
mv file.txt renamed.txt
rm unwanted.txt
shred -u sensitive.txt  # Secure delete
Enter fullscreen mode Exit fullscreen mode

πŸ”— Pipes and Word Count: |, wc

cat file.txt | wc -l
Enter fullscreen mode Exit fullscreen mode

βž• Redirection: >, >>, 2>, &>, cut, tee

echo "Hello" > file.txt       # Overwrite
echo "World" >> file.txt      # Append
command 2> error.log          # Errors only
command &> all.log            # Stdout + stderr
cut -d':' -f1 /etc/passwd
command | tee output.log      # View + save
Enter fullscreen mode Exit fullscreen mode

πŸ” which & plocate

which bash
plocate file.txt
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž find with exec

find . -name "*.log" -exec rm {} \;
Enter fullscreen mode Exit fullscreen mode

πŸ” grep – Search Text

grep "error" logfile.txt
Enter fullscreen mode Exit fullscreen mode

πŸ”€ strings – Extract printable text from binary

strings binaryfile
Enter fullscreen mode Exit fullscreen mode

πŸ” Compare Files: cmp, diff, sha256sum

cmp file1.txt file2.txt
diff file1.txt file2.txt
sha256sum file.txt
Enter fullscreen mode Exit fullscreen mode

✍️ VIM Editor Basics

Open file:

vim file.txt
Enter fullscreen mode Exit fullscreen mode

Common commands:

  • i: Insert mode

  • Esc: Exit insert

  • :w: Save

  • :q: Quit

  • :wq: Save and quit


πŸ“¦ Archiving: tar and gzip

Create archive:

tar -cvf archive.tar folder/
Enter fullscreen mode Exit fullscreen mode

Extract:

tar -xvf archive.tar
Enter fullscreen mode Exit fullscreen mode

Compress:

gzip file.txt
gunzip file.txt.gz
Enter fullscreen mode Exit fullscreen mode

πŸ”š Conclusion

The Linux file system is a powerful and flexible system, and the commands above will help you navigate and control it like a pro. If you're pursuing DevOps, mastering these tools is essential.

Top comments (0)