shell - Rename File Based on Portion of Directory Name using zsh -


i rename file in directory based upon portion of directory name. have following structure:

sample_1234/all_r2.gz sample_2345/all_r2.gz sample_3456/all_r2.gz 

in each directory create new file concatenation of multiple other files. name new concated file using number portion of directory name. example,

sample_1234/1234_r2.gz sample_2345/2345_r2.gz sample_3456/3456_r2.gz 

i can return portion of directory name want with:

pwd | grep -o '[0-9][0-9][0-9][0-9]' 

but unable new file name using zmv. suggestions in zsh shell? thanks,

don't use find, use zmv. use ( ) pairs grab bits of filename want, , ${1}, ${2} etc reference them. here's folder structure:

$ ls **/* sample_1234/all_r2.gz  sample_4567/all_r2.gz sample_2345/all_r2.gz  sample_6890/all_r2.gz  sample_1234: all_r2.gz  sample_2345: all_r2.gz  sample_4567: all_r2.gz  sample_6890: all_r2.gz 

and here's zmv, assuming you've got loaded:

$ zmv -n 'sample_(*)/all_r2.gz' 'sample_${1}/${1}_r2.gz' mv -- sample_1234/all_r2.gz sample_1234/1234_r2.gz mv -- sample_2345/all_r2.gz sample_2345/2345_r2.gz mv -- sample_4567/all_r2.gz sample_4567/4567_r2.gz mv -- sample_6890/all_r2.gz sample_6890/6890_r2.gz 

it grabs between _ , / in filename from current directory, allows reference number string @ end of directory name. insert filename using ${1} reference, replacing all.

it's worth using zmv -n see happen before it.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -