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

时间:2022-08-05 10:39:16

Take a list of files such as:

获取文件列表,例如:

/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

I would like to rename these files and add some formatting similar to this:

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

me@mymachine:~/specialfiles$ rename 's/group(\d\d)subgroup(\d\d)\.(xyz|abc|nop)/Group$1SubGroup$2.$3/' *

This would end up with the list of files looking like

这最终会得到文件列表

/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

Up to here, this all works fine. The next step, and my real question is, how can I create a directory for each SubGroup in this process using the SubGroup name for the directory?

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

For example, my first guess was to do something like this

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

me@mymachine:~/specialfiles$ rename 's/group(\d\d)subgroup(\d\d)\.(xyz|abc|nop)/Group$1SubGroup$2\/Group$1SubGroup$2.$3/' *

The intent of this would be to rename the files to look something like this

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

/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

However, I end up with a message like

但是,我最终得到了一条消息

Can't rename Group01SubGroup01.xyz Group01SubGroup01/Group01SubGroup01.xyz: No such file or directory

It seems the problem is that the directory Group01SubGroup01 needs to already exist for this to complete successfully. Any ideas on how I can somehow use regular expression search and replace syntax to create directories if they don't already exist?

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

1 个解决方案

#1


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

#1


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