Using a Linux shell, how do I start a program with a different working directory from the current working directory?
For example, I have a binary file helloworld that creates the file hello-world.txt in the current directory.
This file is inside of directory /a.
Currently, I am in the directory /b. I want to start my program running ../a/helloworld and get the hello-world.txt somewhere in a third directory /c.
12 Answers 12
Call the program like this:
(cd /c; /a/helloworld)
The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c, then executes helloworld from /a. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.
Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c, make the execution of helloworld conditional:
(cd /c && /a/helloworld)
Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld via exec:
(cd /c && exec /a/helloworld)
[Thanks to Josh and Juliano for giving tips on improving this answer!]
7 Comments
() construct will have the exitcode of the last command inside. You can check that either directly through chaining or by inspecting $?.Similar to David Schmitt's answer, plus Josh's suggestion, but doesn't leave a shell process running:
(cd /c && exec /a/helloworld)
This way is more similar to how you usually run commands on the shell. To see the practical difference, you have to run ps ef from another shell with each solution.
2 Comments
exec replaces the shell process with the given command, without creating a new process.Using pushd/popd and a sub shell:
(pushd SOME_PATH && run_stuff; popd)
If you don't want a subshell, you could run them as separate commands, but be aware that if any of them fail, your shell might not end up back in the original directory at the end:
pushd SOME_PATH
run_stuff
popd
Demo:
$ pwd
/home/abhijit
$ pushd /tmp # directory changed
$ pwd
/tmp
$ popd
$ pwd
/home/abhijit
11 Comments
pushd SOME_PATH && run_stuff && popd -- if run_stuff fails, than popd is not going to be executed.set -e in the file and then it would fail to popd.pushd "${SOME_PATH}" && run_stuff; popd is better than the current answer, since the pushd/popd semantics were specifically designed for this situation of going into some directory and then coming back to the original one.sh -c 'cd /c && ../a/helloworld'
1 Comment
Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:
cd SOME_PATH && run_some_command ; cd -
Comments
I always think UNIX tools should be written as filters, read input from stdin and write output to stdout. If possible you could change your helloworld binary to write the contents of the text file to stdout rather than a specific file. That way you can use the shell to write your file anywhere.
$ cd ~/b
$ ~/a/helloworld > ~/c/helloworld.txt
1 Comment
why not keep it simple
cd SOME_PATH && run_some_command && cd -
the last 'cd' command will take you back to the last pwd directory. This should work on all *nix systems.
2 Comments
run_some_command fails, cd - won't get executed.One way to do that is to create a wrapper shell script.
The shell script would change the current directory to /c, then run /a/helloworld. Once the shell script exits, the current directory reverts back to /b.
Here's a bash shell script example:
#!/bin/bash
cd /c
/a/helloworld
Comments
The following worked for my simple need to set up an alias to which I can append an argument, without having to put the instructions in a function; worked in Bash and ZSH:
$ CWD=/your/target/dir command args
My use case: I have a shell script named run-service that resolves paths to other files based on its own location. If I call it with cd ~/Code/company/scripts && run-service dev/some-service, it will expect to find a config file in ~/Code/company/services/dev/some-service. So instead of always having to cd into the directory and calling the script, I just did this:
# Alias definition
alias run-service="CWD=/your/target/dir run-service"
# Calling the alias
$ run-service foo
It's probably too simplistic to be of general use, but it works for my basic use-case.
Comments
If you always want it to go to /C, use an absolute path when you write the file.
Comments
If anyone is still searching for this nowadays, here is how I did it on Windows 10:
Set-Location -Path "<absolute path to your working directory>"; Start-Process "<absolute path to your file>"
Comments
If you want to perform this inside your program then I would do something like:
#include <unistd.h>
int main()
{
if(chdir("/c") < 0 )
{
printf("Failed\n");
return -1 ;
}
// rest of your program...
}
suresets the working directory to the home directory of user you specify before running any-ccommands. This was very helpful to me.a:visitedlink and an upvote already on it. It's another thing when you're the asker, or even the answerer. There should be a badge for that... "Forgetful"