Linux dirname Command

Linux 命令大全Linux Command Reference


What is the dirname command?

dirnameIt is a simple but practical command-line tool in Linux/Unix systems, used to extract the directory part from a file path. It helps you quickly get the parent directory of a path without manually parsing strings.

Basic Functionality

  • Input: a file path string
  • Output: the directory portion of the path (i.e., what remains after removing the last slash and everything after it)

Command Syntax

dirname [选项] 文件名...

Parameter Description

  • filename: can be one or more file paths (supports absolute and relative paths)
  • **...** : indicates that multiple file paths can be processed at the same time

Options

Although in most casesdirnameno options are needed, the GNU version supports the following standard options:

Option Description
-z Use NUL character (\0) to separate output instead of newlines
--help Show help information
--version Show version information

How It Works

dirnameThe command processes paths through the following steps:

  1. Remove all trailing slashes (/) from the path
  2. Remove the last slash and all characters after it
  3. If the result is empty, output "." (current directory)

Processing Flowchart


Usage Examples

Basic Usage

Example

$ dirname /home/user/docs/file.txt
/home/user/docs

$ dirname relative/path/to/file
relative/path/to

Processing Multiple Files

Example

$ dirname /a/b/c.txt /x/y/z.txt
/a/b
/x/y

Special Path Handling

Example

$ dirname /usr/bin/      # Note the trailing slash
/usr

$ dirname file.txt       # Only filename
.

$ dirname /              # Root directory
/

Using in Scripts

Example

#!/bin/bash

# Get the script's directory
SCRIPT_DIR=$(dirname "$0")
echo "Script directory:$SCRIPT_DIR"

# Get the directory of the config file path
CONFIG_PATH="/etc/app/config.cfg"
CONFIG_DIR=$(dirname "$CONFIG_PATH")
echo "Config directory:$CONFIG_DIR"

Common Use Cases

1. Get the script's directory

Example

#!/bin/bash
# Get the absolute path of the script's directory
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
echo "Script directory:$SCRIPT_DIR"

2. Constructing relative paths

Example

# Assume we know the file is in a subdirectory of a certain directory
BASE_DIR="/var/log"
FULL_PATH="$BASE_DIR/app/error.log"

# Get the log directory
LOG_DIR=$(dirname "$FULL_PATH")

3. Using with basename

Example

# Decompose the full path
FULL_PATH="/home/user/docs/report.pdf"

DIR=$(dirname "$FULL_PATH")
FILE=$(basename "$FULL_PATH")

echo "Directory:$DIR"
echo "Filename:$FILE"

Notes

Symbolic links:dirnameIt does not resolve symbolic links; if you need to resolve them, you can combine withreadlinkuse

Example

dirname "$(readlink -f "/path/with/symlink")"

Space handling: When the path contains spaces, make sure to use quotes

Example

dirname "/path/with spaces/file.txt"

Relative paths: The output will remain in relative path form

Example

$ dirname ../parent/file.txt
../parent

Empty input: If no arguments are provided, the GNU version outputs ".", but this is not POSIX standard behavior


Comparison with Similar Commands

Command Function Example Input Example Output
dirname Extract directory part /a/b/c.txt /a/b
basename Extract filename part /a/b/c.txt c.txt
realpath Get absolute path (resolve symbolic links) ../file /full/path/to/file

Practical Exercises

Create a test file and get its directory path

Example

touch /tmp/testfile
dirname /tmp/testfile

Write a script that displays its own directory

Example

#!/bin/bash
echo "This script is located at:$(dirname "$0")"

Try processing paths with spaces and special characters

Example

mkdir -p "/tmp/my dir"
touch "/tmp/my dir/special file.txt"
dirname "/tmp/my dir/special file.txt"

Linux 命令大全Linux Command Reference

Other Extensions