如何在不更改当前目录的情况下在文件夹中运行命令?

Tim*_*nov 31 command-line bash scripts

您可能觉得奇怪,但我想在特定文件夹中运行命令而不更改 shell 中的当前文件夹。示例 - 这是我通常做的:

~$ cd .folder
~/.folder$ command --key
~/.folder$ cd ..
~$ another_command --key
Run Code Online (Sandbox Code Playgroud)

虽然我想要这样的东西:

~$ .folder command --key
~$ another_command --key
Run Code Online (Sandbox Code Playgroud)

是否可以?

Flo*_*sch 68

如果你想避免第二个cd你可以使用

(cd .folder && command --key)
another_command --key
Run Code Online (Sandbox Code Playgroud)

  • 神奇的括号!这是如何运作的?+1 (2认同)

Rad*_*anu 11

没有cd......甚至一次都没有。我找到了两种方法:

# Save where you are and cd to other dir
pushd .folder
command --key
# Get back where you were at the beginning.
popd
another_command --key
Run Code Online (Sandbox Code Playgroud)

第二:

find . -maxdepth 1 -type d -name ".folder" -execdir command --key \;
another_command --key
Run Code Online (Sandbox Code Playgroud)