cp命令中是否有选项将源路径反映为目标路径?

时间:2014-09-30 05:46:10

标签: shell

如果源路径和目标路径相同,如何跳过重新键入? 例如,我需要备份文件或重命名它:

# taking back-up
$ cp ~/project/uboot/u-boot.img ~/project/uboot/ver1-u-boot.img

# renaming it
$ mv ~/project/uboot/u-boot.img ~/project/uboot/ver1-u-boot.img

2 个答案:

答案 0 :(得分:2)

关于花括号的全部内容!

$ cp ~/project/uboot/{,ver1-}u-boot.img

或者更详细

$ mv ~/project/uboot/{u-boot.img,ver1-u-boot.img}

shell会重现您在问题中明确写出的内容,这意味着您可以写出完整路径一次。 Here是进一步阅读的良好链接。

答案 1 :(得分:1)

怎么样

( cd ~/project/u-name-it; cp_or_mv file ver1-file )

base=base-path cp_or_mv $base/file $base/ver1-file

然而,对于我来说,这非常类似于脚本或函数的用例。 因为这些当然可以变得非常复杂(例如增加版本会很好)我​​更愿意给出一个简单的例子作为激励;)

# Untested, test well before using; should work in zsh & bash IFIAC
# usage:
#   bup directory [files...]
function bup
{
     local dir=$1
     shift
     for file; do
       cp $dir/$file $dir/ver1-$file
     done
}

HTH 罗伯特

相关问题