-
-
Notifications
You must be signed in to change notification settings - Fork 1
Linux basename dirname Guide
Mattscreative edited this page Dec 5, 2025
·
2 revisions
Complete beginner-friendly guide to basename and dirname on Linux, covering Arch Linux, CachyOS, and other distributions including path manipulation, filename extraction, and directory path extraction.
Basic usage:
# Extract filename
basename /path/to/file.txt
# Output: file.txtRemove extension:
# Remove suffix
basename /path/to/file.txt .txt
# Output: fileBasic usage:
# Extract directory
dirname /path/to/file.txt
# Output: /path/toGet parent:
# Parent directory
dirname /path/to/dir/
# Output: /path/toBoth commands:
# Get directory and filename
FILE="/path/to/file.txt"
DIR=$(dirname "$FILE")
NAME=$(basename "$FILE")
echo "Directory: $DIR"
echo "Filename: $NAME"Strip extension:
# Remove extension
basename file.txt .txt
# Output: fileIn scripts:
#!/bin/bash
FILE="/path/to/file.txt"
# Get components
DIR=$(dirname "$FILE")
NAME=$(basename "$FILE")
BASE=$(basename "$FILE" .txt)
echo "Full path: $FILE"
echo "Directory: $DIR"
echo "Filename: $NAME"
echo "Base name: $BASE"Check installation:
# basename and dirname are part of coreutils
# Usually pre-installed
# Check commands
which basename
which dirnameThis guide covered basename and dirname usage, path manipulation, and filename extraction for Arch Linux, CachyOS, and other distributions.
- readlink Guide - Symbolic link resolution
- realpath Guide - Absolute path resolution
- Bash Scripting Guide - Scripting basics
-
basename Documentation:
man basename -
dirname Documentation:
man dirname
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.