How to run a command with a different working directory?2019 Community Moderator Electioncd to original location when recording session with “script” commandrun command as different nologin userchroot with working directory specifiedHow does Unix keep track of a user's working directory when navigating the file system?In what cases may some programs require you to run them on a specific directory?sudo start login shell (-i) and stay at current working directoryhow to run shell command inside awkHow to change working directory of a child process by posix_spawn?tmux change default working directory of a session without attachingComparing working directory with “string”
Air travel with refrigerated insulin
Can you take a "free object interaction" while incapacitated?
Magnifying glass in hyperbolic space
Asserting that Atheism and Theism are both faith based positions
Does capillary rise violate hydrostatic paradox?
Should a narrator ever describe things based on a character's view instead of facts?
Friend wants my recommendation but I don't want to give it to him
Do people actually use the word "kaputt" in conversation?
Why does the frost depth increase when the surface temperature warms up?
Are hand made posters acceptable in Academia?
Strange behavior in TikZ draw command
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
Do I have to take mana from my deck or hand when tapping this card?
How would a solely written language work mechanically
Reason why a kingside attack is not justified
A seasonal riddle
How to split IPA spelling into syllables
What is the period/term used describe Giuseppe Arcimboldo's style of painting?
Why can't I get pgrep output right to variable on bash script?
Amorphous proper classes in MK
When is the exact date for EOL of Ubuntu 14.04 LTS?
Showing mass murder in a kid's book
Writing in a Christian voice
Output visual diagram of picture
How to run a command with a different working directory?
2019 Community Moderator Electioncd to original location when recording session with “script” commandrun command as different nologin userchroot with working directory specifiedHow does Unix keep track of a user's working directory when navigating the file system?In what cases may some programs require you to run them on a specific directory?sudo start login shell (-i) and stay at current working directoryhow to run shell command inside awkHow to change working directory of a child process by posix_spawn?tmux change default working directory of a session without attachingComparing working directory with “string”
I use this shell pipeline to get a SQL dump using the terminal:
$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..
As can be seen, I entered the var/lib/mysql
directory and create the dump to a file and come back from where I was initially.
The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql
directory.
Can anyone suggest that?
shell pwd
add a comment |
I use this shell pipeline to get a SQL dump using the terminal:
$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..
As can be seen, I entered the var/lib/mysql
directory and create the dump to a file and come back from where I was initially.
The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql
directory.
Can anyone suggest that?
shell pwd
More generally( cd some_place && do_stuff )
Note the parenthesis.
– ctrl-alt-delor
13 hours ago
add a comment |
I use this shell pipeline to get a SQL dump using the terminal:
$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..
As can be seen, I entered the var/lib/mysql
directory and create the dump to a file and come back from where I was initially.
The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql
directory.
Can anyone suggest that?
shell pwd
I use this shell pipeline to get a SQL dump using the terminal:
$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..
As can be seen, I entered the var/lib/mysql
directory and create the dump to a file and come back from where I was initially.
The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql
directory.
Can anyone suggest that?
shell pwd
shell pwd
edited 5 hours ago
Paradox
163111
163111
asked 14 hours ago
ArefeArefe
17611
17611
More generally( cd some_place && do_stuff )
Note the parenthesis.
– ctrl-alt-delor
13 hours ago
add a comment |
More generally( cd some_place && do_stuff )
Note the parenthesis.
– ctrl-alt-delor
13 hours ago
More generally
( cd some_place && do_stuff )
Note the parenthesis.– ctrl-alt-delor
13 hours ago
More generally
( cd some_place && do_stuff )
Note the parenthesis.– ctrl-alt-delor
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
To be honest, I don't see a reason for the two calls to cd
at all.
You don't seem to use the directory that you cd
into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.
You could therefore, quite likely, just use
mysqldump -uroot -p"craft" --add-drop-table craft
> ~/../docker-entrypoint-initdb.d/base.sql
regardless of what directory you run that from.
Yes, my understanding is not correct that I need to enter thevar/lib/mysql
to run the command for the dump.
– Arefe
14 hours ago
add a comment |
In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.
Firstly, since cd
sets the OLDPWD
variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ;
rather than &&
there:
cd "$workdir" && somecommand ; cd "$OLDPWD"
That's still unreliable if the first cd
fails; to be more robust, we really need
if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi
Even at this point, we're struggling if we need the exit status of somecommand
after this.
It's usually best to run the command in a subshell, and change only the subshell's working directory:
( cd "$workdir" && somecommand )
This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.
1
cd -
is even more portable thancd "$OLDPWD"
– Stéphane Chazelas
13 hours ago
The last approach is also what I normally use.
– Weijun Zhou
12 hours ago
@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to/dev/null
.
– Toby Speight
11 hours ago
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%2f507148%2fhow-to-run-a-command-with-a-different-working-directory%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To be honest, I don't see a reason for the two calls to cd
at all.
You don't seem to use the directory that you cd
into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.
You could therefore, quite likely, just use
mysqldump -uroot -p"craft" --add-drop-table craft
> ~/../docker-entrypoint-initdb.d/base.sql
regardless of what directory you run that from.
Yes, my understanding is not correct that I need to enter thevar/lib/mysql
to run the command for the dump.
– Arefe
14 hours ago
add a comment |
To be honest, I don't see a reason for the two calls to cd
at all.
You don't seem to use the directory that you cd
into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.
You could therefore, quite likely, just use
mysqldump -uroot -p"craft" --add-drop-table craft
> ~/../docker-entrypoint-initdb.d/base.sql
regardless of what directory you run that from.
Yes, my understanding is not correct that I need to enter thevar/lib/mysql
to run the command for the dump.
– Arefe
14 hours ago
add a comment |
To be honest, I don't see a reason for the two calls to cd
at all.
You don't seem to use the directory that you cd
into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.
You could therefore, quite likely, just use
mysqldump -uroot -p"craft" --add-drop-table craft
> ~/../docker-entrypoint-initdb.d/base.sql
regardless of what directory you run that from.
To be honest, I don't see a reason for the two calls to cd
at all.
You don't seem to use the directory that you cd
into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.
You could therefore, quite likely, just use
mysqldump -uroot -p"craft" --add-drop-table craft
> ~/../docker-entrypoint-initdb.d/base.sql
regardless of what directory you run that from.
edited 14 hours ago
answered 14 hours ago
KusalanandaKusalananda
136k17257426
136k17257426
Yes, my understanding is not correct that I need to enter thevar/lib/mysql
to run the command for the dump.
– Arefe
14 hours ago
add a comment |
Yes, my understanding is not correct that I need to enter thevar/lib/mysql
to run the command for the dump.
– Arefe
14 hours ago
Yes, my understanding is not correct that I need to enter the
var/lib/mysql
to run the command for the dump.– Arefe
14 hours ago
Yes, my understanding is not correct that I need to enter the
var/lib/mysql
to run the command for the dump.– Arefe
14 hours ago
add a comment |
In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.
Firstly, since cd
sets the OLDPWD
variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ;
rather than &&
there:
cd "$workdir" && somecommand ; cd "$OLDPWD"
That's still unreliable if the first cd
fails; to be more robust, we really need
if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi
Even at this point, we're struggling if we need the exit status of somecommand
after this.
It's usually best to run the command in a subshell, and change only the subshell's working directory:
( cd "$workdir" && somecommand )
This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.
1
cd -
is even more portable thancd "$OLDPWD"
– Stéphane Chazelas
13 hours ago
The last approach is also what I normally use.
– Weijun Zhou
12 hours ago
@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to/dev/null
.
– Toby Speight
11 hours ago
add a comment |
In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.
Firstly, since cd
sets the OLDPWD
variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ;
rather than &&
there:
cd "$workdir" && somecommand ; cd "$OLDPWD"
That's still unreliable if the first cd
fails; to be more robust, we really need
if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi
Even at this point, we're struggling if we need the exit status of somecommand
after this.
It's usually best to run the command in a subshell, and change only the subshell's working directory:
( cd "$workdir" && somecommand )
This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.
1
cd -
is even more portable thancd "$OLDPWD"
– Stéphane Chazelas
13 hours ago
The last approach is also what I normally use.
– Weijun Zhou
12 hours ago
@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to/dev/null
.
– Toby Speight
11 hours ago
add a comment |
In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.
Firstly, since cd
sets the OLDPWD
variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ;
rather than &&
there:
cd "$workdir" && somecommand ; cd "$OLDPWD"
That's still unreliable if the first cd
fails; to be more robust, we really need
if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi
Even at this point, we're struggling if we need the exit status of somecommand
after this.
It's usually best to run the command in a subshell, and change only the subshell's working directory:
( cd "$workdir" && somecommand )
This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.
In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.
Firstly, since cd
sets the OLDPWD
variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ;
rather than &&
there:
cd "$workdir" && somecommand ; cd "$OLDPWD"
That's still unreliable if the first cd
fails; to be more robust, we really need
if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi
Even at this point, we're struggling if we need the exit status of somecommand
after this.
It's usually best to run the command in a subshell, and change only the subshell's working directory:
( cd "$workdir" && somecommand )
This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.
edited 13 hours ago
answered 13 hours ago
Toby SpeightToby Speight
5,37611132
5,37611132
1
cd -
is even more portable thancd "$OLDPWD"
– Stéphane Chazelas
13 hours ago
The last approach is also what I normally use.
– Weijun Zhou
12 hours ago
@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to/dev/null
.
– Toby Speight
11 hours ago
add a comment |
1
cd -
is even more portable thancd "$OLDPWD"
– Stéphane Chazelas
13 hours ago
The last approach is also what I normally use.
– Weijun Zhou
12 hours ago
@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to/dev/null
.
– Toby Speight
11 hours ago
1
1
cd -
is even more portable than cd "$OLDPWD"
– Stéphane Chazelas
13 hours ago
cd -
is even more portable than cd "$OLDPWD"
– Stéphane Chazelas
13 hours ago
The last approach is also what I normally use.
– Weijun Zhou
12 hours ago
The last approach is also what I normally use.
– Weijun Zhou
12 hours ago
@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to
/dev/null
.– Toby Speight
11 hours ago
@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to
/dev/null
.– Toby Speight
11 hours ago
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%2f507148%2fhow-to-run-a-command-with-a-different-working-directory%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
More generally
( cd some_place && do_stuff )
Note the parenthesis.– ctrl-alt-delor
13 hours ago