Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
@jasonwryan The first part is the same, but that one doesn't say anything about how to string-replace in a filename, so it probably makes sense to leave them separate – Michael Mrozek Oct 8 '11 at 23:05
@Michael You are right: I had conflated it with another question – jasonwryan Oct 8 '11 at 23:07
up vote 3 down vote accepted

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

share|improve this answer

In zsh:

autoload zmv
zmv '(**/)(*).cc' '$1$2.cpp'

In bash ≥4:

for x in **/*.cc; do mv "$x" "${x%.cc}.cpp"; done

In any shell:

find -name '*.cc' -exec sh -c 'for x; do mv "$x" "${x%.cc}.cpp"; done' _ {} +

Simpler, and portable to older systems, but slightly slower:

find -name '*.cc' -exec sh -c 'mv "$0" "${0%.cc}.cpp"' {} \;

See this answer for explanations.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.