Search code examples
renamezshbatch-rename

replacing filenames with parent folder name without changing file endings


Im struggling replacing filenames with parent folder name and conserving the different file endings.

Example;

.sp1/4287/4287/iho.cst
.sp1/4287/4287/iho.dbf
.sp1/4287/4287/iho.prj
.sp1/4287/4287/iho.shp
.sp1/4287/4287/iho.shx

renamed to;

.sp1/4287/4287/4287.cst
.sp1/4287/4287/4287.dbf
        .
        .
        .

Im currently trying out zsh shell using zmv.

zmv '(*)/*' '$1/$1' 

But this is matching all. I don't get how to escape the file endings (if possible). Also tried rename but without success.

Since I have multiple sp folders (sp2, sp3, ..spN) and since each e.g. sp1/ contain a lot of subfolders like 4287 with the same type of files, Im seeking a batch solution.

Any pointers would be appreciated.

UPDATE

This works when in spN/XXXX/;

zmv '(*)/*.(*)' '$1/$1.$2';

How can I write a loop going through the spN/XXXX/ folders and executing the above zmv code?

#!/usr/bin/env zsh
source ~/.zshrc

for i in ./*;
  for y in i;
    do
      zmv '(*)/*.(*)' '$1/$1.$2';
    done

Solution

  • I'm not really familiar with zmv, but I guess this will do it:

    zmv '(*)/*.(*)' '$1/$1.$2'