Linux Path Essentials
Understanding Linux File System Paths
In the Linux filesystem, paths are critical navigation mechanisms that define the location of files and directories. Every resource in Linux is represented by a unique path, which serves as its precise address within the system's hierarchical structure.
Path Types in Linux
Linux supports two primary path types:
Path Type |
Description |
Example |
Absolute Path |
Full path from root directory |
/home/user/documents/file.txt |
Relative Path |
Path relative to current directory |
./documents/file.txt |
Basic Path Navigation Commands
## Print current working directory
pwd
## List directory contents
ls /home/user
## Change directory
cd /var/log
## View directory structure
tree /etc
Path Representation Workflow
graph TD
A[Root Directory /] --> B[Home Directory]
A --> C[System Directories]
B --> D[User Specific Paths]
C --> E[Configuration Paths]
Code Example: Path Manipulation
#!/bin/bash
## Get absolute path of a file
absolute_path=$(readlink -f ~/documents/example.txt)
## Extract directory from path
directory=$(dirname "$absolute_path")
## Extract filename from path
filename=$(basename "$absolute_path")
echo "Absolute Path: $absolute_path"
echo "Directory: $directory"
echo "Filename: $filename"
This script demonstrates fundamental path manipulation techniques in Linux, showcasing how to resolve, extract, and work with filesystem paths efficiently.