basename /var/db/shadow/hash/850F62CD-966C-43A7-9C66-9F9E6799A955Basename can also be used to trim output. For example, let’s say we didn’t need the final portion of the above filename in our output. We could run basename using the -s option, followed by the string at the end that we do not want to see to output of just the first 4 sections:
basename -s -9F9E6799A955 /var/db/shadow/hash/850F62CD-966C-43A7-9C66-9F9E6799A955The dirname command is even more basic. It outputs the directory portion of the file’s path. For example, based on the same string, the following would tell you what directory the UUID files with the passwords are stored in:
dirname /var/db/shadow/hash/850F62CD-966C-43A7-9C66-9F9E6799A955A great example of when this gets more useful is keying off of currently active data. For example, if we’re scripting a make operation, we can use the which command to get an output that just contains the path to the make binary:
which makeWe can then wrap that for expansion and grab just the place that the active make binary is stored:
dirname `which make`This allows us to key other operations off the path of an object. A couple of notable example of this is home or homeDirectory paths and then breaking up data coming into a script via a positional parameter (e.g. $1).
krypted May 27th, 2011
krypted September 29th, 2007
Posted In: Mac OS X