Skip to content

dirname Linux Command Guide

The Linux dirname command is a helpful tool that allows users to extract the directory part of a given pathname. It is particularly useful for shell scripting and automation tasks. By using the dirname command, users can quickly obtain the path without the filename, making it easier to manage and manipulate file paths programmatically. The command is simple to use and provides options for additional functionality, making it a valuable asset for Linux users and system administrators.

dirname Syntax:

Terminal window
dirname [option] [parameter]

Options:

OptionDescription
-zEnd each output line with NUL

Parameters:

ParameterDescription
pathThe path for which to retrieve the directory name

dirname Command Samples:

Extract Directory Path from a File Path

Terminal window
dirname /path/to/file.txt

This command will extract and display the directory path from the given file path.

Get Parent Directory Path

Terminal window
dirname /path/to/directory/

Using this command with a directory path will return the parent directory’s path.

Handle Relative Paths

Terminal window
dirname ../relative/path/file.txt

It can also handle relative paths, giving the directory path of the specified file.

Extract Directory Path from a Full Path

Terminal window
dirname /home/user/Documents/file.doc

This command will extract and display the directory path from a full file path.

Terminal window
dirname $(readlink -f /path/to/symlink)

By using readlink -f with dirname, you can resolve symlinks before extracting the directory path.

Handle Multiple Paths

Terminal window
dirname /path/to/file1.txt /path/to/file2.txt

It can handle multiple paths as well, extracting the directory paths for each file specified.

Extract Directory Path for Current Directory

Terminal window
dirname .

Even for the current directory, the dirname command will return the directory path.

dirname FAQ:

How do I use dirname in Linux?

To use the dirname command in Linux, execute the following command:

Terminal window
dirname /path/to/file

What does the dirname command do in Linux?

The dirname command in Linux prints the directory component of the given path.

How can I get the parent directory using dirname in Linux?

You can get the parent directory of a file by using the dirname command with the file’s path. Here’s an example:

Terminal window
dirname /path/to/your/file.txt

Can I use dirname with relative paths in Linux?

Yes, you can use dirname with both absolute and relative paths in Linux. For example:

Terminal window
dirname ./relative/path/to/file

How can I ignore any trailing slashes in a path with dirname in Linux?

When using the dirname command in Linux, you can ignore any trailing slashes in a path by specifying the path without the trailing slash. Here’s an example:

Terminal window
dirname /path/with/trailing/slash/

Is there a way to suppress error messages when using dirname in Linux?

If you want to suppress error messages when using the dirname command in Linux, you can redirect the standard error output (stderr) to /dev/null. Here’s an example:

Terminal window
dirname /path/with/error 2>/dev/null

Applications of the dirname command

  • To extract the directory component of a filepath
  • To manipulate filepaths in shell scripts
  • To retrieve the path of a file’s parent directory