How to rename with prefix/suffix?
In Bash and zsh you can do this with Brace Expansion. This simply expands a list of items in braces. For example:
# echo {vanilla,chocolate,strawberry}-ice-cream
vanilla-ice-cream chocolate-ice-cream strawberry-ice-cream
So you can do your rename as follows:
mv {,new.}original.filename
as this expands to:
mv original.filename new.original.filename
I've seen people mention a rename
command, but it is not routinely available on Unix systems (as opposed to Linux systems, say, or Cygwin - on both of which, rename is an executable rather than a script). That version of rename
has a fairly limited functionality:
rename from to file ...
It replaces the from part of the file names with the to, and the example given in the man page is:
rename foo foo0 foo? foo??
This renames foo1 to foo01, and foo10 to foo010, etc.
I use a Perl script called rename
, which I originally dug out from the first edition Camel book, circa 1992, and then extended, to rename files.
#!/bin/perl -w
#
# @(#)$Id: rename.pl,v 1.7 2008/02/16 07:53:08 jleffler Exp $
#
# Rename files using a Perl substitute or transliterate command
use strict;
use Getopt::Std;
my(%opts);
my($usage) = "Usage: $0 [-fnxV] perlexpr [filenames]\n";
my($force) = 0;
my($noexc) = 0;
my($trace) = 0;
die $usage unless getopts('fnxV', \%opts);
if ($opts{V})
{
printf "%s\n", q'RENAME Version $Revision: 1.7 $ ($Date: 2008/02/16 07:53:08 $)';
exit 0;
}
$force = 1 if ($opts{f});
$noexc = 1 if ($opts{n});
$trace = 1 if ($opts{x});
my($op) = shift;
die $usage unless defined $op;
if (!@ARGV) {
@ARGV = <STDIN>;
chop(@ARGV);
}
for (@ARGV)
{
if (-e $_ || -l $_)
{
my($was) = $_;
eval $op;
die $@ if $@;
next if ($was eq $_);
if ($force == 0 && -f $_)
{
print STDERR "rename failed: $was - $_ exists\n";
}
else
{
print "+ $was --> $_\n" if $trace;
print STDERR "rename failed: $was - $!\n"
unless ($noexc || rename($was, $_));
}
}
else
{
print STDERR "$_ - $!\n";
}
}
This allows you to write any Perl substitute or transliterate command to map file names. In the specific example requested, you'd use:
rename 's/^/new./' original.filename
You could use the rename(1)
command:
rename 's/(.*)$/new.$1/' original.filename
Edit: If rename
isn't available and you have to rename more than one file, shell scripting can really be short and simple for this. For example, to rename all *.jpg
to prefix_*.jpg
in the current directory:
for filename in *.jpg; do mv "$filename" "prefix_${filename}"; done;
or also, leveraging from Dave Webb's answer and using brace expansion:
for filename in *.jpg; do mv {,prefix_}"$filename"; done;
You can achieve a unix compatible multiple file rename (using wildcards) by creating a for loop:
for file in *; do
mv $file new.${file%%}
done