Unix - UNIX File System

1. What is the UNIX File System?

  • The UNIX file system is a hierarchical (tree-like) structure that organizes all files and directories starting from the root directory /.

  • In UNIX, everything is treated as a file:

    • Regular files (text, data, programs)

    • Directories (which are also files containing other files)

    • Devices (/dev/sda, /dev/tty)

    • Pipes and sockets


2. Structure of the File System

At the top is the root directory /, from which everything else branches out.

Common directories in UNIX:

  • / → Root directory

  • /bin → Essential binaries (commands like ls, cp, mv)

  • /boot → Boot loader files and kernel

  • /dev → Device files (hard drives, USBs, terminals)

  • /etc → System configuration files

  • /home → Home directories for users

  • /lib → Shared libraries and kernel modules

  • /tmp → Temporary files

  • /usr → User programs and data (like /usr/bin, /usr/lib)

  • /var → Variable data (logs, mail, spool)


3. Example: Navigating the File System

# Start at root
cd /

# List root contents
ls
# Output may look like:
# bin  boot  dev  etc  home  lib  tmp  usr  var

# Move into home directory
cd /home/username
ls
# Documents  Downloads  Pictures

# Create a new directory
mkdir /home/username/testdir

# Create a new file inside it
echo "Hello UNIX" > /home/username/testdir/file1.txt

# Check file details
ls -l /home/username/testdir
# -rw-r--r-- 1 username users 11 Aug 16  file1.txt

4. File Types in UNIX

Use ls -l to see file types:

  • - Regular file (text, binary, program)

  • d Directory

  • l Symbolic link (shortcut)

  • c Character device file (e.g., /dev/tty)

  • b Block device file (e.g., /dev/sda)

  • p Named pipe

  • s Socket

Example:

ls -l /dev/sda
# brw-rw---- 1 root disk 8, 0 Aug 16  /dev/sda

Here, b means it’s a block device file (hard disk).


5. Important File System Commands

  • pwd → Show current working directory

  • ls -l → List files with details

  • cd dir → Change directory

  • mkdir dir → Create directory

  • rmdir dir → Remove empty directory

  • rm file → Remove file

  • cp source dest → Copy file

  • mv source dest → Move/rename file

  • cat file → View file contents

  • file filename → Find file type


6. Diagram – UNIX File System

/
├── bin       (essential commands: ls, cp, mv)
├── boot      (kernel, boot loader files)
├── dev       (device files: sda, tty)
├── etc       (configuration files)
├── home
│   ├── alice
│   │   └── file1.txt
│   └── bob
├── lib       (libraries)
├── tmp       (temporary files)
├── usr
│   ├── bin   (user commands)
│   ├── lib   (user libraries)
│   └── share (shared data)
└── var       (logs, mail, spool)

Summary

  • UNIX uses a single-root hierarchical file system.

  • Everything (files, directories, devices) is treated as a file.

  • Standard directories (/bin, /etc, /home, /usr, /var, etc.) serve specific purposes.

  • Commands like ls, cd, pwd, mkdir, cat, and file are used to explore and manipulate the file system.