Nomoa.com

Paving the way for .NET in Tonga

OpenBSD

Categories
Main Menu
Google Ads
Mind Dumps Go Here + OpenBSD
Browse in : All > Soap Box
All > Soap Box > OpenBSD
Any of these categories - All of these categories

Unix: renaming multiple files at once

Soap Box
Posted by: Samiuela LV Taufa on July 05, 2007 5:00:40 PM

This is a common enough problem that when I came across the related discussion on the misc.openbsd mailing list I just had to summarise.

http://comments.gmane.org/gmane.os.openbsd.misc/125685

Problem:

In a given directory we have a number of photos that we would like to rename to something else. In our instance, the directory contains image files of the same naming convention, for example:

file0.jpg
file1.jpg
file2.jpg
...
...
filen.jpg

 

Solutions - Scripted:

Suggested by a few respondents. 

for f in *.jpg; do 
   mv $f ${f%.jpg}_thumb.jpg; 

 done

Suggested by Sebastian Benoit.

from the Perl Cookbook, Chap. 9.9.

#!/usr/bin/perl -w
# rename - Larry's filename fixer
$op = shift or die "Usage: rename expr [files]n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}

To do your rename:

file.jpg
file1.jpg
file_2.jpg

to:

file_thumb.jpg
file1_thumb.jpg
file_2_thumb.jpg

you could then do

rename 's/.jpg/_thumb.jpg/' file.jpg file1.jpg file_2.jpg

The first argument of this rename command can be any valid Perl expression

Solutions - Programs:

mmv [move/copy/append/link multiple files with wildcards]

Description
This is mmv, a program to move/copy/append/link multiple files according to a set of wildcard patterns. The wildcard matches can be reused in forming the target names. You can i.e. move all *.c.or? files to or?.new.*.c by saying

$ mmv "*.c.or?" or=2.new.=1.c

The multiple action is performed safely, i.e. without any unexpected deletion of files due to collisions of target names with existing filenames or with other target names. Furthermore, before doing anything, mmv attempts to detect any errors that would result from the entire set of actions specified and gives the user the choice of either aborting before beginning, or proceeding by avoiding the offending parts.

and thunar

Description
Thunar is a file manager for the Xfce Desktop environment. The interface is similar to Nautilus (the famous file manager for GNOME). Thunar has been designed to be fast and easy to use.

The scripts are nice and cool, although the Perl script definitely seems to just add complexity (although it does work ?)

There are no comments attached to this item.

Ratings
Options :
View Article Map
Log In to Contribute
View Archives