This is a command-line trick from a book-"The Productive Programmer":

pushd `which java`/..

The author says this command can temporarily go the directory where the executable command java lives.

When I typed this command in Linux, Bash complained that:

bash: pushd: /usr/bin/java/..: Not a directory

I also tried to change directory to /usr/bin/java/.. directly:

$ pushd /usr/bin/java/.. 
bash: pushd: /usr/bin/java/..: Not a directory

Why did I fail? Why cannot change to a directory using /..? How to modify this command to fulfill the same task?

  • 1
    Java is a file in that directory. Try using dirname...
    – Jeff Schaller
    Dec 24, 2015 at 13:17
  • 1
    path/.. depends on the path being a directory (where '..' is a reference to its parent).
    – Skaperen
    Dec 24, 2015 at 13:23

1 Answer 1


That fails because /usr/bin/java is a file, not a directory.

You can do:

cd "$(dirname "$(which java)")"

Or use parameter expansion in zsh:

cd "${"$(which java)"%/*}"
  • 2
    A somewhat cleaner solution for ZSH would be cd =java(:h)
    – thrig
    Dec 24, 2015 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .