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