dirnamex
Strip the last component from file names.
Mirrors GNU dirname. Run it as dirnamex, or as gnux dirnamex.
Synopsis
dirnamex [OPTION] NAME...GNU reference
Description
dirnamex prints each NAME with its last non-slash component and trailing slashes removed. If NAME contains no slash the result is '.', the current directory, and anything directly under the root yields the root itself. The rest of the path is left exactly as written: no symbolic links are resolved and no '.' or '..' components are collapsed. -z separates the output with NUL instead of newlines, for names that may contain newlines.
Permissions
dirnamex works on the text of its operands only: it never touches the filesystem, so the names need not exist and no permissions are required.
Use cases
- Finding the directory a script lives in from $0.
- Computing the parent directory to create before writing a file.
- Splitting a list of paths into their directories in a pipeline.
- Walking up a path several levels by applying
dirnamexrepeatedly. - Getting the name of a file's containing folder together with
basenamex. - Finding the real directory of a file behind symbolic links together with
realpathx. - Producing NUL-separated output with
-zfor names that may contain newlines.
Flags
--help display this help and exit
--version output version information and exit
-z, --zero end each output line with NUL, not newlineExamples
Print the directory part of a path
dirnamex /usr/bin/sortHandle several names at once
dirnamex dir1/str dir2/str stdio.hIgnore trailing slashes
dirnamex /usr/bin/Get '.' for a bare name and the root for a top-level entry
dirnamex stdio.h /etcLeave '.' and '..' exactly as written (no normalization)
dirnamex a/../b/./cSeparate the output with NUL bytes instead of newlines
dirnamex -z a/b c/d | od -cCreate the directory a file will be written to
mkdirx -p "$(dirnamex out/reports/q1.csv)"Create a log file's directory, then the file
d=$(dirnamex logs/2024/app.log) && mkdirx -p "$d" && touchx logs/2024/app.logChange to the directory a script lives in
cd "$(dirnamex "$0")" && pwdxGo up two levels
dirnamex "$(dirnamex /srv/app/releases/v1/bin)"Print the name of the folder that contains a file
basenamex "$(dirnamex /var/lib/app/data.db)"Find the real directory of a file behind symbolic links
dirnamex "$(realpathx scripts/run.sh)"Split several paths into their directories with the gnux shell
gnux shell -c 'dirnamex src/main.go docs/guide.md README.md'Options
Each option has a page of its own, with its usage and examples.
- -z, --zero: End each output line with NUL, not newline.
Exit status
| Status | Meaning |
|---|---|
| 0 | success: every operand was processed |
| 1 | failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake |
Help in the terminal
dirnamex --help # options, first examples, exit status
gnux help dirnamex # this page: the full manual
gnux help dirnamex --OPTION # one option in depth, with its examples