Skip to content

MacOS dirname command

The MacOS dirname command is a useful tool for extracting the directory component of a file path. By using this command, you can easily parse and manipulate file paths in your shell scripts. This command specifically returns the directory component of the given path, excluding the last component. This can be helpful when you need to work with file paths in a programmatic way without having to manually manipulate strings. The dirname command is simple to use and can save you time and effort when working with file paths in your MacOS terminal.

Terminal window
dirname [path]
OptionDescription
NoneNo options available
ParameterDescription
pathPath for which the directory name will be extracted
Terminal window
dirname /Users/johndoe/Documents/report.txt

Returns the directory path “/Users/johndoe/Documents” of the file “report.txt”.

Terminal window
dirname /Users/janedoe/Pictures/

Returns the directory path “/Users/janedoe” of the folder “Pictures”.

Terminal window
dirname ~/Downloads/../Documents/../Pictures/photo.jpg

Returns the directory path ”~/Documents/Pictures” after handling the relative paths.

Terminal window
current_dir=$(dirname $0)
echo "The current script is located in: $current_dir"

Utilizes dirname within a shell script to retrieve and display the directory path where the script is located.

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

Terminal window
dirname /path/to/file

What is the purpose of the dirname command in MacOS?

Section titled “What is the purpose of the dirname command in MacOS?”

The dirname command in MacOS is used to extract the directory component from a given path, providing the path of the specified file or directory’s parent directory.

How can I get the directory name from a file path using dirname in MacOS?

Section titled “How can I get the directory name from a file path using dirname in MacOS?”

You can get the directory name from a file path in MacOS by running the following command:

Terminal window
dirname /path/to/file.txt

Can I use dirname to retrieve the parent directory of a specified path in MacOS?

Section titled “Can I use dirname to retrieve the parent directory of a specified path in MacOS?”

Yes, you can use the dirname command in MacOS to retrieve the parent directory of a specified path by running the following command:

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

How do I suppress the trailing newline when using dirname in MacOS?

Section titled “How do I suppress the trailing newline when using dirname in MacOS?”

To suppress the trailing newline when using the dirname command in MacOS, you can use the -z option. Here is an example:

Terminal window
dirname -z /path/to/file.txt

Is it possible to display the relative path when using dirname in MacOS?

Section titled “Is it possible to display the relative path when using dirname in MacOS?”

Yes, you can display the relative path when using the dirname command in MacOS by specifying the -r option. Here is an example:

Terminal window
dirname -r /path/to/file.txt

How can I handle multiple file paths with dirname in MacOS?

Section titled “How can I handle multiple file paths with dirname in MacOS?”

You can handle multiple file paths with the dirname command in MacOS by providing all the paths as arguments. Here is an example:

Terminal window
dirname /path/to/file1 /path/to/file2
  • Extracting the directory part of a file path
  • Obtaining the parent directory of a specified file or directory
  • Scripting and automation tasks
  • Working with file and directory paths in shell scripts