复制 bash 目录后,在新目录中编辑文件名?

After copying bash directory, edit filenames in new directoy?

我目前正在解决 bash shell 中的一个问题,将一个目录复制到一个新目录,当您检查该新目录时,它会更改文件名它复制了。不确定如何准确表达它,所以我会展示它应该输出的内容:

-------$ ls -a myproject/
 ./ ../ file1 file2.c file3.txt .this_is_a_hidden_file
 -------$ bkup myproject mybackup
 -------$ ls -a mybackup
 ./ ../ file1-BACKUP file2-BACKUP.c file3-BACKUP.txt

所以我在脚本中的第二个参数 bkup 复制了第一个参数中的内容,然后编辑参数 #1 的文件名以在主干和主干之间添加“-BACKUP”扩展名(例如在 file2.c 之间使 file2-BACKUP.c

我已经知道如何使用 cp -a sourceDir./ destinationDir 复制目录,但我该如何编辑 destinationDir 中的文件名?任何帮助表示赞赏!谢谢。

使用 rename 命令使用正则表达式重命名多个文件,试试下面的方法

# change directory to the destination folder
$ cd destinationDir

# rename all files 
# -n flag will only display how files will be renamed 
# once satisfied then remove the -n flag from below command and it will actually rename all files
$ rename -n -v 's/(.+?)(\.[^.]*$|$)/-BACKUP/' *

您可以参数化命令并将其包含在脚本中 (bkup) 我不在我的 Linux 盒子前..所以请测试,如果您发现任何错误请告诉我。