Jump to content
Sign in to follow this  
grep420

Rename directories with spaces in the name

Recommended Posts

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

  • Like 1

Share this post


Link to post
Share on other sites

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?

  • Like 1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

 

morbondu

Share this post


Link to post
Share on other sites

Heres a bash solution albeit uglier:

 

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

 

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

 

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

 

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...