Reputation: 3512
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
Upvotes: 2
Views: 396
Reputation: 2363
I don't know zmv
but try this script:
while read file
do
prefix=$(echo $file | sed 's|\(.*\)/.*$|\1|');
parent=$(echo $file | sed 's|.*/\([^/]\+\)/[^/]\+$|\1|');
child=$(echo $file | sed 's|.*/\([^.]\+\).*$|\1|');
ext=$(echo $file | sed 's|.*/[^.]\+\.\(.\+\)$|\1|');
mv "$prefix/$child.$ext" "$prefix/$parent.$ext"
done < <(find -type f)
EDIT: The first version doesn't work if the files or directories contains spaces.
Upvotes: 0
Reputation: 780899
I'm not really familiar with zmv
, but I guess this will do it:
zmv '(*)/*.(*)' '$1/$1.$2'
Upvotes: 1