Is there a way to execute a command in a different directory without having to cd
to it? I know that I could simply cd
in and cd
out, but I'm just interested in the possibilities of forgoing the extra steps :)
I don't know if this counts, but you can make a subshell:
$ (cd /var/log && cp -- *.log ~/Desktop)
The directory is only changed for that subshell, so you avoid the work of needing to cd -
afterwards.
-
2
-
Nice, I had forgotten altogether about sub-shells, very handy in this case. – Naftuli Kay May 26 '11 at 21:02
-
7To avoid potential chaos, sub out the semicolon with two ampersands:
$ (cd /var/log && cp *.log ~/Desktop)
. That way, if the directory doesn't exist, no further commands are executed. – Naftuli Kay Apr 11 '13 at 19:38 -
3Note for anyone else as naive as me: the $ at start isn't part of the command, just everything after it. – Maltronic Apr 15 '17 at 13:58
-
1
Some programs have options with which you can tell them to chdir(2) themselves (e.g. GNU tar’s -C
/--directory
).
Outside of such programs though, something will have to chdir. You could write and use some sort of compiled “binary” program instead of having the shell do it, but it probably would not yield much benefit.
In a comment in another answer, you gave an example:
execindirectory -d /var/log "cp *.log ~/Desktop"
Since the *.log
pattern is expanded by the shell itself (not cp), something will have to chdir to the directory before having a shell evaluate your command.
If you are just interesting in avoiding having to “cd back”, then you can use a subshell to isolate the effect of the cd from your working shell instance.
(cd /path/to/dir && some command)
You can package this up in a shell function.
(I dropped the -d
option from your example usage since there is little point to this command if the directory is actually optional.)
runindir() { (cd "$1" && shift && eval "$@"); }
runindir /var/log 'cp *.log ~/Desktop' # your example
runindir /var/log cp \*.log \~/Desktop # eval takes multiple args
runindir /var/log cp \*.log ~/Desktop # it is okay to expand tilde first
-
2
Not to undermine the value of answers given by other people, but I believe what you want is this:
(cd /path/to && ./executable [ARGS])
Note the parens to invoke cd
in a sub-shell.
-
I don't think so. As I understand, he wants to end up in the initial directory. – Adam Byrtek May 26 '11 at 17:51
-
Oh, I totally forgot to wrap that into sub-shell. Corrected my answer, thank you. – alex May 26 '11 at 18:48
Here is something that should let you cd
back where you were (using Bash), since not forgetting to do so seems to be the purpose of the question:
# Save where you are and cd to other dir
pushd /path/to/dir/that/needs/to/be/current/dir
run-your-command
# Get back where you were at the beginning.
popd
(EDIT: slightly shorter version, thanks to @Random832)
-
1
-
Sadly, your example:
execindirectory -d /var/log "cp *.log ~/Desktop"
doesn't need a change to the dir, because
cp /var/log/*.log ~/Desktop
would do the same. Can't you get closer to your real problem? Because we might know a better solution for that too.
A complicated way to solve your problem, which is far away from the elegance of Michaels solution, is the usage of find, which has a switch '-execdir' to be performed in the dir, where a file is found. Badly adopted to your example:
find /var/log -maxdepth 1 -type f -name "*.log" -execdir echo cp {} ~/Desktop ";"
Maybe it is useful for your real problem. -okdir instead of -execdir will ask you to confirm every invocation.
-okdir and -execdir might need gnu-find to be installed, which is typically used on Linux.
How about ./your/path/command.sh
?
-
No, I actually meant something like the following:
execindirectory -d /var/log "cp *.log ~/Desktop"
. – Naftuli Kay May 26 '11 at 1:23 -
I'm perfectly aware that this isn't strictly necessary, by the way, I'm just interested in if it can actually be done. – Naftuli Kay May 26 '11 at 1:24
echo "#!/bin/bash; cd $1; exec $2" > /usr/local/bin/execindirectory; chmod +x /usr/local/bin/execindirectory
. Might need to put a little more effort if you want it to actually support option "tags" such as-d
and stuff. – LawrenceC May 26 '11 at 2:26