Pass output of “whereis” command to “cd” to change directory in one step
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 ?
bash command-line cd-command command-substitution
add a comment |
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 ?
bash command-line cd-command command-substitution
What is the output of yor second command?
– Nils
Jun 18 '16 at 7:20
bash: cd: . /usr/local/lib: No such file or directory
– Vicky Dev
Jun 18 '16 at 9:38
add a comment |
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 ?
bash command-line cd-command command-substitution
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 ?
bash command-line cd-command command-substitution
bash command-line cd-command command-substitution
edited 3 hours ago
Rui F Ribeiro
39.5k1479132
39.5k1479132
asked Jun 17 '16 at 4:40
Vicky DevVicky Dev
15218
15218
What is the output of yor second command?
– Nils
Jun 18 '16 at 7:20
bash: cd: . /usr/local/lib: No such file or directory
– Vicky Dev
Jun 18 '16 at 9:38
add a comment |
What is the output of yor second command?
– Nils
Jun 18 '16 at 7:20
bash: cd: . /usr/local/lib: No such file or directory
– Vicky Dev
Jun 18 '16 at 9:38
What is the output of yor second command?
– Nils
Jun 18 '16 at 7:20
What is the output of yor second command?
– Nils
Jun 18 '16 at 7:20
bash: cd: . /usr/local/lib: No such file or directory– Vicky Dev
Jun 18 '16 at 9:38
bash: cd: . /usr/local/lib: No such file or directory– Vicky Dev
Jun 18 '16 at 9:38
add a comment |
3 Answers
3
active
oldest
votes
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 memissing operanderror.
– Vicky Dev
Jun 17 '16 at 4:47
2
@Rahul Yes, I expect thatwhichis 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, saycd $(which firefoxx | xargs dirname)then it returnsmissing operandwhich is the same error that VickyDev reports.
– John1024
Jun 17 '16 at 5:03
1
@VickyDev rememberdirnameis used to strip last component from file name. so it depends on your system. Because when I use the same commandwhereis findit gives mefind: /usr/bin/find, but I can'tcdto/usr/bin/findit will give me error like 'No such file' because last component ofwhereisresultfindis not a directory. so I had to usedirnamealong withcd$(..)to change directory.
– Rahul
Jun 17 '16 at 5:18
1
@Rahul, got that, thanks a lot, your last answer (the one withfind) is really re-usable and generalised
– Vicky Dev
Jun 17 '16 at 5:34
2
you should probably replacewhichwithcommand -vand see unix.stackexchange.com/questions/85249/…
– Dani_l
Jun 17 '16 at 15:24
|
show 11 more comments
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.
Nope, sorry it doesn't work, doingwhereis node_modulesgives me path/usr/local/lib/node_modulesbut doingcdlike you did doesn't put me in that directory path.
– Vicky Dev
Jun 17 '16 at 4:45
Trycd $(dirname $(whereis node_modules | cut -d' ' -f2) )
– John1024
Jun 17 '16 at 5:08
Very close, but it goes to/usr/local/libbut stays out ofnode_modules.
– Vicky Dev
Jun 17 '16 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.lsis at/bin/ls, socd $(dirname $(whereis ls) )will resolve tocd /bin. I will adjust my answer to go into a directory if that is what you have specified.
– DopeGhoti
Jun 17 '16 at 5:35
1
@VickyDev this will surely help you.
– Rahul
Jun 17 '16 at 6:26
|
show 1 more comment
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
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 tocd, string operation on the output of command just like in other languages ?
– Vicky Dev
Jun 17 '16 at 4:59
What do you mean one step? Is notcd "$(npm get prefix)/lib/node_modules"one step only?
– cuonglm
Jun 17 '16 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 thenode_modulesdir but generalised for any directory in my system
– Vicky Dev
Jun 17 '16 at 5:01
1
It depends on your tool. Here you want to get tonpmdefault modules location, then it's alwaysnpmprefix +lib/node_modules, other tools need other method.
– cuonglm
Jun 17 '16 at 5:02
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f290321%2fpass-output-of-whereis-command-to-cd-to-change-directory-in-one-step%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 memissing operanderror.
– Vicky Dev
Jun 17 '16 at 4:47
2
@Rahul Yes, I expect thatwhichis 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, saycd $(which firefoxx | xargs dirname)then it returnsmissing operandwhich is the same error that VickyDev reports.
– John1024
Jun 17 '16 at 5:03
1
@VickyDev rememberdirnameis used to strip last component from file name. so it depends on your system. Because when I use the same commandwhereis findit gives mefind: /usr/bin/find, but I can'tcdto/usr/bin/findit will give me error like 'No such file' because last component ofwhereisresultfindis not a directory. so I had to usedirnamealong withcd$(..)to change directory.
– Rahul
Jun 17 '16 at 5:18
1
@Rahul, got that, thanks a lot, your last answer (the one withfind) is really re-usable and generalised
– Vicky Dev
Jun 17 '16 at 5:34
2
you should probably replacewhichwithcommand -vand see unix.stackexchange.com/questions/85249/…
– Dani_l
Jun 17 '16 at 15:24
|
show 11 more comments
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 memissing operanderror.
– Vicky Dev
Jun 17 '16 at 4:47
2
@Rahul Yes, I expect thatwhichis 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, saycd $(which firefoxx | xargs dirname)then it returnsmissing operandwhich is the same error that VickyDev reports.
– John1024
Jun 17 '16 at 5:03
1
@VickyDev rememberdirnameis used to strip last component from file name. so it depends on your system. Because when I use the same commandwhereis findit gives mefind: /usr/bin/find, but I can'tcdto/usr/bin/findit will give me error like 'No such file' because last component ofwhereisresultfindis not a directory. so I had to usedirnamealong withcd$(..)to change directory.
– Rahul
Jun 17 '16 at 5:18
1
@Rahul, got that, thanks a lot, your last answer (the one withfind) is really re-usable and generalised
– Vicky Dev
Jun 17 '16 at 5:34
2
you should probably replacewhichwithcommand -vand see unix.stackexchange.com/questions/85249/…
– Dani_l
Jun 17 '16 at 15:24
|
show 11 more comments
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.
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.
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Jun 17 '16 at 4:43
RahulRahul
9,02412842
9,02412842
No, these doesn't work either,cd $(which node_modules | xargs dirname)gives memissing operanderror.
– Vicky Dev
Jun 17 '16 at 4:47
2
@Rahul Yes, I expect thatwhichis 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, saycd $(which firefoxx | xargs dirname)then it returnsmissing operandwhich is the same error that VickyDev reports.
– John1024
Jun 17 '16 at 5:03
1
@VickyDev rememberdirnameis used to strip last component from file name. so it depends on your system. Because when I use the same commandwhereis findit gives mefind: /usr/bin/find, but I can'tcdto/usr/bin/findit will give me error like 'No such file' because last component ofwhereisresultfindis not a directory. so I had to usedirnamealong withcd$(..)to change directory.
– Rahul
Jun 17 '16 at 5:18
1
@Rahul, got that, thanks a lot, your last answer (the one withfind) is really re-usable and generalised
– Vicky Dev
Jun 17 '16 at 5:34
2
you should probably replacewhichwithcommand -vand see unix.stackexchange.com/questions/85249/…
– Dani_l
Jun 17 '16 at 15:24
|
show 11 more comments
No, these doesn't work either,cd $(which node_modules | xargs dirname)gives memissing operanderror.
– Vicky Dev
Jun 17 '16 at 4:47
2
@Rahul Yes, I expect thatwhichis 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, saycd $(which firefoxx | xargs dirname)then it returnsmissing operandwhich is the same error that VickyDev reports.
– John1024
Jun 17 '16 at 5:03
1
@VickyDev rememberdirnameis used to strip last component from file name. so it depends on your system. Because when I use the same commandwhereis findit gives mefind: /usr/bin/find, but I can'tcdto/usr/bin/findit will give me error like 'No such file' because last component ofwhereisresultfindis not a directory. so I had to usedirnamealong withcd$(..)to change directory.
– Rahul
Jun 17 '16 at 5:18
1
@Rahul, got that, thanks a lot, your last answer (the one withfind) is really re-usable and generalised
– Vicky Dev
Jun 17 '16 at 5:34
2
you should probably replacewhichwithcommand -vand see unix.stackexchange.com/questions/85249/…
– Dani_l
Jun 17 '16 at 15:24
No, these doesn't work either,
cd $(which node_modules | xargs dirname) gives me missing operand error.– Vicky Dev
Jun 17 '16 at 4:47
No, these doesn't work either,
cd $(which node_modules | xargs dirname) gives me missing operand error.– Vicky Dev
Jun 17 '16 at 4:47
2
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 5:03
@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 5:03
1
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 5:18
@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 5:18
1
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 5:34
@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 5:34
2
2
you should probably replace
which with command -v and see unix.stackexchange.com/questions/85249/…– Dani_l
Jun 17 '16 at 15:24
you should probably replace
which with command -v and see unix.stackexchange.com/questions/85249/…– Dani_l
Jun 17 '16 at 15:24
|
show 11 more comments
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.
Nope, sorry it doesn't work, doingwhereis node_modulesgives me path/usr/local/lib/node_modulesbut doingcdlike you did doesn't put me in that directory path.
– Vicky Dev
Jun 17 '16 at 4:45
Trycd $(dirname $(whereis node_modules | cut -d' ' -f2) )
– John1024
Jun 17 '16 at 5:08
Very close, but it goes to/usr/local/libbut stays out ofnode_modules.
– Vicky Dev
Jun 17 '16 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.lsis at/bin/ls, socd $(dirname $(whereis ls) )will resolve tocd /bin. I will adjust my answer to go into a directory if that is what you have specified.
– DopeGhoti
Jun 17 '16 at 5:35
1
@VickyDev this will surely help you.
– Rahul
Jun 17 '16 at 6:26
|
show 1 more comment
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.
Nope, sorry it doesn't work, doingwhereis node_modulesgives me path/usr/local/lib/node_modulesbut doingcdlike you did doesn't put me in that directory path.
– Vicky Dev
Jun 17 '16 at 4:45
Trycd $(dirname $(whereis node_modules | cut -d' ' -f2) )
– John1024
Jun 17 '16 at 5:08
Very close, but it goes to/usr/local/libbut stays out ofnode_modules.
– Vicky Dev
Jun 17 '16 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.lsis at/bin/ls, socd $(dirname $(whereis ls) )will resolve tocd /bin. I will adjust my answer to go into a directory if that is what you have specified.
– DopeGhoti
Jun 17 '16 at 5:35
1
@VickyDev this will surely help you.
– Rahul
Jun 17 '16 at 6:26
|
show 1 more comment
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.
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.
edited Jun 17 '16 at 14:53
dhag
11.3k33045
11.3k33045
answered Jun 17 '16 at 4:43
DopeGhotiDopeGhoti
43.9k55582
43.9k55582
Nope, sorry it doesn't work, doingwhereis node_modulesgives me path/usr/local/lib/node_modulesbut doingcdlike you did doesn't put me in that directory path.
– Vicky Dev
Jun 17 '16 at 4:45
Trycd $(dirname $(whereis node_modules | cut -d' ' -f2) )
– John1024
Jun 17 '16 at 5:08
Very close, but it goes to/usr/local/libbut stays out ofnode_modules.
– Vicky Dev
Jun 17 '16 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.lsis at/bin/ls, socd $(dirname $(whereis ls) )will resolve tocd /bin. I will adjust my answer to go into a directory if that is what you have specified.
– DopeGhoti
Jun 17 '16 at 5:35
1
@VickyDev this will surely help you.
– Rahul
Jun 17 '16 at 6:26
|
show 1 more comment
Nope, sorry it doesn't work, doingwhereis node_modulesgives me path/usr/local/lib/node_modulesbut doingcdlike you did doesn't put me in that directory path.
– Vicky Dev
Jun 17 '16 at 4:45
Trycd $(dirname $(whereis node_modules | cut -d' ' -f2) )
– John1024
Jun 17 '16 at 5:08
Very close, but it goes to/usr/local/libbut stays out ofnode_modules.
– Vicky Dev
Jun 17 '16 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.lsis at/bin/ls, socd $(dirname $(whereis ls) )will resolve tocd /bin. I will adjust my answer to go into a directory if that is what you have specified.
– DopeGhoti
Jun 17 '16 at 5:35
1
@VickyDev this will surely help you.
– Rahul
Jun 17 '16 at 6:26
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 4:45
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 4:45
Try
cd $(dirname $(whereis node_modules | cut -d' ' -f2) )– John1024
Jun 17 '16 at 5:08
Try
cd $(dirname $(whereis node_modules | cut -d' ' -f2) )– John1024
Jun 17 '16 at 5:08
Very close, but it goes to
/usr/local/lib but stays out of node_modules.– Vicky Dev
Jun 17 '16 at 5:11
Very close, but it goes to
/usr/local/lib but stays out of node_modules.– Vicky Dev
Jun 17 '16 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 '16 at 5:35
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 5:35
1
1
@VickyDev this will surely help you.
– Rahul
Jun 17 '16 at 6:26
@VickyDev this will surely help you.
– Rahul
Jun 17 '16 at 6:26
|
show 1 more comment
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
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 tocd, string operation on the output of command just like in other languages ?
– Vicky Dev
Jun 17 '16 at 4:59
What do you mean one step? Is notcd "$(npm get prefix)/lib/node_modules"one step only?
– cuonglm
Jun 17 '16 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 thenode_modulesdir but generalised for any directory in my system
– Vicky Dev
Jun 17 '16 at 5:01
1
It depends on your tool. Here you want to get tonpmdefault modules location, then it's alwaysnpmprefix +lib/node_modules, other tools need other method.
– cuonglm
Jun 17 '16 at 5:02
add a comment |
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
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 tocd, string operation on the output of command just like in other languages ?
– Vicky Dev
Jun 17 '16 at 4:59
What do you mean one step? Is notcd "$(npm get prefix)/lib/node_modules"one step only?
– cuonglm
Jun 17 '16 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 thenode_modulesdir but generalised for any directory in my system
– Vicky Dev
Jun 17 '16 at 5:01
1
It depends on your tool. Here you want to get tonpmdefault modules location, then it's alwaysnpmprefix +lib/node_modules, other tools need other method.
– cuonglm
Jun 17 '16 at 5:02
add a comment |
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
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
answered Jun 17 '16 at 4:54
cuonglmcuonglm
103k23202302
103k23202302
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 tocd, string operation on the output of command just like in other languages ?
– Vicky Dev
Jun 17 '16 at 4:59
What do you mean one step? Is notcd "$(npm get prefix)/lib/node_modules"one step only?
– cuonglm
Jun 17 '16 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 thenode_modulesdir but generalised for any directory in my system
– Vicky Dev
Jun 17 '16 at 5:01
1
It depends on your tool. Here you want to get tonpmdefault modules location, then it's alwaysnpmprefix +lib/node_modules, other tools need other method.
– cuonglm
Jun 17 '16 at 5:02
add a comment |
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 tocd, string operation on the output of command just like in other languages ?
– Vicky Dev
Jun 17 '16 at 4:59
What do you mean one step? Is notcd "$(npm get prefix)/lib/node_modules"one step only?
– cuonglm
Jun 17 '16 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 thenode_modulesdir but generalised for any directory in my system
– Vicky Dev
Jun 17 '16 at 5:01
1
It depends on your tool. Here you want to get tonpmdefault modules location, then it's alwaysnpmprefix +lib/node_modules, other tools need other method.
– cuonglm
Jun 17 '16 at 5:02
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 4:59
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 4:59
What do you mean one step? Is not
cd "$(npm get prefix)/lib/node_modules" one step only?– cuonglm
Jun 17 '16 at 5:00
What do you mean one step? Is not
cd "$(npm get prefix)/lib/node_modules" one step only?– cuonglm
Jun 17 '16 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 '16 at 5:01
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 5:01
1
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 5:02
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 5:02
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f290321%2fpass-output-of-whereis-command-to-cd-to-change-directory-in-one-step%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the output of yor second command?
– Nils
Jun 18 '16 at 7:20
bash: cd: . /usr/local/lib: No such file or directory– Vicky Dev
Jun 18 '16 at 9:38