Shell – Replace a substring of filenames in a directory

renameshell

There exists a directory, which includes several levels of sub-directories. Under these directories, there are a set of files whose names include a common word, e.g, .cc. How can I replace the .cc in the names of these files with .cpp?

Best Answer

When you say the files "names include a common word," I am assuming that you are referring to the fact that they share the .cc extension: if so, using Gilles' answer here you could construct a command that would achieve your goal:

 find -type f -exec sh -c '
    for file; do [ "${file##*.}" = "cc" ] && 
    mv -- "$file" "${file%.cc}.ccp"; done
    ' -- {} +

See this answer on SO for more detail

Related Question