[conspire] Howto find replace change newline control character in filename script program perl

Hereon hereon1 at fastmail.us
Wed Dec 14 21:05:33 PST 2005

Howto find replace change newline control character in filename script program perl for linux unix

Thanks to Bill & Sam.

Useful for fixing .html files saved by Mozilla firefox.

I sometimes copy the title of an article and paste it to the save dialog box of Mozilla firefox.
Sometimes this contains a newline character that I don't notice.
K3b cd writing backup fails on the filenames containing newlines.

Place the script below in a text editor & save it in /usr/local/bin
as FindReplaceNewlineCharacterInFilename.pl
chmod 755 FindReplaceNewlineCharacterInFilename.pl
Go to the top of the directory tree you wish to search & replace.

Run the program

Running it with no options lists filnames which contain newline characters.
Run it with the "-r" option to actually change the file names.

#!/usr/bin/perl -w
# Usage: FindReplaceNewlineCharacterInFilename.pl [-r]
# Recursively finds all files and directories with newline characters in their name
# and optionally renames them to have spaces instead. By default, files are not renamed;
# the files that would be effected are listed on stdout. If invoked with the -r option,
# the files are renamed. 
use File::Find;           # Recursively visits every file in a list of directories
use Cwd;                  # for returning the current directory with getcwd().
use strict;
no warnings 'File::Find';

$::DO_RENAME = $ARGV[0] eq "-r";   # Is true if user wants to actually rename the file

my @dir;
push( @dir, getcwd() );   # Start in the current directory
find( \&RemoveUnsafeCharactersFromFilename , @dir );

# Replace all files with \n in them with spaces

sub RemoveUnsafeCharactersFromFilename {

    # If the filename (that Find::File::find() puts in $_ and in $File::Find::name)
    # contains a Newline character (\n, i.e. hex 0A ASCII 11 entered as ^V^J), 
    # replace it with a simple space character.

    # If the current filename contains the undesired control character,
    if( m/[\n]/ ) {  # m/pattern/ does a regex match of `pattern' against the $_ variable

    	# Create string for user display that shows "/r"'s in the file name.
        my $old_filename = $File::Find::name;
        my $old_display_name = $old_filename;   # show the old filename on a single line
        $old_display_name =~ s/[\n]/\\n/g;	# Put "\n" in the output to see.
        s/[\n]/ /g;  # s/old/new/ replaces `old' pattern with `new' value in the $_ variable

	# Show user the file name.
        print $::DO_RENAME ? "Renaming " : "";		# Say "Renaming" if '-r' option
        print "$old_display_name\n";		
	print $::DO_RENAME ? "To ===== $_\n" : "";	# Show new file name if renaming

        # We only rename files if the script is invoked with the -r option
        if( $::DO_RENAME ) {
            rename( $old_filename, $_ ) or die "Couldn't rename $old_display_name to $_: $!";
    1;	# Return 1 just in case.
  hereon1 at fastmail.us

http://www.fastmail.fm - Same, same, but different

More information about the conspire mailing list