Forrest logo
back to the dirname tool

dirname:tldr:d4258

dirname: Calculate the parent directory of a given path.
$ dirname ${filename_or_directory}
try on your machine

The command dirname ${filename_or_directory} is a shell command used to retrieve the directory name or path of a file or directory.

Here's how it works:

  1. The command starts with dirname, which is a utility built into Unix-like operating systems that extracts the directory part of a given path.

  2. Within the command, ${filename_or_directory} denotes a placeholder that should be replaced with an actual file name or directory path.

  3. When you execute the command, the shell will replace ${filename_or_directory} with the provided file name or directory path.

  4. The dirname utility then looks at the path and removes the file name or last component of the directory path, returning the remaining directory path.

For example, if you run the command dirname /home/user/files/test.txt, it will return /home/user/files. Similarly, if you run dirname /home/user/files/, it will return /home/user.

In summary, the dirname command helps in extracting the directory component of a given file name or directory path.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the dirname tool