移动文件并留下软链接

its*_*dok 10 linux command-line-interface symlink

我正在寻找一个允许我执行等效操作的 linux 命令:

cp /some/path/file /another/path/ && ln -sf /another/path/file /some/path/
Run Code Online (Sandbox Code Playgroud)

如果没有,那么对一堆文件执行此操作的最佳方法是什么?

Mr *_*ark 7

一个小说明,您可以两次使用 ln 使命令实际上不移动数据(假设两个路径都在同一个文件系统上)。

ln /some/path/file /another/path/ && ln -sf /another/path/file /some/path/
Run Code Online (Sandbox Code Playgroud)

但我假设您想将 /some/path/ 的内容移动到另一个磁盘,然后创建指向新文件的链接,以便“没有人”注意到。

for f in `ls /some/path/`; do ln /some/path/$f /another/path/ && ln -sf /another/path/$f /some/path; done
Run Code Online (Sandbox Code Playgroud)

将其包装在 bash 函数中:

function cpln {
    for f in `ls $1`
    do
        ln $1/$f $2 && ln -sf $2/$f $1
    done
}
Run Code Online (Sandbox Code Playgroud)