Linux dirname command for beginners (4 examples)

Are you a new Linux user? Does your work provide for a shell script? If your answer to both of these questions is yes, then the tool we’ll be discussing here is likely to interest you. Tool name dirnameand this tool is mainly used in situations where you need to strip the last component from an absolute filename.

In this article, we will discuss the basis of dirname, as well as how you can use it. But before we do that, you need to know that all examples / instructions mentioned here have been tested on Ubuntu 16.04 LTS.

Dirname command in Linux

Following is the general syntax for the dirname command:

dirname [OPTION] NAME ...

This is how the man page describes this command:

Вывод ИМЕНИ с его последним компонентом без косой черты и задними косыми чертами; если имя не содержит 
/'s, выводиться '.' (то есть текущий каталог).

The following examples will give you a better idea of ​​how you can use this tool.

Q1. How long does the dirname command work?

The main use of this tool is just write the command name followed by the absolute filename. In the output, you get the complete directory tree, excluding the file name.

Here’s an example:

[email protected]:~# dirname /etc/cron.d/awstats                                                                                                                  
/etc/cron.d

Q2. Can it work with multiple directories?

Yes of course. All you have to do is supply the absolute filename, one by one. The following example shows this:

[email protected]:~# dirname /etc/cron.d/awstats /etc/nginx/nginx.conf /etc/nginx/vhosts/andreyex/andreyex.ru.conf
/etc/cron.d
/etc/nginx
/etc/nginx/vhosts/andreyex

Q3. What if the filename is an absolute name?

If just a filename is specified (and not an absolute one, complete with a directory tree), then a dot (.) Is shown on the output:

[email protected]:/etc/nginx/vhosts/andreyex# dirname andreyex.ru.conf                                                                                            
.

If you look at the man page description for the command (mentioned at the beginning), this behavior is clearly stated there.

Q4. How can I replace newline character with NUL in the output?

By default, lines in the output are separated by a newline, as this is clear from question 2 above. However, if you want, you can have NUL-separated output. To do this, use the command line parameter -z

Using this command line as an example, the option is in action:

[email protected]:/etc/nginx/vhosts/andreyex# dirname -z /etc/cron.d/awstats /etc/nginx/nginx.conf /etc/nginx/vhosts/andreyex/andreyex.ru.conf                    
/etc/cron.d/etc/nginx/etc/nginx/vhosts/[email protected]:/etc/nginx/vhosts/andreyex#

Note that the output of the results is no longer separated from each other by a new line.

Conclusion

The dirname command may not be as fancy as some of the other commands in Linux, but it would be correct to say that it is a great tool that can come in handy when working with shell scripts. Everything we’ve discussed here is pretty much everything suggested by the tool. In case you have any doubts or queries, please leave a comment below.

Sidebar