I am trying to determine a perl method to add an extension to a list of files. I can already rename a list of files with a certain extension to some other extension, but I need to be able to do this for any file name, whether or not it already has an extension.

For example, I have a list of files such as:


And I want to rename them ALL to filename.extension:


At the shell, if you've got the Perl rename installed (sometimes called prename):

rename -v 's/$/.bad/' *

If you have too many files for the shell * glob to handle them all you can mix'n'match with find like this (also replace + with \; if necessary):

find . -maxdepth 1 -exec rename -v 's/$/.bad/' {} +

For Perl, just use move from a standard module:

use File::Copy;
for (<*>) { move($_, "$_.bad"); }
  • oh yeah. find. duh. way better than my ideas. – mikeserv Jan 8 '16 at 20:32

Assuming no spaces in the file names:

for file in *; do mv "${file}" "${file}.bad"; done
  • 2
    This will work with spaces, It even works with newlines. – chaos Jan 8 '16 at 19:56
[ ! . -sf / ] &&
mkdir ../bad  &&
pax  -rwls/$/.bad/ . ../bad

That will create a hardlink mirror of . in ../bad. Every file rooted in . will afterward also be found in ../bad but by a name with the appendix .bad. If the new tree satisfies, remove the current tree and mv the ../bad tree over it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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