LinuxCommandLibrary

dirname

Extract directory path from a filename

TLDR

Calculate the parent directory of a given path

$ dirname [path/to/file_or_directory]
copy

Calculate the parent directory of multiple paths
$ dirname [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Delimit output with a NUL character instead of a newline (useful when combining with xargs)
$ dirname [[-z|--zero]] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

SYNOPSIS

dirname [OPTION]... NAME...

PARAMETERS

--zero, -z
    End each output line with NUL, not newline. This is useful for processing with commands like xargs -0.

--help
    Display a help message and exit.

--version
    Display version information and exit.

DESCRIPTION


The dirname command is a standard Unix/Linux utility that outputs the directory portion of a given pathname. It takes one or more pathnames as arguments and, for each, prints the parent directory. Its primary use is to remove the non-directory suffix (the filename or last component) from a file path, leaving only the directory path.

For example, if you provide /home/user/documents/report.txt, dirname will output /home/user/documents. If the input is a directory path like /usr/local/bin/, it will output /usr/local. If the path contains no slashes (e.g., myfile.txt), it will output . (representing the current directory).

dirname is an essential tool in shell scripting for manipulating file paths, allowing scripts to construct new paths relative to an existing file, determine a script's own directory, or extract just the directory part for various processing needs. It operates purely on the string representation of the path, not on whether the path actually exists on the filesystem.

CAVEATS


Trailing Slashes: dirname treats multiple trailing slashes as a single slash. For instance, dirname /usr/local/// will yield /usr/local.
Root Directory: If the input is /, it returns /.
No Slashes: If the input pathname contains no slashes (e.g., filename.txt), it returns . (the current directory).
Empty Input: Providing an empty string "" as input usually results in . according to POSIX specifications.
Non-existent Paths: dirname operates solely on the string structure of the path; it does not check if the path or its components actually exist on the filesystem. This means it will process valid-looking but non-existent paths correctly.

COMMON USE CASES


dirname is frequently used in shell scripts to:
- Navigate to a script's own directory: cd "$(dirname "$0")"
- Construct new paths: "$(dirname "$FILE")"/new_file.txt
- Process files in a directory: find . -name '*.log' -print0 | xargs -0 -I {} mv {} "$(dirname {})"/logs/

POSIX COMPLIANCE

The behavior of dirname is strictly defined by POSIX, which is crucial for writing portable shell scripts that need to run consistently across different Unix and Linux environments. This compliance ensures that its output for various input path structures remains predictable.

HISTORY

dirname is a very old and fundamental command that has been a part of Unix and Linux systems for decades. It is specified by the Single Unix Specification (SUS) and POSIX. This standardization ensures its consistent behavior across various Unix-like operating systems. On Linux systems, it is typically provided as part of the GNU coreutils package. Its robust and predictable behavior makes it a cornerstone for portable shell scripting and path manipulation tasks.

SEE ALSO

Copied to clipboard