使用正则表达式搜索并用目录创建替换 linux 上的重命名

Using regular expression search and replace rename on linux with directory creation

获取文件列表如:

/home/me/specialfiles/group01subgroup01.xyz
/home/me/specialfiles/group01subgroup01.abc
/home/me/specialfiles/group01subgroup01.nop
/home/me/specialfiles/group01subgroup02.xyz
/home/me/specialfiles/group01subgroup02.abc
/home/me/specialfiles/group01subgroup02.nop
/home/me/specialfiles/group01subgroup03.xyz
/home/me/specialfiles/group01subgroup03.abc
/home/me/specialfiles/group01subgroup03.nop
/home/me/specialfiles/group01subgroup04.xyz
/home/me/specialfiles/group01subgroup04.abc
/home/me/specialfiles/group01subgroup04.nop

我想重命名这些文件并添加一些类似于此的格式:

me@mymachine:~/specialfiles$ rename 's/group(\d\d)subgroup(\d\d)\.(xyz|abc|nop)/GroupSubGroup./' *

最后的文件列表看起来像

/home/me/specialfiles/Group01SubGroup01.xyz
/home/me/specialfiles/Group01SubGroup01.abc
/home/me/specialfiles/Group01SubGroup01.nop
/home/me/specialfiles/Group01SubGroup02.xyz
/home/me/specialfiles/Group01SubGroup02.abc
/home/me/specialfiles/Group01SubGroup02.nop
/home/me/specialfiles/Group01SubGroup03.xyz
/home/me/specialfiles/Group01SubGroup03.abc
/home/me/specialfiles/Group01SubGroup03.nop
/home/me/specialfiles/Group01SubGroup04.xyz
/home/me/specialfiles/Group01SubGroup04.abc
/home/me/specialfiles/Group01SubGroup04.nop

到目前为止,一切正常。下一步,我真正的问题是,如何在此过程中使用目录的子组名称为每个子组创建目录?

例如,我的第一个猜测是做这样的事情

me@mymachine:~/specialfiles$ rename 's/group(\d\d)subgroup(\d\d)\.(xyz|abc|nop)/GroupSubGroup\/GroupSubGroup./' *

这样做的目的是将文件重命名为如下所示

/home/me/specialfiles/Group01SubGroup01/Group01SubGroup01.xyz
/home/me/specialfiles/Group01SubGroup01/Group01SubGroup01.abc
/home/me/specialfiles/Group01SubGroup01/Group01SubGroup01.nop
/home/me/specialfiles/Group01SubGroup02/Group01SubGroup02.xyz
/home/me/specialfiles/Group01SubGroup02/Group01SubGroup02.abc
/home/me/specialfiles/Group01SubGroup02/Group01SubGroup02.nop
/home/me/specialfiles/Group01SubGroup03/Group01SubGroup03.xyz
/home/me/specialfiles/Group01SubGroup03/Group01SubGroup03.abc
/home/me/specialfiles/Group01SubGroup03/Group01SubGroup03.nop
/home/me/specialfiles/Group01SubGroup04/Group01SubGroup04.xyz
/home/me/specialfiles/Group01SubGroup04/Group01SubGroup04.abc
/home/me/specialfiles/Group01SubGroup04/Group01SubGroup04.nop

但是,我最终收到了

这样的消息
Can't rename Group01SubGroup01.xyz Group01SubGroup01/Group01SubGroup01.xyz: No such file or directory

看来问题是目录 Group01SubGroup01 需要已经存在才能成功完成。关于如何以某种方式使用正则表达式搜索和替换语法来创建目录(如果目录尚不存在)的任何想法?

cd /home/me/specialfiles/
find ./ -maxdepth 1 -name "Group*" | xargs -i basename {} | while read filename; do  _dir=${filename%.*}; mkdir -p $_dir; mv $filename $_dir;  done