dirname Command in Linux



The Linux dirname command strips the last component from a file path. It prints the part before the last slash of the path. If the path does not contain any slash (/) then a dot (.) will be printed. In essence, the dirname command is used to access the directory portion of a given file or directory path.

Table of Contents

Syntax for the dirname Command

The general syntax of using the Linux dirname command is as follows −

dirname [options] [path]

The [options] field is used to specify the syntax options offered by the dirname utility, while the [path] field is used to specify the file path.

Options for the dirname Command

The dirname command options are listed below −

Flags Options Description
-z --zero It ends the output with NULL instead of new line
--help To display the dirname command help
--version To check the command version

Using the dirname Command in Linux

This section will discuss how to use the dirname command in Linux.

Extracting the Path of File or Directory

Extracting the path of a file or directory is one of the dirname commands basic uses. For example, to extract the directory part of the path /etc/openvpn, execute −

dirname /etc/openvpn
Extracting the Path of File or Directory 1

The output displays /etc, because it is the path component before the last slash.

Lets run another command −

dirname /docs/website/index.html
Extracting the Path of File or Directory 2

The output is /docs/website/, because it comes before the last slash.

In the above examples, the absolute paths are used, absolute paths start with /. Lets use dirname with the relative path −

dirname docs/website/index.html
Extracting the Path of File or Directory 3

Even in this case, the dirname command precisely strips the directory path.

Moreover, the dirname treats multiple slashes before the last component of the path as a single slash. For instance, in the following example, there is a triple slash in the path −

dirname docs/website///index.html
Extracting the Path of File or Directory 4

It essentially standardizes the path.

However, if the multiple slashes are not before the paths last component, then it will maintain the path structure. It is to keep the path in its original form.

dirname docs//website/index.html
Extracting the Path of File or Directory 5

If no path is specified, the output will be a dot (.), which represents the current directory −

dirname script.sh
Extracting the Path of File or Directory 6

Printing Output without New Line

In shell scripting, you may need a path without a new line. The -z flag or --zero option displays the path without the new line.

dirname -z /docs/website/index.html
Printing Output without New Line

In the above image, it can be confirmed that the output ends with a NULL instead of the new line.

Extracting Multiple File Paths

The dirname command can take multiple paths. For example, in the following command, two paths are specified −

dirname /docs/website/index.html /etc/openvpn
Extracting Multiple File Paths 1

However, if used in a bash script, multiple file paths can be processed. The for loop takes paths from the PATHS array in the following script.

Then, the dirname command extracts the path and stores it in the dir variable. The echo command displays the extracted paths to the standard output.

#!/bin/bash

PATHS=("/home/user/file/docs" "/Documents/report.docx" "/docs/website/index.html")

for path in "${PATHS[@]}"
do
   dir=$(dirname "$path")
   echo "The path is: $dir"
done
Extracting Multiple File Paths 2

Conclusion

The dirname in Linux extracts the directory portion of the specified path up to the last slash. This utility is a powerful tool that can be used in shell scripting for path manipulation. This tutorial covered the dirname command syntax, its options, and its usage in Linux.

Advertisements