4

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)

Or

cd "`dirname $(whereis node_modules)`"

Also

cd "$(whereis node_modules)"

But none of the above method works.

Can somebody find what should be wrong in above codes ?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Vicky Dev
  • 259

4 Answers4

6

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"
else
    cd "$(dirname "$location" )"
fi

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

dhag
  • 15,736
  • 4
  • 55
  • 65
DopeGhoti
  • 76,081
  • 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 '16 at 04:45
  • Try cd $(dirname $(whereis node_modules | cut -d' ' -f2) ) – John1024 Jun 17 '16 at 05:08
  • Very close, but it goes to /usr/local/lib but stays out of node_modules. – Vicky Dev Jun 17 '16 at 05: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 '16 at 05:35
  • @DopeGhoti, many thanks to you for suggesting an alternate way. Can you post links for how to create function in .bash_profile in your answer ? – Vicky Dev Jun 17 '16 at 05:42
  • 1
    @VickyDev this will surely help you. – Rahul Jun 17 '16 at 06:26
6

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.

Rahul
  • 13,589
  • No, these doesn't work either, cd $(which node_modules | xargs dirname) gives me missing operand error. – Vicky Dev Jun 17 '16 at 04:47
  • @VickyDev are you sure ? I tested same command replacing node_modules with find and it cd to dir – Rahul Jun 17 '16 at 04:50
  • Yes I tried it several times, but it gives me missing operand error – Vicky Dev Jun 17 '16 at 04:53
  • missing operand means that you specified a command which which could not find on your system. Try a different command, one that which can locate. – John1024 Jun 17 '16 at 04:54
  • @John1024 as OP have specified ubuntu in tag, assuming that, it should have which command, what you say ? – Rahul Jun 17 '16 at 05:00
  • 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 '16 at 05:03
  • Doing which node_modules give me empty string, but doing whereis node_modules gives me node_modules: /usr/local/lib/node_modules, so how can I cd to that path (removing colon and everything before it) in same step ? – Vicky Dev Jun 17 '16 at 05:05
  • @VickyDev see my updated answer – Rahul Jun 17 '16 at 05:12
  • That works, thanks, so it will work for any whereis directory path which have a colon and directory name before it right ?, Generalised ? If yes then I will at-once accept it. – Vicky Dev Jun 17 '16 at 05:14
  • 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 '16 at 05:18
  • It won't work if there are multiple paths(space delimited), I get that, but it should work if there is only one path location of any component/command/directory, right ? I guess that's about it then. – Vicky Dev Jun 17 '16 at 05:22
  • Oh no, problem is there, I have also installed bower it is installed only in one path /usr/local/bin/bower but when I use your command like cd $(whereis bower | cut -d ' ' -f2) it gives me bash: cd: /usr/local/bin/bower: Not a directory error. – Vicky Dev Jun 17 '16 at 05:27
  • @VickyDev at that time you would have to use cd $(whereis bower | cut -d ' ' -f2 | xargs dirname) – Rahul Jun 17 '16 at 05:29
  • @VickyDev as I told you, it depends on your system, dirname is used to strip last component of path. previous command gave you error because bower is not a directory. – Rahul Jun 17 '16 at 05:30
  • 1
    @Rahul, got that, thanks a lot, your last answer (the one with find) is really re-usable and generalised – Vicky Dev Jun 17 '16 at 05:34
  • 2
    you should probably replace which with command -v and see http://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then – Dani_l Jun 17 '16 at 15:24
4

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
/usr/lib/node_modules
cuonglm
  • 153,898
  • 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 '16 at 04:59
  • What do you mean one step? Is not cd "$(npm get prefix)/lib/node_modules" one step only? – cuonglm Jun 17 '16 at 05: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 '16 at 05: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 '16 at 05:02
0

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`
Zaw
  • 1