Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename directories with spaces in the name
#1

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

Reply
#2
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?
Reply
#3
save it as a text file like rename.pl for example, chmod +x rename.pl, ./rename.pl
Reply
#4

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

 

morbondu

Reply
#5

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)

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)