Bash – Copy files in same folder and replace year in its name

bashcp

How can I copy a lot of files in the same directory replacing the year in its name?

I have this (can be different filename and extensions):

filename1-2014.ext
filename2-2014.ext
filename3-2014.ext
...
filenamen-2014.ext

And I want this:

filename1-2014.ext
filename1-2015.ext
filename2-2014.ext
filename2-2015.ext
filename3-2014.ext
filename3-2015.ext
...
filenamen-2014.ext
filenamen-2015.ext

I can make a script to do it, but I'm wondering is there is an easy way of doing it?

Best Answer

  • With find:

    find . -type f -name '*2014*' -exec bash -c 'cp "$0" "${0/2014/2015}"' {} \;
    
  • In the shell loop:

    for file in *2014*; do
        cp "$file" "${file/2014/2015}"
    done
    
Related Question