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”
How to get directions in deep space?
Error in master's thesis, I do not know what to do
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
Asserting that Atheism and Theism are both faith based positions
Checking @@ROWCOUNT failing
"Marked down as someone wanting to sell shares." What does that mean?
How do I lift the insulation blower into the attic?
Why would five hundred and five same as one?
Should I be concerned about student access to a test bank?
How to split IPA spelling into syllables
How do you say "Trust your struggle." in French?
What properties make a magic weapon befit a Rogue more than a DEX-based Fighter?
How to test the sharpness of a knife?
Do I have to take mana from my deck or hand when tapping this card?
Should a narrator ever describe things based on a character's view instead of facts?
What is the meaning of "You've never met a graph you didn't like?"
What do the positive and negative (+/-) transmit and receive pins mean on Ethernet cables?
"Oh no!" in Latin
Why doesn't Gödel's incompleteness theorem apply to false statements?
How would a solely written language work mechanically
Is there any common country to visit for persons holding UK and Schengen visas?
I keep switching characters, how do I stop?
Amorphous proper classes in MK
Sort with assumptions
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
14 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 6 hours ago
Paradox
163111
163111
asked 15 hours ago
ArefeArefe
17611
17611
More generally( cd some_place && do_stuff )
Note the parenthesis.
– ctrl-alt-delor
14 hours ago
add a comment |
More generally( cd some_place && do_stuff )
Note the parenthesis.
– ctrl-alt-delor
14 hours ago
More generally
( cd some_place && do_stuff )
Note the parenthesis.– ctrl-alt-delor
14 hours ago
More generally
( cd some_place && do_stuff )
Note the parenthesis.– ctrl-alt-delor
14 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
15 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
14 hours ago
The last approach is also what I normally use.
– Weijun Zhou
13 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
12 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
15 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
15 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 15 hours ago
answered 15 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
15 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
15 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
15 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
15 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
14 hours ago
The last approach is also what I normally use.
– Weijun Zhou
13 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
12 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
14 hours ago
The last approach is also what I normally use.
– Weijun Zhou
13 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
12 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 14 hours ago
answered 14 hours ago
Toby SpeightToby Speight
5,37611132
5,37611132
1
cd -
is even more portable thancd "$OLDPWD"
– Stéphane Chazelas
14 hours ago
The last approach is also what I normally use.
– Weijun Zhou
13 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
12 hours ago
add a comment |
1
cd -
is even more portable thancd "$OLDPWD"
– Stéphane Chazelas
14 hours ago
The last approach is also what I normally use.
– Weijun Zhou
13 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
12 hours ago
1
1
cd -
is even more portable than cd "$OLDPWD"
– Stéphane Chazelas
14 hours ago
cd -
is even more portable than cd "$OLDPWD"
– Stéphane Chazelas
14 hours ago
The last approach is also what I normally use.
– Weijun Zhou
13 hours ago
The last approach is also what I normally use.
– Weijun Zhou
13 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
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
12 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
14 hours ago