Skip to content

Lecture 2: Linux Directories

Directory

What is a Directory?

A directory is a container for system and data files — essentially a folder used to organize files within the filesystem.

Linux vs. Windows Directory Structure

  • Windows: Each drive (e.g., C:\, D:\) has its own root.
  • Linux: A single root (/) exists for the entire system; all files and directories branch from it.

Linux Directory Structure

DirectoryPurposeDirectoryPurpose
/binUser Binaries./sbinSystem Binaries.
/etcConfiguration Files./devDevice Files.
/procProcess Information./varVariable Files.
/tmpTemporary Files./usrUser Programs.
/homeHome Directories./bootBoot Loader Files.
/libSystem Libraries./optOptional Applications.
/mntMount Directory./mediaRemovable Devices.
/srvService Data./runVolatile runtime data.

Difference Between / and /root

DirectoryDescription
/The root directory, top of the Linux file hierarchy. Every directory is a subdirectory of /.
/rootThe home directory of the system administrator (root user).

The /boot Directory

  • Contains essential boot files, including the Linux kernel and bootloader (e.g. GRUB).
  • Required for system startup; losing this directory prevents the OS from booting.
  • Static and unshareable directory used before user-mode processes start.

Linux Directory Commands

CommandDescription
pwdPrints the current working directory path.
lsLists directory contents.
cdChanges the current directory.
mkdirCreates a new directory.
rmdirRemoves an empty directory.

pwd Command

Description

  • pwd stands for Print Working Directory.
  • Displays the absolute path of the current directory.
  • Exists as both a shell built-in and a binary (/bin/pwd).

Syntax

bash
pwd    # Print the full filename of the current working directory.
pwd -L # Use the $PWD environment variable, even if it contains symlinks
pwd -P # Resolve all symlinks

$PWD Environment Variable

  • Stores the path of the current working directory.
  • echo $PWD gives the same output of pwd -L.

Options

  • --help: Displays help for the command.
  • --version: Displays version information.

cd Command

Description

  • cd stands for Change Directory.
  • Used to move between directories.
  • Available as a shell built-in.

Syntax

bash
cd [directory]

Examples

bash
cd /         # Move to the root directory
cd           # Move to home directory
cd ~         # Move to home directory
cd Documents # Move to a subdirectory
cd ..        # Move to parent directory

Absolute vs Relative Paths

  • Absolute Path: Starts from / (root). Example: /home/user/Documents
  • Relative Path: Starts from the current directory. Example: ../Downloads

Path Completion

  • Press Tab to auto-complete filenames and directories.

ls Command

Description

Lists the files and directories within a directory (by default the current working directory).

Syntax

bash
ls [options] [directory]

Common Options

OptionDescription
-a, --allInclude hidden files (. prefix).
-A, --almost-allShow hidden files but omit . and ...
-d, --directoryList directories themselves, not their contents.
-gShow group ownership instead of file owner.
-h, --human-readableShow human-readable sizes (e.g., KB, MB).
-i, --inodeShow inode numbers.
-lLong listing format with detailed info.
-r, --reverseReverse the sorting order.
-R, --recursiveRecursive listing including subdirectories.
-SSort by file size, largest first
-tSort by modification time, newest first.

ls -l Format Fields

  1. Permissions: e.g., -rwxr-xr-- (user/group/others).
  2. Links: Number of hard links.
  3. Owner: File owner.
  4. Group: Group ownership.
  5. Size: File size in bytes.
  6. Date/Time: Last modification.
  7. Name: File or directory name.

Custom Block Sizes:

UnitUnit
K = KilobyteM = Megabyte
G = GigabyteT = Terabyte
P = PetabyteE = Exabyte
Z = ZettabyteY = Yottabyte
bash
ls -l --block-size=M  # Display sizes in megabytes

Hidden Files and Directories

  • Hidden files start with a dot (.), e.g., .bashrc.
  • ls -a: Lists all files including . (current directory) and .. (parent directory).
  • ls -A: Lists hidden files but omits . and ...

Reasons for Hidden Files:

  • Keeps directories clean.
  • Hides backups or configuration files.
  • Prevents accidental deletion.

Other Examples

  • ls ~ -> Lists home directory contents.

  • ls ../ -> Lists parent directory contents.

  • ls -g -> Excludes owner column from output.

  • ls -R -> Recursively lists subdirectories.

mkdir Command

Description

Creates directories in the filesystem.

Syntax

bash
mkdir [options] directory_name

Options

OptionDescription
-p / --parentsCreate parent directories if needed.
-v / --verbosePrint a message for each directory created.
-m MODESet permissions (same format as chmod).
--helpDisplay help information.
--versionShow version info.

Examples

bash
mkdir new_folder
mkdir -p dir1/dir2/dir3
mkdir -m 755 secure_dir # Create a new directory with rwx-rw-rw- permissions

rmdir Command

Description

Removes empty directories only.

Syntax

bash
rmdir [options] directory_name

Example

bash
rmdir temp_folder

Recursive Deletion

bash
rmdir -p parent/child

Deletes a directory and its empty parent directories.

Renaming Directories

Linux uses the mv command to move or rename directories (and files).

There is no dedicated rename command.

Syntax

bash
mv [options] old_name new_name

Options

OptionDescription
--backupCreates a backup before overwriting.
-fForces overwrite without confirmation.
-iAsks before overwriting.
-vVerbose output showing operations.

rename Command

Description

Used for batch renaming of multiple files or directories.
May need to be installed depending on the distribution.

Example

Installation

bash
# Arch Linux (btw)
sudo pacman -S rename

# Debian / Ubuntu
sudo apt install rename

# Fedora
sudo yum install prename