Linux-Noob Forums
Rename directories with spaces in the name - Printable Version

+- Linux-Noob Forums (https://www.linux-noob.com/forums)
+-- Forum: Linux Noob (https://www.linux-noob.com/forums/forum-3.html)
+--- Forum: Tips and Tricks (https://www.linux-noob.com/forums/forum-59.html)
+---- Forum: Filesystem Management (https://www.linux-noob.com/forums/forum-26.html)
+---- Thread: Rename directories with spaces in the name (/thread-3778.html)



Rename directories with spaces in the name - grep420 - 2004-01-09


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




Rename directories with spaces in the name - tek-69 - 2004-01-09

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?



Rename directories with spaces in the name - grep420 - 2004-01-09

save it as a text file like rename.pl for example, chmod +x rename.pl, ./rename.pl



Rename directories with spaces in the name - morbondu - 2004-01-09


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

 

morbondu




Rename directories with spaces in the name - znx - 2005-03-21


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)