Tux

...making Linux just a little more fun!

Talkback:131/lg_tips.html

Richard Neill [rn214 at hermes.cam.ac.uk]
Tue, 03 Oct 2006 00:56:29 +0100

Re file renaming (here, using "wavren", may I recommend installing the qmv and imv utilities. They are excellent. http://www.nongnu.org/renameutils/

imv filename
-> slightly faster than mv,

qmv
-> brings up an editor with columns for oldname, newname. Checks for errors.

Best wishes,

Richard


Top    Back


Benjamin A. Okopnik [ben at linuxgazette.net]
Wed, 4 Oct 2006 01:21:39 -0400

On Tue, Oct 03, 2006 at 12:56:29AM +0100, Richard Neill wrote:

> Re file renaming (here, using "wavren", may I recommend installing the 
> qmv and imv utilities. They are excellent.
> http://www.nongnu.org/renameutils/
> 
> imv filename
>    -> slightly faster than mv,
> 
> qmv
>   -> brings up an editor with columns for oldname, newname. Checks for 
> errors.

Good recommendations, Richard - thanks!

I'll add to that list, too: my own 'ren' Perl script (credit to Larry Wall for the original idea.) The script itself is short but provides huge amounts of flexibility and power by leveraging Perl's "regular expression" engine as well as Perl's 'tr' operator. You can get quite fancy with these:

# Lowercase all filenames in the current directory
ren 'tr/A-Z/a-z/' *

# Delete all commas, % signs, and semicolons in filenames starting with
# a digit
ren 'tr/,;%//d' [0-9]*

# Change all '.htm' extensions to '.html'
ren 's/$/l/' *.htm

# ROT13-encode all the filenames that end in 'png'
ren 'tr/a-zA-Z/n-za-mN-ZA-M/' *png

# Swap book title and author name (separated by a dash and spaces) and
# keeping the extension
ren 's/^(.*?)( - )(.*)(\.\w+)$/$3$2$1$4/' *

# Attach the filesize to the end of all filenames, skipping directories
# and symlinks
ren 's/$/"-".-s/e if -f' *

# Replace digits with their German word equivalents
ren 's/\d/(qw|Ein Zwei Drei Vier Funf Sechs Sieben Acht Neun|)[$&]." "/ge' *

# Capitalize the first letter in each separate word in the filename,
# ignoring the extension
ren 's/(?<!\.)\b./\u$&/g' *

The really tricky renaming jobs require a good understanding of regular expressions, but the simple ones are easy and obvious - and in some cases (e.g., renaming large batches of files), it's nearly irreplaceable.

* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Benjamin A. Okopnik [ben at linuxgazette.net]
Wed, 4 Oct 2006 01:31:27 -0400

On Wed, Oct 04, 2006 at 01:21:39AM -0400, Benjamin Okopnik wrote:

> 
> Good recommendations, Richard - thanks!
> 
> I'll add to that list, too: my own 'ren' Perl script (credit to Larry
> Wall for the original idea.)

Whoops - forgot the script itself. :)

#!/usr/bin/perl -w
# Created by Ben Okopnik on Fri Dec 2 18:59:31 EST 1994
$do = shift or die "Usage: ", $0 =~ /([^\/]+)$/, " 'perlexpr' <file[s]>\n";

for (@ARGV){ $is = $_; eval $do; die "$@\n" if $@; rename $is, $_ unless -f }

* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


John Karns [johnkarns at gmail.com]
Sun, 8 Oct 2006 15:51:17 -0500 (COT)

On Wed, 4 Oct 2006, Benjamin A. Okopnik wrote:

