跟随移动的文件到其目标目录

Ger*_*rry 6 shell cd-command rename

有没有办法将文件从一个目录移动到另一个目录,然后在一个命令中 cd 进入后者?类似mv /a/file /another/dir && cd _但没有第二次输入目录路径的东西。

jas*_*yan 7

有一些函数可以移动和复制文件并将它们跟踪到源自Arch Linux 板上线程的目标目录:

# Follow copied and moved files to destination directory
cpf() { cp "$@" && goto "$_"; }
mvf() { mv "$@" && goto "$_"; }
goto() { [ -d "$1" ] && cd "$1" || cd "$(dirname "$1")"; }
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过发出以下命令来 {move,copy} 并跟踪文件:

mvf file /dest/dir/
Run Code Online (Sandbox Code Playgroud)


h3r*_*ler 5

在您的.bashrc

cpcd() { cp "$1" "$2" && cd "$2"; }
Run Code Online (Sandbox Code Playgroud)

那就是我会怎么做

然后你会cpcd像你一样使用cp

cpcd blah dir/
Run Code Online (Sandbox Code Playgroud)

它会将 blah 复制到 dir,如果成功,它会将目录更改为 dir/

  • @Gerry:“尽管”是什么意思-修改`.bashrc` 有什么问题?在大多数情况下,这就是你做这样的事情的方式。 (2认同)