Linux dirname Command
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:
- Remove all trailing slashes (/) from the path
- Remove the last slash and all characters after it
- If the result is empty, output "." (current directory)
Processing Flowchart

Usage Examples
Basic Usage
Example
/home/user/docs
$ dirname relative/path/to/file
relative/path/to
Processing Multiple Files
Example
/a/b
/x/y
Special Path Handling
Example
/usr
$ dirname file.txt # Only filename
.
$ dirname / # Root directory
/
Using in Scripts
Example
# 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
# 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
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
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
Space handling: When the path contains spaces, make sure to use quotes
Example
Relative paths: The output will remain in relative path form
Example
../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
dirname /tmp/testfile
Write a script that displays its own directory
Example
echo "This script is located at:$(dirname "$0")"
Try processing paths with spaces and special characters
Example
touch "/tmp/my dir/special file.txt"
dirname "/tmp/my dir/special file.txt"
Other Extensions
Linux Command Reference