Navigating the File System
1. Understanding Absolute vs Relative Paths
| Path Type | Definition | Examples | Usage |
|---|---|---|---|
| Absolute Path | Full path from the root directory to a file or directory | /home/user/documents/file.txt |
Used when you need to reference a file or directory regardless of current location |
| Relative Path | Path relative to the current working directory | documents/file.txt |
Used when referencing files or directories from the current location |
Note: Absolute paths begin with
/, while relative paths do not.2. Navigating Directories
| Command | Description | Example |
|---|---|---|
| cd | Change directory | cd /home/user/documents |
| cd .. | Move up one directory level | cd .. |
| cd ~ | Go to the home directory | cd ~ |
| cd - | Switch back to the previous directory | cd - |
Note: The
pwd command prints the current working directory.
3. Displaying Current Directory
| Command | Description | Example |
|---|---|---|
| pwd | Prints the full path of the current directory | pwd |
4. Listing Directory Contents
| Command | Description | Options | Example |
|---|---|---|---|
| ls | List directory contents | -l, -a, -h |
ls -la |
| ls -l | Long format listing | -l |
ls -l /home/user |
| ls -a | List all files including hidden ones | -a |
ls -a |
| ls -h | Human-readable file sizes | -h |
ls -lh |
| ls -R | Recursively list subdirectories | -R |
ls -R /etc |
Note: The
ls command is one of the most frequently used commands in Linux to view directory contents. It supports various options to customize output format and display.5. Using Directory Stack
| Command | Description | Example |
|---|---|---|
| pushd | Changes to a directory and adds it to the directory stack | pushd /home/user/documents |
| popd | Removes the last directory from the stack and switches to it | popd |
| dirs | Displays the directory stack | dirs |
| pushd -n | Pushes directory without changing to it | pushd -n /tmp |
| popd +N | Removes and switches to the Nth directory in stack (1-based) | popd +2 |
| dirs -l | Lists directories with full paths | dirs -l |
Example: Managing Directory Stack
pushd /home/user
pushd /var/log
dirs
# Output: /var/log /home/user
popd
# Now in /home/user
popd
# Now in original directory
Note: The directory stack is managed as a LIFO (Last In, First Out) queue.
6. Viewing File Type
| Command | Description | Example |
|---|---|---|
| file | Identifies the type of a file based on its content | file document.txt |
| file -b | Displays file type without the filename prefix | file -b document.txt |
| file -I | Displays file type with MIME information | file -I image.png |
Example: Using file command
$ file document.txt
document.txt: ASCII text
$ file -I image.png
image.png: image/png; charset=binary
7. Following Symbolic Links
| Command | Description | Example |
|---|---|---|
| ls -l | List files with details including symbolic links | ls -l /path/to/symlink |
| readlink | Display the value of a symbolic link | readlink /path/to/symlink |
| realpath | Print the canonicalized absolute path, resolving symbolic links | realpath /path/to/symlink |
Example: Using readlink to resolve a symlink
$ readlink /etc/alternatives/java
/usr/lib/jvm/java-11-openjdk/bin/java
Example: Using realpath to get the absolute path of a symlink
$ realpath /etc/alternatives/java
/usr/lib/jvm/java-11-openjdk/bin/java
Note: The
readlink command does not follow nested symbolic links by default. Use readlink -f to resolve all levels of symbolic links.
Warning: Be cautious when following symbolic links in scripts or automated tools, as they can lead to unexpected behavior if the target is moved or deleted.
8. Understanding Hidden Files
| File Pattern | Description | Usage |
|---|---|---|
| .filename | Hidden files starting with a dot | Configuration and system files |
| .. | Parent directory reference | Navigate up one level in the directory tree |
| . | Current directory reference | Reference to the current working directory |
Note: The
-a flag includes all files, including hidden ones. The -l flag provides long format output.9. Using Tab Completion
| Feature | Description | Usage |
|---|---|---|
| Basic tab completion | Autocomplete filenames, directories, and commands | Press Tab after typing part of a path or command |
| Double tab | Show all possible completions | Press Tab twice to list all matches |
| File path completion | Complete directory and file names | Type /usr/bi then Tab to complete to /usr/bin/ |
| Command completion | Complete command names | Type ls /ho then Tab to complete to /home/ |
Note: Tab completion works in most shells including
bash, zsh, and fish.Warning: In some shells, tab completion may not work in paths with spaces or special characters. Use quotes to handle such cases.
10. Using Wildcards
Wildcards are special characters used to match filenames based on patterns. They are commonly used in shell commands and scripts to perform operations on multiple files.
| Wildcard | Description | Example | Matches |
|---|---|---|---|
| * | Matches any sequence of characters (including none) | ls *.txt |
All files ending in .txt |
| ? | Matches any single character | ls file?.log |
Files like file1.log, fileA.log |
| [abc] | Matches any one of the characters inside brackets | ls [abc].txt |
Files named a.txt, b.txt, or c.txt |
| [a-z] | Matches any character in the range | ls [a-z].log |
All files ending in .log with lowercase letters |
| [!abc] | Matches any character NOT in the brackets | ls [!abc].txt |
All files ending in .txt except those starting with a, b, or c |