移动文件并在后面留下软链接

我正在寻找一个单一的Linux命令,让我做到这一点相当于:

cp /some/path/file /another/path/ && ln -sf /another/path/file /some/path/ 

如果没有一个,那么为一堆文件做这个最好的方法是什么?

一个小问题是,你可以在两个时间使用命令不实际移动数据(假设两个path在同一个文件系统上)。

 ln /some/path/file /another/path/ && ln -sf /another/path/file /some/path/ 

但是我假设你想把/ some / path /的内容移动到另一个磁盘上,然后创build到新文件的链接,以便“没有人”注意到。

 for f in `ls /some/path/`; do ln /some/path/$f /another/path/ && ln -sf /another/path/$f /some/path; done 

用bash函数包装它:

 function cpln { for f in `ls $1` do ln $1/$f $2 && ln -sf $2/$f $1 done } 

我可以使用的脚本(需要两个参数/ some / path / file和/ another / path /):

 #!/bin/bash cp $1 $2 if [ "$?" -ne "0" ]; then echo "Some error" exit 1 fi ln -sf $2/${1##*/} ${1%/*} 

说真的,我认为这是一个非常简单的问题。

以下是我在Perl中可以做的事情:

 #!/bin/perl # Usage: cpln TARGETDIR SOURCE... # Example: find tree/ -type f | xargs cpln commands/ $target = shift; foreach(@ARGV) { m[(.*)/([^/]+)]; system("cp $_ $target"); system("ln -sf $target/$2 $1/"); } 

我希望有更优雅的东西,但我想我会用它。