Copy and rename sequence of files in bash.

Categories shell

Today I learned an useful substring replacement construct in bash. Copy & rename with sequence of files is common task in my work. Here’s a quick way:

maciek$ ls
b_gun1_anim0000.png	b_gun1_anim0002.png	b_gun1_anim0004.png	b_gun1_anim0006.png	b_gun1_anim0008.png	b_gun1_anim0010.png	b_gun1_anim0012.png
b_gun1_anim0001.png	b_gun1_anim0003.png	b_gun1_anim0005.png	b_gun1_anim0007.png	b_gun1_anim0009.png	b_gun1_anim0011.png
maciek$ for frame in b_*anim*.png; do cp $frame ${frame/b_/c_}; done;
maciek$ ls
b_gun1_anim0000.png	b_gun1_anim0003.png	b_gun1_anim0006.png	b_gun1_anim0009.png	b_gun1_anim0012.png	c_gun1_anim0002.png	c_gun1_anim0005.png	c_gun1_anim0008.png	c_gun1_anim0011.png
b_gun1_anim0001.png	b_gun1_anim0004.png	b_gun1_anim0007.png	b_gun1_anim0010.png	c_gun1_anim0000.png	c_gun1_anim0003.png	c_gun1_anim0006.png	c_gun1_anim0009.png	c_gun1_anim0012.png
b_gun1_anim0002.png	b_gun1_anim0005.png	b_gun1_anim0008.png	b_gun1_anim0011.png	c_gun1_anim0001.png	c_gun1_anim0004.png	c_gun1_anim0007.png	c_gun1_anim0010.png
maciek$