1

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:

file1
file2
file3
ps.dir

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

file1.bad
file2.bad
file3.bad
ps.dir.bad

3 Answers 3

4

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"); }
1
  • oh yeah. find. duh. way better than my ideas. Commented Jan 8, 2016 at 20:32
2

Assuming no spaces in the file names:

for file in *; do mv "${file}" "${file}.bad"; done
1
  • 2
    This will work with spaces, It even works with newlines. Commented Jan 8, 2016 at 19:56
1
[ ! . -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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.