使用Ubuntu 18.04。假设我们有一个名为debug.log的文件。您可以使用以下任一命令创建一个名为debug_BACKUP.log的副本:
cp debug.log debug_BACKUP.log
cp debug{,_BACKUP}.log或者,用cp替换mv来重命名文件。
现在假设我们有debug1.log和debug2.log。我们希望创建名为debug1_BACKUP.log和debug2_BACKUP.log的副本。是否有一个单一的命令来实现这一点?
当我尝试以下任一项时:
cp debug*.log debug*_BACKUP.log
cp debug*{,_BACKUP}.log错误是cp: target 'debug*_BACKUP.log' is not a directory。
发布于 2019-11-07 16:32:11
大括号展开是关于如何在glob展开之前重写命令的shell的说明。它们不会传递给命令本身-- cp不知道是否使用了大括号扩展。因此,cp甚至不知道是否使用通配符;当您运行cp *.txt dir/时,shell在运行它之前生成一个C字符串数组,该数组对应于cp foo.txt bar.txt baz.txt dir/。
这意味着,如果要在通配符扩展发生后重写内容,则需要手工完成。
for f in debug*.log; do
[[ $f = *_BACKUP.log ]] && continue # skip things that are already backup files
cp "$f" "${f%.log}_BACKUP.log"
done发布于 2019-11-07 16:37:57
很少有优秀的批量重命名程序,包括基于Perl的文件重命名。您可以通过以下三个步骤实现批量复制:
https://stackoverflow.com/questions/58753096
复制相似问题