Overview
The dirname command removes the filename from the end of a specified file path (string) and displays only the directory portion (parent path). It is the counterpart to the basename command. In shell scripts, it is an essential tool for tasks such as moving to the directory where a target file is located or identifying the storage location of the script itself.
Specifications (Arguments and Options)
Syntax
dirname string
dirname [options]... string...
Main Arguments and Options
The dirname command has very few complex options.
| Option | Description |
-z | Uses a null character (\0) as the output delimiter instead of a newline (zero). |
--help | Displays help information. |
Basic Usage
When you provide a file path as an argument, the command returns the path of the directory containing that file.
Command
# Extract the directory portion from a path
dirname /var/www/html/index.php
Execution Result
/var/www/html
Practical Commands
Display the Directory Where a Command is Located
By combining dirname with the which command, you can obtain only the directory path where a specific command is installed.
# Display the directory containing the ls command
dirname $(which ls)
Output:
/usr/bin
Move to the Location of a Target File in a Shell Script
This method extracts the directory portion from a path stored in a variable and uses cd to move there. This is a common technique for processing log files.
#!/bin/bash
LOG_FILE="/var/log/apache2/access.log"
# Move to the directory where the log file is located
cd "$(dirname "$LOG_FILE")"
# Verify the current location
pwd
Output:
/var/log/apache2
Batch Process Multiple Paths
If you specify multiple paths as arguments, the command returns the directory name for each one.
dirname /usr/bin/python3 /etc/nginx/nginx.conf
Output:
/usr/bin
/etc/nginx
Customization Points
- Handling Relative Paths: If the argument does not contain a path (e.g.,
script.sh), the command returns., representing the current directory. - Root Directory: If the argument is
/, the result will also be/. - Combination with basename: Use
basenamewhen you need only the filename anddirnamewhen you need the location.
Important Notes
- String Manipulation Only: Like
basename,dirnamedoes not check if the specified path actually exists. It simply cuts the string before the last forward slash. - Trailing Slashes: If a path ends with a slash (e.g.,
/var/www/), that slash is considered part of the “filename” (directory name), and the level above it (/var) is returned. - Required Command Substitution: Writing
cd dirname $VARin a script will not work. You must wrap it in command substitution, such ascd "$(dirname "$VAR")".
Application
Get the Script’s Own Directory and Move There (Essential Technique)
This is the most important idiom for ensuring that a shell script can perform its tasks relative to its own location, regardless of where it is executed from.
#!/bin/bash
# Get the absolute path of the directory where this script is located
SCRIPT_DIR=$(cd $(dirname "$0") && pwd)
echo "Script location: $SCRIPT_DIR"
# This allows you to safely load external configuration files
# source "$SCRIPT_DIR/config.env"
Summary
The dirname command proves its true value in path manipulation within shell scripts, particularly when combined with cd. While it is rarely used in manual operations, it is a mandatory tool for automation scripts to “locate and move to a file’s directory.” Along with basename and readlink, it is one of the three essential commands for path handling.
