[conspire] How automatically replace control (newline) character with space in file & directory names

Bill Ward bill at wards.net
Sun Dec 4 11:10:58 PST 2005


#!/usr/bin/perl -w
use strict;
opendir DOT, "." or die "Can't read directory: #!\n";
foreach my $file (readdir DOT) {
    my $newfile = $file;
    $newfile =~ s/\n/ /g;
    next if $newfile eq $file;  # skip files that lack \n
    if (-e $newfile) {
         warn "Can't rename $file because $newfile already exists\n";
    }
    else {
        rename $file, $newfile or die "Error renaming $file: $!\n";
    }
}

On 12/4/05, Hereon <hereon1 at fastmail.us> wrote:
> Can someone tell me a way to accomplish this with a command, commands,
> shell script, or other program?
>
> I have some files & directories with names that contain the newline
> character, \n , 0A hex.
>
> [These get created sometimes as I copy & past text from web pages into
> the filename as I save a webpage, from firefox, to disk.]
>
> I wish to replace that newline with a space character, automatically,
> without doing it by hand for each file.
>
> I have figured out one way to locate the files:
>
> In the home directory do:
> ls -QR * | grep '\\n'
>
> Then, individually, I can do a locate on part of the file name to get a
> full path.
>
>
>
> ====
> $ ls -QR | grep '\\n' > FilesWithNewline.txt
>
> $ cat FilesWithNewline.txt
>
> . . . [Many file or directory names listed, like the following lines:]
>
> "An elevator to space NASA gives idea a lift\n12 teams vying for
> $100,000 in prizes in Mountain View article.cgi.html"
> "An elevator to space NASA gives idea a lift\n12 teams vying for
> $100,000 in prizes in Mountain View article.cgi_files"
> "./Engineering/Space/An elevator to space NASA gives idea a lift\n12
> teams vying for $100,000 in prizes in Mountain View article.cgi_files":
> "./Engineering/Space/An elevator to space NASA gives idea a lift\n12
> teams vying for $100,000 in prizes in Mountain View
> article.cgi_files/B1654090_data":
>
> . . .
>
> ====
>
> So, what's an easy way to search through the file tree & automatically
> replace the '\n' with a space character?
> Thanks.
> --
>   Hereon
>   hereon1 at fastmail.us
>
> --
> http://www.fastmail.fm - I mean, what is it about a decent email service?
>
>
> _______________________________________________
> conspire mailing list
> conspire at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/conspire
>



--
Help save the San Jose Earthquakes - http://www.soccersiliconvalley.com/




More information about the conspire mailing list