• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

dirname: Calculates the parent directory of a given file or directory path

by admin

“dirname” is a command-line utility that calculates and outputs the parent directory of a given file or directory path. Its primary purpose is to extract the directory portion of a path, excluding the filename or the last component of the path.

When provided with a file or directory path as an argument, “dirname” parses the path and returns the parent directory component. It identifies the portion of the path that precedes the last forward slash (“/”) or backslash (“”) character, depending on the operating system.

The output of “dirname” is the directory path without the filename or the last component. This can be useful in various scenarios, such as script automation, file manipulation, or when working with file paths in general. By using “dirname,” users can easily isolate the directory part of a path for further processing or to perform operations specifically on the parent directory.

For example, if given the path “/home/user/documents/file.txt,” the “dirname” command would return “/home/user/documents,” as it identifies and extracts the parent directory portion of the path.

“dirname” can be particularly handy when used in conjunction with other shell commands or scripts that require working with file paths. It allows users to extract the directory component dynamically, regardless of the specific filename or the structure of the path.

It’s important to note that “dirname” does not validate the existence or accessibility of the directory path it outputs. It simply calculates the parent directory based on the provided path. Therefore, the output of “dirname” may still refer to a non-existent or inaccessible directory.

dirname Command Examples

1. Calculate the parent directory of a given path:

# dirname /path/to/file_or_directory

2. Calculate the parent directory of multiple paths:

# dirname /path/to/file_a /path/to/directory_b

3. Delimit output with a NUL character instead of a newline (useful when combining with xargs):

# dirname --zero /path/to/directory_a p/ath/to/file_b

Summary

In summary, “dirname” is a command-line utility that calculates the parent directory of a given file or directory path. By extracting the directory portion and excluding the filename or the last component, “dirname” provides a convenient way to isolate and work with the parent directory part of a path. This functionality is useful in various scripting or file manipulation scenarios where the parent directory needs to be referenced or operated upon.

Filed Under: Linux

Some more articles you might also be interested in …

  1. git delete-branch: Delete local and remote Git branches
  2. GoBuster: command not found
  3. UNIX / Linux : How crontab validates the access based on the cron.allow and cron.deny files
  4. vnstati Command Examples in Linux
  5. nsenter: command not found
  6. ctags: Generates an index (or tag) file of language objects found in source files for many popular programming languages
  7. espanso: Cross-platform Text Expander written in Rust
  8. aria2c: Fast download utility (Command Examples)
  9. flashrom: command not found
  10. How to add swap space in linux

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright