5
1
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?
5
1
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?
6
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
1
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.
1
use a simple for-loop:
for file in *.txt; do echo $file $file.blah; done
This command echoes the old file name and the new file to the terminal. If you're getting expected results, go for the real command:
for file in *.txt; do mv $file $file.blah; done
Anyway, this won't rename *.txt files in subdirectories. Use find for it.
0
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
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 – 2012-01-18T05:24:25.827
So it will be great if someone can still answer this question with an full answer – Anson – 2012-01-18T05:31:43.947
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