> On Wed, Oct 04, 2006 at 01:21:39AM -0400, Benjamin Okopnik wrote:
>>
>> Good recommendations, Richard - thanks!
>>
>> I'll add to that list, too: my own 'ren' Perl script (credit to Larry
>> Wall for the original idea.)
>
> Whoops - forgot the script itself. :)
>
> ```
> #!/usr/bin/perl -w
> # Created by Ben Okopnik on Fri Dec 2 18:59:31 EST 1994
> $do >
> for (@ARGV){ $is > '''

When dealing with multiple files, the mmv utility can also be very handy, and particularly when renaming a group of files having shared substring compenents. Added features: Does some error checking to avoid overwriting any existing files, is aliased from 'mcp' to copy files, 'mad' to append files, and 'mln' to handle links.

E.g.,

mmv "abc*.*" "xyz#2.#1"

would rename "abc.txt" to "xyztxt.". (The first ???*??? matched "", and the second matched "txt".)

SYNOPSIS
    mmv [-m|x|r|c|o|a|l|s] [-h] [-d|p] [-g|t] [-v|n] [--] [from to]

DESCRIPTION
    Mmv moves (or copies, appends, or links, as specified) each source file
    matching a from pattern to the target name specified by the to pattern.
    This  multiple  action is performed safely, i.e. without any unexpected
    deletion of files due to collisions of target names with existing file???
    names  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 proceeding
    by avoiding the offending parts or aborting.  mmv  does  support  large
    files  (LFS)  but  it does NOT support sparse files (i.e. it explodes
    them).

Top    Back


Benjamin A. Okopnik [ben at linuxgazette.net]
Sun, 8 Oct 2006 23:11:07 -0400

On Sun, Oct 08, 2006 at 03:51:17PM -0500, John Karns wrote:

> On Wed, 4 Oct 2006, Benjamin A. Okopnik wrote:
> 
> >On Wed, Oct 04, 2006 at 01:21:39AM -0400, Benjamin Okopnik wrote:
> >>
> >>Good recommendations, Richard - thanks!
> >>
> >>I'll add to that list, too: my own 'ren' Perl script (credit to Larry
> >>Wall for the original idea.)
> >
> >Whoops - forgot the script itself. :)
> >
> >#!/usr/bin/perl -w
> ># Created by Ben Okopnik on Fri Dec 2 18:59:31 EST 1994
> >$do = shift or die "Usage: ", $0 =~ /([^\/]+)$/, " 'perlexpr' <file[s]>\n";
> >
> >for (@ARGV){ $is = $_; eval $do; die "$@\n" if $@; rename $is, $_ unless 
> >-f }
> 
> When dealing with multiple files, the mmv utility can also be very handy,
> and particularly when renaming a group of files having shared substring
> compenents.  Added features: Does some error checking to avoid overwriting
> any existing files, is aliased from 'mcp' to copy files, 'mad' to append
> files, and 'mln' to handle links.

I didn't mention it, but 'ren' will not overwrite existing files either (that's the "unless -f" bit in the code.) However, I agree with you: the 'mmv' suite is quite nice - I've played around with it before.

* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Jason Creighton [jcreigh at gmail.com]
Wed, 4 Oct 2006 23:37:24 -0600

On Wed, Oct 04, 2006 at 01:21:39AM -0400, Benjamin A. Okopnik wrote:

> On Tue, Oct 03, 2006 at 12:56:29AM +0100, Richard Neill wrote:
> > Re file renaming (here, using "wavren", may I recommend installing the 
> > qmv and imv utilities. They are excellent.
> > http://www.nongnu.org/renameutils/
> > 
> > imv filename
> >    -> slightly faster than mv,
> > 
> > qmv
> >   -> brings up an editor with columns for oldname, newname. Checks for 
> > errors.
> 
> Good recommendations, Richard - thanks!

*nods* Good tip, I'll probably end up using it in the future, although the "destination only" format (qmv -f do) seems like it'll be easier to work with in vim than the dual-column format.

> I'll add to that list, too: my own 'ren' Perl script (credit to Larry
> Wall for the original idea.) The script itself is short but provides
> huge amounts of flexibility and power by leveraging Perl's "regular
> expression" engine as well as Perl's 'tr' operator. You can get quite
> fancy with these:

Why not just use the "rename" that ships with Perl? It seems to be installed as prename on Debian etch:

~$ ls -l `which rename`
lrwxrwxrwx 1 root root 24 2006-08-05 10:44 /usr/bin/rename -> /etc/alternatives/rename
~$ ls -l /etc/alternatives/rename
lrwxrwxrwx 1 root root 16 2006-08-13 20:43 /etc/alternatives/rename -> /usr/bin/prename
~$ mkdir /tmp/foobar
~$ cd /tmp/foobar/
/tmp/foobar$ for n in {1..10}; do touch $n; done
/tmp/foobar$ ls
1  10  2  3  4  5  6  7  8  9
/tmp/foobar$ rename 's/^/foobar/' *
/tmp/foobar$ ls
foobar1  foobar10  foobar2  foobar3  foobar4  foobar5  foobar6  foobar7
foobar8  foobar9
/tmp/foobar$ rename 'tr/oa/zq/' *
/tmp/foobar$ ls
fzzbqr1  fzzbqr10  fzzbqr2  fzzbqr3  fzzbqr4  fzzbqr5  fzzbqr6  fzzbqr7
fzzbqr8  fzzbqr9
/tmp/foobar$ rename '$_ = reverse' *
/tmp/foobar$ ls
01rqbzzf  1rqbzzf  2rqbzzf  3rqbzzf  4rqbzzf  5rqbzzf  6rqbzzf  7rqbzzf
8rqbzzf  9rqbzzf
/tmp/foobar$ 

Jason Creighton


Top    Back


Benjamin A. Okopnik [ben at linuxgazette.net]
Thu, 5 Oct 2006 08:15:13 -0400

On Wed, Oct 04, 2006 at 11:37:24PM -0600, Jason Creighton wrote:

> On Wed, Oct 04, 2006 at 01:21:39AM -0400, Benjamin A. Okopnik wrote:
> > On Tue, Oct 03, 2006 at 12:56:29AM +0100, Richard Neill wrote:
> > > Re file renaming (here, using "wavren", may I recommend installing the 
> > > qmv and imv utilities. They are excellent.
> > > http://www.nongnu.org/renameutils/
> > > 
> > > imv filename
> > >    -> slightly faster than mv,
> > > 
> > > qmv
> > >   -> brings up an editor with columns for oldname, newname. Checks for 
> > > errors.
> > 
> > Good recommendations, Richard - thanks!
> 
> nods Good tip, I'll probably end up using it in the future, although
> the "destination only" format (qmv -f do) seems like it'll be easier to
> work with in vim than the dual-column format.
> 
> > I'll add to that list, too: my own 'ren' Perl script (credit to Larry
> > Wall for the original idea.) The script itself is short but provides
> > huge amounts of flexibility and power by leveraging Perl's "regular
> > expression" engine as well as Perl's 'tr' operator. You can get quite
> > fancy with these:
> 
> Why not just use the "rename" that ships with Perl? It seems to be
> installed as prename on Debian etch:

[snip] I hadn't realized they started doing that. However, I still like my solution:

ben at Fenrir:~$ grep '^use' `which rename`
use strict;
use Getopt::Long;
ben at Fenrir:~$ wc -l `perldoc -l Getopt::Long`
1086 /usr/share/perl/5.8/Getopt/Long.pod

I prefer to load two lines of code instead of a thousand plus. :)

* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]
Fri, 6 Oct 2006 21:22:21 +0100

On Wed, 4 Oct 2006 23:37:24 -0600 Jason Creighton <jcreigh at gmail.com> wrote:

> Why not just use the "rename" that ships with Perl? It seems to be
> installed as prename on Debian etch:

It's installed with all the Debian derivates -- has been since Woody. Note that the rename command in this case is NOT the same as the one DeadRat and Fedora use, which is annoying since it means compatability is PITA.

> ``
> ~$ ls -l `which rename`
> lrwxrwxrwx 1 root root 24 2006-08-05 10:44 /usr/bin/rename
> -> /etc/alternatives/rename 
> ~$ ls -l /etc/alternatives/rename

So much typing when:

readlink -f "$(which rename)"

Does the same thing. ;) The reason why the alternatives system is used here is because the binary I mentioned for Redhat can also appear on Debian systems, hence the multiple-dependency hint.

-- Thomas Adam

-- 
"If I were a witch's hat, sitting on her head like a paraffin stove, I'd
fly away and be a bat." -- Incredible String Band.


Top    Back