0

使用 Ubuntu 18.04。假设我们有一个名为debug.log. 您可以创建debug_BACKUP.log使用以下任一命令调用的副本:

cp debug.log debug_BACKUP.log
cp debug{,_BACKUP}.log

或者,替换cpmv重命名文件。

现在假设我们有debug1.logdebug2.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

4

2 回答 2

1

大括号扩展是关于如何在全局扩展发生之前重写命令的指令。它们不会传递给命令本身——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-07T16:32:11.907 回答
1

很少有优秀的批量重命名程序,包括基于 Perl 的文件重命名。您可以通过 3 个步骤实现批量复制:

  1. 将文件复制到 tmp 子文件夹
  2. 执行批量重命名,将文件移回当前文件夹
  3. 删除 tmp 文件夹
于 2019-11-07T16:37:57.100 回答