Linux Comm Dirname
[ Linux Command Encyclopaedia](#)
* * *
## What is the dirname Command
`dirname` 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 from a path without manually parsing strings.
### Basic Functionality
* **Input**: A file path string
* **Output**: The directory part of the path (i.e., content after removing the last slash)
* * *
## Command Syntax
dirname filename...
### Parameter Description
* **Filename**: Can be one or more file paths (supports absolute and relative paths)
* **...**: Indicates that multiple file paths can be processed simultaneously
### Option Parameters
While `dirname` doesn't require options in most cases, the GNU version supports the following standard options:
| Option | Description |
| --- | --- |
| `-z` | Use NUL character () to separate output, instead of newline |
| `--help` | Display help information |
| `--version` | Display version information |
* * *
## How It Works
The `dirname` 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 directory where the script is located
SCRIPT_DIR=$(dirname "$0")
echo "Script directory: $SCRIPT_DIR"
# Get the directory of config file path
CONFIG_PATH="/etc/app/config.cfg"
CONFIG_DIR=$(dirname "$CONFIG_PATH")
echo "Config directory: $CONFIG_DIR"
* * *
## Common Application Scenarios
### 1. Get Script's Directory
## Example
#!/bin/bash
# Get the absolute path where the script is located
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
echo "Script location: $SCRIPT_DIR"
### 2. Build Relative Paths
## Example
# Assuming 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. Use 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**: `dirname` does not resolve symbolic links. To resolve them, use with `readlink`
## Example
dirname "$(readlink -f "/path/with/symlink")"
**Space Handling**: When the path contains spaces, ensure quotes are used
## Example
dirname "/path/with spaces/file.txt"
**Relative Paths**: The output will maintain the relative path form
## Example
$ dirname ../parent/file.txt
../parent
**Empty Input**: If no argument is 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 symlinks) | ../file | /full/path/to/file |
* * *
## Practice Exercises
Create a test file and get its directory path
## Example
touch /tmp/testfile
dirname /tmp/testfile
Write a script that displays the directory where it itself is located
## Example
#!/bin/bash
echo "This script is located at: $(dirname "$0")"
Try handling 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 Command Encyclopaedia](#)
YouTip