Skip to content

Linux dirname Guide

Mattscreative edited this page Dec 5, 2025 · 2 revisions

Linux dirname Guide

Complete beginner-friendly guide to dirname on Linux, covering Arch Linux, CachyOS, and other distributions including extracting directory paths, path manipulation, and directory operations.


Table of Contents

  1. Understanding dirname
  2. dirname Basics
  3. Extracting Directories
  4. Path Manipulation
  5. Troubleshooting

Understanding dirname

What is dirname?

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

dirname Basics

Extract Directory

Basic usage:

# Extract directory
dirname /path/to/file.txt

# Output: /path/to

Multiple Paths

Process several:

# Multiple paths
dirname /path/to/file1.txt /path/to/file2.txt

# Directory for each

Extracting Directories

From Variable

In scripts:

#!/bin/bash
FILE="/path/to/file.txt"
DIR=$(dirname "$FILE")
echo "Directory: $DIR"

From Command

With command:

# From find
find . -name "*.txt" | xargs -I {} dirname {}

# Gets directories

Path Manipulation

Combine with basename

Together:

#!/bin/bash
FILE="/path/to/file.txt"
DIR=$(dirname "$FILE")
NAME=$(basename "$FILE")
echo "Directory: $DIR"
echo "Filename: $NAME"

Troubleshooting

dirname Not Found

Check installation:

# Check dirname
which dirname

# Usually in coreutils
# Install if missing
sudo pacman -S coreutils

Summary

This guide covered dirname usage, directory extraction, and path manipulation for Arch Linux, CachyOS, and other distributions.


Next Steps


This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.

Clone this wiki locally