Understanding the file system hierarchy in a Linux system is crucial for navigating and managing files effectively. The Linux file system follows the Filesystem Hierarchy Standard (FHS), which defines the directory structure and its contents in Unix-like operating systems.
pwd
stands for “print working directory.” Its function is simple: it prints out the shell’s current working directory. But what exactly is a working directory?
An important concept to understand is that the shell has a default location where any file operations will occur—this is known as the working directory. When you create new files or directories, view existing ones, or delete them, the shell assumes you are referring to the current working directory unless you specify otherwise. Therefore, it’s crucial to be aware of the directory the shell is “in” at any given time. Deleting files from the wrong directory could be disastrous. If you’re ever unsure, the pwd
command will tell you exactly what the current working directory is.
You can change the working directory using the cd
command, which stands for “change directory.” Try typing the following:
cd / pwd ls
Now your working directory is “/”. If you’re coming from a Windows background you’re probably used to each drive having its own letter, with your main hard drive typically being “C:”. Unix-like systems don’t split up the drives like that. Instead they have a single unified file system, and individual drives can be attached (“mounted”) to whatever location in the file system makes most sense. The “/” directory, often referred to as the root directory, is the base of that unified file system.
From there everything else branches out to form a tree of directories and subdirectories.
Here’s a guide to understanding the location sense in a Linux system:
The Root Directory (/
)
The root directory, denoted as /
, is the top-level directory of the Linux file system. All other directories and files reside within this directory. It’s the starting point from which the entire directory structure branches out.
Common Directories Under the Root
/bin
- Contains essential binary executables (commands) needed for the system to boot and run in single-user mode.
- Examples:
ls
,cp
,mv
,bash
.
/sbin
- Contains essential system binaries, typically used by the root user for system administration tasks.
- Examples:
ifconfig
,fdisk
,reboot
.
/boot
- Contains the bootloader files, kernel images, and related files needed to boot the system.
- Examples:
vmlinuz
,initrd
.
/dev
- Contains device files, representing hardware devices and special files.
- Examples:
sda
(first hard drive),tty
(terminals),null
.
/etc
- Contains system-wide configuration files and shell scripts that are used to boot and configure the system.
- Examples:
passwd
,fstab
,hosts
.
/home
- Contains home directories for all users except the root user. Each user has a subdirectory under
/home
. - Example:
/home/username
.
- Contains home directories for all users except the root user. Each user has a subdirectory under
/lib
- Contains shared libraries needed by essential binaries in
/bin
and/sbin
. - Examples:
libc.so.6
,libdl.so.2
.
- Contains shared libraries needed by essential binaries in
/media
- Mount points for removable media like USB drives, CD-ROMs, etc.
- Example:
/media/usb
.
/mnt
- Temporary mount points for file systems.
- Example:
/mnt/mydrive
.
/opt
- Contains optional software packages and add-on applications that are not part of the default installation.
- Example:
/opt/google/chrome
.
/proc
- A virtual filesystem that provides information about running processes and the kernel.
- Examples:
/proc/cpuinfo
,/proc/meminfo
.
/root
- Home directory for the root user.
- Example:
/root
.
/run
- A temporary filesystem that stores data related to running processes. This directory is cleared at boot.
- Example:
/run/lock
.
/srv
- Contains data for services provided by the system, such as web and FTP servers.
- Example:
/srv/www
.
/sys
- A virtual filesystem that provides information about the system and kernel hardware.
- Example:
/sys/class
.
/tmp
- Temporary files created by system and user processes. This directory is usually cleared at boot.
- Example:
/tmp/session-xyz
.
/usr
- Contains user utilities and applications. It’s often the largest directory.
- Subdirectories include:
/usr/bin
: Non-essential user binaries./usr/sbin
: Non-essential system binaries./usr/lib
: Libraries for binaries in/usr/bin
and/usr/sbin
./usr/local
: Locally installed software./usr/share
: Shared data used by applications./usr/include
: Header files for development.
/var
- Contains variable data files, including logs, spool files, and transient files.
- Subdirectories include:
/var/log
: Log files./var/spool
: Spool directories for tasks like mail and printing./var/tmp
: Temporary files that should persist between reboots.
Navigating the File System
Moving Around
cd /path/to/directory
Change the current directory
cd /home/user cd /usr/local/bin cd .. # Go to parents directory cd ~ # Go to home directory cd / # Go to root directory
You can find the difference between absolute path and relative path here
Viewing Contents
ls /path/to/directory
List the contents of a directory
ls /etc ls -l /var/log ls -a /home/user
Viewing Current Directory
pwd
Permissions and Ownership
Each file and directory in Linux has permissions and ownership settings:
Permissions Determine who can read, write, and execute the file.
ls -l /path/to/file
Output example:
-rwxr-xr-x 1 user group size date time filename
rwx
: Owner permissions (read, write, execute).r-x
: Group permissions (read, execute).r-x
: Others permissions (read, execute).
Ownership: Specifies the user and group that own the file. You can use chown command to change user ownership.
chown user:group /path/to/file chown user /path/to/file