Return filename or directory portion of pathname.


      dirname string

Print to standard output all but the final slash-delimited component of a string (presumably a filename) i.e. Delete the filename portion.

MacOS is not case sensitive and dirname does not consult the file system to see if the case is correct or even that the path/file string exists at all.

If string is a single component, dirname prints . (meaning the current directory).

dirname will exit 0 on success, and >0 if an error occurs.


Set the shell variable FOO to /usr/bin:

FOO=`dirname /usr/bin/trail`

Extract the path from the variable pathnamevar and store in the variable result using parameter expansion $( )

$ result=$(dirname "$pathnamevar")

“Then I came up with this crazy idea just to walk out on the stage with no band at all and just start singing whatever came to mind. I actually fought the idea for a while because it seemed almost too radical, but it became obvious what I was supposed to be doing” ~ Bobby McFerrin

Related macOS commands

basename – Strip directory and suffix from filenames.
ls - List information about file(s).
Equivalent Windows command: Variable Substring

Copyright © 1999-2024 SS64.com
Some rights reserved