I can't find a way to pass output of whereis command to cd command in same line so I don't have to do cd in the second step.

I have tried passing like below:

cd $(whereis node_modules)


cd "`dirname $(whereis node_modules)`"


cd "$(whereis node_modules)"

But none of the above method works.

Can somebody find what should be wrong in above codes ?

  • What is the output of yor second command?
    – Nils
    Jun 18, 2016 at 7:20
  • bash: cd: . /usr/local/lib: No such file or directory
    – Vicky Dev
    Jun 18, 2016 at 9:38

4 Answers 4


You can do that with,

cd "`which node_modules`"

With dirname to get the directory:

cd "$(dirname "$(which node_modules)" )"

as you have mentioned in the comment I am expecting to do this in one step & assuming nod_module is a directory, so you can do that with the following command:

cd $(whereis node_modules | cut -d ' ' -f2)

(Note that the latter command assumes that the Linux whereis is being used, not the BSD one, and that the path does not contain any spaces.)

As suggested by @Dani_I, you can have a look at this Why not use "which"? What to use then?, which might be more useful.

  • No, these doesn't work either, cd $(which node_modules | xargs dirname) gives me missing operand error.
    – Vicky Dev
    Jun 17, 2016 at 4:47
  • 2
    @Rahul Yes, I expect that which is installed. I don't have node_modules installed but I tried your command for firefox: cd $(which firefox | xargs dirname) and it worked fine. When I tried it with a bad name, say cd $(which firefoxx | xargs dirname) then it returns missing operand which is the same error that VickyDev reports.
    – John1024
    Jun 17, 2016 at 5:03
  • 1
    @VickyDev remember dirname is used to strip last component from file name. so it depends on your system. Because when I use the same command whereis find it gives me find: /usr/bin/find, but I can't cd to /usr/bin/find it will give me error like 'No such file' because last component of whereis result find is not a directory. so I had to use dirname along with cd$(..) to change directory.
    – Rahul
    Jun 17, 2016 at 5:18
  • 1
    @Rahul, got that, thanks a lot, your last answer (the one with find) is really re-usable and generalised
    – Vicky Dev
    Jun 17, 2016 at 5:34
  • 2
    you should probably replace which with command -v and see unix.stackexchange.com/questions/85249/…
    – Dani_l
    Jun 17, 2016 at 15:24

This appears to do the trick:

cd "$(dirname "$(whereis node_modules)")"

If, as per your comment, you want to go into the target if it is a directory:

location=$(whereis node_modules)
if [[ -d "$location" ]]; then
    cd "$location"
    cd "$(dirname "$location" )"

The above could easily be made into a function in your .bash_profile.

  • Nope, sorry it doesn't work, doing whereis node_modules gives me path /usr/local/lib/node_modules but doing cd like you did doesn't put me in that directory path.
    – Vicky Dev
    Jun 17, 2016 at 4:45
  • Try cd $(dirname $(whereis node_modules | cut -d' ' -f2) )
    – John1024
    Jun 17, 2016 at 5:08
  • Very close, but it goes to /usr/local/lib but stays out of node_modules.
    – Vicky Dev
    Jun 17, 2016 at 5:11
  • That's because the command I gave you (for which you asked) goes to the directory in which the specified thing you searched for resides. ls is at /bin/ls, so cd $(dirname $(whereis ls) ) will resolve to cd /bin. I will adjust my answer to go into a directory if that is what you have specified.
    – DopeGhoti
    Jun 17, 2016 at 5:35
  • 1
    @VickyDev this will surely help you.
    – Rahul
    Jun 17, 2016 at 6:26

whereis gives you the pattern name and the location, separated by colon, so performing cd or dirname on whereis result can not work:

$ whereis node_modules
node_modules: /usr/lib/node_modules

The proper method is using npm itself to get its default prefix:

$ cd "$(npm get prefix)/lib/node_modules"
$ pwd
  • Your answer is good, thanks, but I was expecting to do it in same step, can't we just remove everything before first colon including colon and pass it to cd, string operation on the output of command just like in other languages ?
    – Vicky Dev
    Jun 17, 2016 at 4:59
  • What do you mean one step? Is not cd "$(npm get prefix)/lib/node_modules" one step only?
    – cuonglm
    Jun 17, 2016 at 5:00
  • But when I don't know the whole path and just last directory name then how would I use your command ? It's not just about the node_modules dir but generalised for any directory in my system
    – Vicky Dev
    Jun 17, 2016 at 5:01
  • 1
    It depends on your tool. Here you want to get to npm default modules location, then it's always npm prefix + lib/node_modules, other tools need other method.
    – cuonglm
    Jun 17, 2016 at 5:02

This works for me. Where is has -q (quite) option, which is just for the purpose of passing result in command line.

cd `whereis -q node_modules`

You must log in to answer this question.

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