[Gllug] Finding filenames with special characters (was Mass renaming of files)

Richard Jones rich at annexia.org
Sun May 11 08:37:34 UTC 2003


On Sun, May 11, 2003 at 07:48:49AM +0100, Simon Morris wrote:
> * " | ?

Essentially the problem here is with quoting. I think you'll find
it's hard to write a simple script which copes with these cases.
However, here is a not-so-simple script which will work:

----------------------------------------------------------------------
#!/usr/bin/perl -w

use strict;

my $root = $ARGV[0];
warn "root = $root\n";

sub visit
  {
    my $fn = shift;
    my $dir = shift;

    warn "visiting $dir\n";
    opendir DIR, $dir or die "visit: $dir: $!";
    my @files = readdir DIR;
    closedir DIR;

    foreach (@files) {
      next if /^\.{1,2}$/;
      $_ = &$fn ($dir, $_);
      visit ($fn, "$dir/$_") if -d "$dir/$_";
    }
  }

visit (sub {
	 my $dir = shift;
	 local $_ = shift;
	 my $old = "$dir/$_";
	 tr/:/_/;
	 rename $old, "$dir/$_" or warn "rename: $old, $dir/$_: $!";
	 $_
       }, $root);
----------------------------------------------------------------------

Run it as ./rename.pl TOPDIR

> There are also some directorys and files with no names! How am I
> supposed to find them???

With no names ... this sounds unlikely. Valid Unix filenames have to
be at least one character long. Do a 'ls -lb' of the offending
directory and post it here.

> It would probably be better to stop the OS allowing daft filenames like
> these. Is that possible?

Are these mounted by the clients using Samba or appleshare? If so then
there might be a way to configure or patch these tools so that they
refuse to create files with unusual names.

Rich.

-- 
Richard Jones, Red Hat Inc. (London) and Merjis Ltd. http://www.merjis.com/
http://www.annexia.org/ Freshmeat projects: http://freshmeat.net/users/rwmj
MONOLITH is an advanced framework for writing web applications in C, easier
than using Perl & Java, much faster and smaller, reusable widget-based arch,
database-backed, discussion, chat, calendaring:
http://www.annexia.org/freeware/monolith/


-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list