kmandla@6m47421: /usr/share/pixmaps$ dirname /usr/share/pixmaps /usr/share
dirname strips away the directory and shows only the path up to the final folder. In the example it was the current directory — you get the same results from
dirname $(pwd) — but you can feed it any path (or more than one), so it’s not correct to say “your current directory.”
If you feed
dirname a path and file name, you get the path of to the file, which is where
dirname becomes useful.
kmandla@6m47421: ~$ dirname /home/kmandla/downloads/list.html.txt /home/kmandla/downloads
Now you can effectively strip away the filename from a string, and leave only the path to it.
kmandla@6m47421: ~$ dirname $(find /usr/share/pixmaps/ -type f -iname "*png") | sort -u /usr/share/pixmaps /usr/share/pixmaps/pidgin /usr/share/pixmaps/pidgin/animations/16 /usr/share/pixmaps/pidgin/buttons ...
So I know I have png files somewhere inside /usr/share/pixmaps, /usr/share/pixmaps/pidgin, and so on.
Now comes the strange part: If you give
dirname a file in your current path, you get a lonesome dot.
kmandla@6m47421: ~$ dirname .bashrc .
This strikes me as odd behavior, since this is also
kmandla@6m47421: ~$ dirname /home/kmandla/.bashrc /home/kmandla
The man page comes to the rescue this time, and after reading that, I would hazard to guess that
dirname is just traipsing along the string until it reaches the last slash, then dumping everything before that to STDOUT. No slashes means … a dot.😕
And my suspicions are confirmed with this:
kmandla@6m47421: ~$ dirname this/is/a/test this/is/a
dirname is a filter for slash marks, and whether or not that represents an actual folder tree on your system is … moot.🙄
dirname is useful in the same way as
basename, but again, I can’t give you any tried-and-true uses that aren’t specific to my own real-life adventures. I leave it to you to come up with something earth-shattering and life-changing to use with
dirname. It’s not an impossibility, even if it is just a slash-mark filter. …😉