Search code examples
regexlinuxreplacerenamemkdir

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


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?


Solution

  • 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