how to rename a file with "!", "?", and other strange chars?

Aaron Peterson dopplecoder at gmail.com
Sat Sep 17 16:41:00 PDT 2005


On 9/17/05, Gary Kline <kline at tao.thought.org> wrote:
> 
>         I scarfed up a slew of php files that are around 100 bytes
>         in strlen and with "\ " and other non-shell-friendly bytes.
>         Is there a way to use perl to chop off the first N bytes?
> 
>         For example, a file many be named 00001\
>         00002xyz\?00003=Test.php.  What's the most logical way to
>         perl this file to "Test.php?

a script you run as:

% script.pl *

from the directory these files are in might look like:

foreach $old (@ARGV) {
  $new = $old;
  $new =~ s/\W//g;
  rename $old, $new;
}

That would remove all non "word" characters in the filename.  Perl
defines word characters as A-Z 0-9 and underscores.

or you could do something like this:

foreach $old (@ARGV) {
  $old =~ /(\w+\.php)/;
  rename $old, $1;
}

which catches any series of one or more word characters, a period, and
"php" in the variable $1.

There are lots of options, sounds like  you need a book on perl
maybe...  There is good online documentation here:

http://perldoc.perl.org/perl.html

Aaron


More information about the freebsd-questions mailing list