Bash Directory – How to Reference a File’s Directory from the Command Line

bashdirectory

I can type:

dirname ~/home/blah/file.zip

for instance, and this works fine alone, but when I use this syntax as the parameter for some command it always reads 'dirname' as the desired directory. i.e.:

cd dirname ~/home/blah/file.zip
bash: cd: dirname: No such file or directory

Basically, how do I get 'dirname file' to be read as one entity?

Best Answer

cd "$(dirname ~/home/blah/file.zip)"

$( is a form of command substitution. The BashGuide Wiki has some good information about this process.

Related Question