[CLUE-Tech] removing spaces from filenames?

Todd Williams todd at crashonline.org
Mon Nov 18 00:15:47 MST 2002


Dave Price wrote:
> Hi,
> 
> I have a _bunch_ of files that were created on a windoze system with
> embedded spaces in their names.  Can anyone recommend a technique to
> programmatically rename the files - replacing ' ' with '_' on both the
> file and dir names?
> 
> I am thinking perl, but I am not sure what sysntax I would need to use
> to search and rename.

a quick-n-ugly one liner:

ls -1 | perl -ne '/[ ]/ && (chomp($o=$_,$_), s/[ ]/_/g, rename $o,$_);'

You should try it first with a "print" in place of the "rename" to be 
sure you like the results...

Change the ls to an apropriate find to do directory trees.

One of many ways to do it using perl.

Please ask if any of that code is not clear.

Todd


hmmm...  maybe this would be a *little* clearer, or at least more 
verbose and "C"-like:

ls -1 | perl -ne 'if ($_ =~ /[ ]/) { chomp($o=$_); chomp($_); $_ =~ s/[ 
]/_/g; rename $o,$_; };'

see "man perlrun"  for a description of the -n switch, and part of the 
"magic"





More information about the clue-tech mailing list