Skip to content
Advertisement

How do I copy files in the same directory with a number changed of filename

I would like to copy mutiple files using patterns for a new file name.

For example:

./happycat1_sm.png
./happydog1.png
./happyrat1.png

How do I copy these files into the following filename automatically using bash-script?

./happycat2_sm.png
./happydog2.png
./happyrat2.png

Many thanks

Advertisement

Answer

for file in $(find . -maxdepth 1 -type f); do cp "${file}" $(echo "${file}" | sed 's/1/2/'); done

This copies all files in the current directory, replacing 1 with 2. It’s not really useful as it serve a very specific purpose and is not very reusable, but maybe it’s what you need.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement