-
-
Notifications
You must be signed in to change notification settings - Fork 1
Linux dirname Guide
Mattscreative edited this page Dec 5, 2025
·
2 revisions
Complete beginner-friendly guide to dirname on Linux, covering Arch Linux, CachyOS, and other distributions including extracting directory paths, path manipulation, and directory operations.
dirname extracts directory from path.
Uses:
- Get directory: Extract directory path
- Remove filename: Strip filename
- Path manipulation: Process paths
- Scripts: Use in automation
Why it matters:
- Path processing: Extract directories
- Scripting: Process file paths
- Directory operations: Work with directories
Basic usage:
# Extract directory
dirname /path/to/file.txt
# Output: /path/toProcess several:
# Multiple paths
dirname /path/to/file1.txt /path/to/file2.txt
# Directory for eachIn scripts:
#!/bin/bash
FILE="/path/to/file.txt"
DIR=$(dirname "$FILE")
echo "Directory: $DIR"With command:
# From find
find . -name "*.txt" | xargs -I {} dirname {}
# Gets directoriesTogether:
#!/bin/bash
FILE="/path/to/file.txt"
DIR=$(dirname "$FILE")
NAME=$(basename "$FILE")
echo "Directory: $DIR"
echo "Filename: $NAME"Check installation:
# Check dirname
which dirname
# Usually in coreutils
# Install if missing
sudo pacman -S coreutilsThis guide covered dirname usage, directory extraction, and path manipulation for Arch Linux, CachyOS, and other distributions.
- basename Guide - Extract filename
- realpath Guide - Resolve paths
-
dirname Documentation:
man dirname
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.