dirname

Overview

The dirname command strips the last component from file names, returning the directory path portion. It’s the complement to basename for path manipulation.

Syntax

dirname name...

Common Options

Option Description
-z End output with NUL character

Key Use Cases

  1. Extract directory path
  2. Script directory detection
  3. Relative path calculation
  4. File organization
  5. Path validation

Examples with Explanations

Example 1: Basic Usage

dirname /path/to/file.txt

Returns: /path/to

Example 2: Current Directory

dirname file.txt

Returns: .

Example 3: Script Directory

SCRIPT_DIR=$(dirname "$0")

Gets the directory containing the script

Common Usage Patterns

  1. Change to script directory:

    cd "$(dirname "$0")"
  2. Create parent directories:

    mkdir -p "$(dirname "$target_file")"
  3. Relative path operations:

    parent_dir=$(dirname "$PWD")

Best Practices

  1. Quote paths to handle spaces
  2. Use with basename for complete path parsing
  3. Consider absolute vs relative paths
  4. Handle edge cases (root directory, current directory)

Integration Examples

  1. Backup to parent directory:

    backup_dir="$(dirname "$PWD")/backups"
  2. Config file location:

    config_dir="$(dirname "$0")/config"