如何使用Ubuntu“重命名”工具替换目录名和文件名中的字符串?

时间:2018-08-22 13:29:46

标签: linux ubuntu rename

当子目录和文件名包含相同的子字符串时,如何重命名父目录中的子目录和文件名中的子字符串?

特别是,我想在子目录和文件名中都将“ cam_0”替换为“ cam_1”。

示例:

parent:
--subdirectory_cam_0:
------ subdir_cam_0_image1.jpg
------ subdir_cam_0_image2.jpg
------ subdir_cam_0_image3.jpg

更改为:

parent:
--subdirectory_cam_1:
------ subdir_cam_1_image1.jpg
------ subdir_cam_1_image2.jpg
------ subdir_cam_1_image3.jpg

我使用了下一条命令:

find -iname "*cam_0*" | rename 's/(cam_0)/cam_1/'   

但是它仅更改子目录的名称,而不更改文件的名称。

它引发下一条消息:

./subdirectory_cam_0/subdir_cam_0_image1.jpg not renamed: ./subdirectory_cam_1/subdir_cam_0_image1.jpg already exists
./subdirectory_cam_0/subdir_cam_0_image3.jpg not renamed: ./subdirectory_cam_1/subdir_cam_0_image3.jpg already exists
./subdirectory_cam_0/subdir_cam_0_image2.jpg not renamed: ./subdirectory_cam_1/subdir_cam_0_image2.jpg already exists

1 个答案:

答案 0 :(得分:0)

just add g in the end of your rename matching pattern to rename all finded matches.

try this:

find -iname "*cam_0*" | rename 's/(cam_0)/cam_1/g'