Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] find & mv

On Sun, Nov 04, 2001 at 09:27:46AM +1100, steven@xxxxxxxxxxxxxx wrote:

> Morning Sluggers
> I am having trouble with something simple which I am sure is easy.  I just
> want to rename all the files in a directory from say their existing name to
> p-existingname.  e.g.  mv p1.fmt pp1.fmt but for all the files in a
> directory.

Try the 'rename' command, which will hopefully come bundled with
your distribution.  (I think it comes standard with Perl 5.5
upward or something.)  For your example:

    rename 's/p/pp/' *.fmt

If you're not familiar with Perl's regular expressions, basically
what you need to do with rename is use 's/old_text/new_text' as
the first argument to the command.

rename will replace only the first occurance of that text in the
filename, so if you have a file called 'p1p.fmt', it'll get
renamed to 'pp1p.fmt', and not 'pp1pp.fmt'.  One of the most
common uses you'll see rename being used for is

    rename 'tr/A-Z/a-z/' *

which transliterates (tr) all upper case characters (A-Z) to
lowercase characters (a-z).

If you have a whole bunch of files you want to rename, post them
to the slug list and I'm sure many regex gurus can help you out

#ozone/algorithm <ozone@xxxxxxxxxxxxxxxx>          - trust.in.love.to.save