Linux-Noob Forums

Full Version: Rename directories with spaces in the name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Need to rename some directories with spaces in the names? Here is a little perl script to do just that. You will need to edit the $startpath.

 

// Begin Script

#!/usr/bin/perl

use File::Find;

 

$startpath="/path/to/dir";

do {

$flag=0;

find sub {

my $foo=$File::Find::name;

if (-d $foo && $foo=~/ /) {

($foo2=$foo)=~s/ /_/g;

rename($foo, $foo2);

$flag=1;

}

},$startpath;

} until ($flag==0);

exit;

 

// End Script

sounds like something i could use since i recently converted from windows. what do i do, just paste it into a text file? and then read the text file?
save it as a text file like rename.pl for example, chmod +x rename.pl, ./rename.pl

i swear i posted something in here.. about saving, chmod, and execution for this perl scirpt... hrmmm

 

morbondu


Heres a bash solution albeit uglier:

 



Code:
#!/bin/sh
mv "$1" `echo "$1" | sed 's/ //g'`




 

Put that in a file (say rename.sh) and use like this:

 



Code:
rename.sh "file or dir with spaces in it"




 

(Note use Tab to autocomplete the filename and it'll add the " at the end)