How to Use the dirname Command in Linux

Want to Extract your Directory Path? Use the dirname Command in Linux.

dirname or directory name in full, is a computer program in Linux that extracts the directory path from an absolute file path. A file path, which uniquely identifies the location of a file, consists of a directory path and a file name at the end. dirname was written by David MacKenzie and Jim Meyering.

Sometimes you may have a long file path, and you want to extract the directory path. dirname command makes this possible.

In this article, we will look at examples of how the dirname command is used.

So, let’s begin!

Dirname Command Examples.

Firstly, let’s note that the dirname command syntax is dirname [OPTION] NAME … or simply dirname string where string is the pathname. A pathname identifies a location in a directory structure.

Secondly, the dirname command works by looking for slashes (/) in the command and then displaying what it finds before the last slash (/).

Now, we will go on to examples of how to use dirname command in Linux.

Basic Usage of the dirname Command.

This is achieved by writing the command name dirname, followed by the absolute path. dirname then executes the command and extracts the directory path.

Here are two examples to demonstrate this. The first file name does not have an extension, while the second one has .sh. This is to say that dirname executes them the same way.

$ dirname home/bin/usr
$ dirname home/bin/usr/test.sh 

Below is the output of the above commands.

dirname command in linux



What if the File Path has no Slash? 

This means that the file name given is its absolute path as well. As a result, the output will be a dot (.). In other words, the output of the dirname command is in the current directory. 

For example, here is the syntax without a slash(/):

$ dirname test.sh

Below is the output.

dirname command in linux



How to Get Multiple Outputs Using the dirname Command.

dirname can be used to output more than one directory path. This is made possible by writing the different absolute file paths one after the other. The file paths should be separated by a space.

Here is the syntax that we will run in the Command Terminal.

$ dirname home/bin/usr home/bin/usr/thefile.txt home/bin

Below is the output, which shows three directory paths

dirname command in linux

Using dirname Command to Separate Output with NULL.

By default, multiple outputs of dirname command are usually separated by a new line. However, if we want the values to be separated by a NULL character, we use the command line option -z  or --zero.

Here is the command syntax:

$ dirname -z home/usr var/test.sh home/exe/bin

See the output below and it’s all on the same line.

dirname command in linux



Dirname Command Options.

Some of the dirname command options include:

--version which shows the version information and then it exits.

See the command below:

$ dirname --version

Here is the output shown in the screenshot below.

options

--help displays the information that offers assistance. Similarly, one can refer to this information as needed.

The syntax to type in the Command Terminal is as shown below:

$ dirname --help

Consequently, the output is as seen in the screenshot.

output



Using bash dirname Command.

In the example below, we want to get the path of the directory that has the file. We will use the file path variable mainpath.

Have a look at the code below:

mainpath = "/home/data/bin/filename"
dir_path = $(dirname "$mainpath")
echo $dir_path

In conclusion, while a dirname Command in Linux extracts the directory path without the file name, the basename command does the exact opposite. That is to say, it gets the file name instead. 

We hope you have learned the different ways to use the dirname command in Linux. If you have any questions or suggestions, let us know in the comment section below.

If this guide helped you, please share it. 🙂

Author

Leave a Reply

Your email address will not be published. Required fields are marked *