Open In App

Dirname Command in Linux with Examples

Last Updated : 09 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

dirname is a command in Linux that is used to remove the trailing forward slashes “/” from the NAME and prints the remaining portion. If the argument NAME does not contain the forward slash “/” then it simply prints dot “.”. In other words, we can say that the `dirname` command is a useful tool for extracting the directory portion from a given path or filename. 

Syntax of the `dirname` command in Linux

dirname [OPTION] NAME

Basic Example of `dirname` command:

We know that it is used to extract the directory name from a given file path.

Syntax:

dirname /Desktop/root/bash.sh
dirname /Desktop/root/bash.sh

dirname /Desktop/root/bash.sh

Here we can see that our whole path is “/Desktop/root/bash.sh”, where we have an output “/Desktop/root” which is our directory name, and “bash.sh” was our file name.

dirname bash.sh
dirname bash.sh

dirname bash.sh

Here we can see that no path is mentioned on file name “bash.sh” is mentioned. Our output shows “.” which means that we are in the current directory.

Options available in `dirname` command in Linux:

`-z or -zero` Option in `dirname` command in Linux

This option provides a way to end the line by a null command rather than a new line. Using this the next command will execute on the same line rather than on the next line. 

Syntax:

dirname -zero NAME or dirname --z NAME

Example:

dirname -z /Desktop/root/bash.sh
dirname -z /Desktop/root/bash.sh

dirname -z /Desktop/root/bash.sh

As we can see we end the line with a null command rather than a new line.

`–help` Option in `dirname` command in Linux

 It displays helpful information. 

dirname --help
dirname --help

dirname –help

`–version` Option in `dirname` command in Linux

 It displays version information. 

dirname --version
dirname --version

dirname –version

Conclusion

In this article we discussed the `dirname` command in Linux. Which provides a simple and effective way to extract the directory portion from a given file path or name. By going through this article one can understand that `dirname` command helps in manipulating and working with the file path efficiently. Overall, we can say that `dirname` command is a valuable tool in our Linux System for file management and scripting tasks.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads