So I am supposed to find all the files in the given directory with surffix of .C or cc, and change their name to .C.blah or .cc.blah

Is there a command to do that?

Or should I implement this with the command find?

link|improve this question

0% accept rate
What have you tried already (assuming this is homework)? – soandos Jan 18 at 5:01
I have actually sorted this out by using the find command, by implementing the -execdir option and mv command, but I still need to play around with it. – Anson Jan 18 at 5:24
So it will be great if someone can still answer this question with an full answer – Anson Jan 18 at 5:31
After all the Wiking and googling, I now have the answer to my own question =] I can use find to execute command on the files returned by find directly. with the -exec option so it will be find . -type f ( -name ".C" -o -name ".cc" ) -exec mv {} {}.blah\;} – Anson Jan 18 at 6:19
feedback

4 Answers

There are a few approaches, depending on what is available on your *nix.

There two different rename commands around, one provided through perl, and one as part of standard utils.

Perl version:

rename 's/(\.cc$|\.C$)/$1.blah/' *.cc *.C

utils version - you might be able to do this with one line:

rename .C .C.blah *.C
rename .cc .cc.blah *.cc

for loop:

for i in *.C *.cc ; do mv $i $i.blah ; done
link|improve this answer
feedback

use a simple for-loop:

for file in *.txt; do echo $file $file.blah; done
link|improve this answer
feedback

I know you already found your own acceptable solution, but I want to post what I would normally use at work. This syntax works for GNU findutils, which is part of any modern linux distro.

## xargs is much faster than -exec, xargs can run parallel on # cores with -P #
find ./ -type f -name '*.C' -o -name '*.cc' | xargs -I '{}' mv '{}' '{}'.BAK

##  If I'm specifying more than two extensions, I would generally use a regex
find ./ -type f -regex ".*\.\(C\|cpp\)$" | xargs -I '{}' mv '{}' '{}'.BAK

Especially with the now common quad-core SMP servers, you can usually half running time with -p 4. Even with only one core, xargs avoids the extraneous forking find ...-exec would initiate. With a desktop, and unlimited time, it's not really a problem. On the other hand using server resources, and working with many thousands of files, avoiding unnecessary overhead is a priority.

link|improve this answer
feedback

There are two different rename commands.

On Ubuntu/Debian/... distros:

rename -v 's/$/.blah/' *.C *.cc

On RedHat/CentOS/... distros:

rename -v $ .blah *.C *.cc
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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