LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-17-2008, 01:03 PM   #16
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140

If rename creates the name of a file which already exists then the file will be overwritten, so you can end up with fewer files than you started with.

The detox command line utility supplied with some linux distributions can recursively replace spaces with '_' and also remove other characters which might cause problems in file and folder names; it won't overwrite a file that already exists.
More details, and detox, can be obtained from http://detox.sourceforge.net/
 
Old 04-17-2008, 05:35 PM   #17
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ghostdog74 View Post
is rename a perl script? does it have the ability to do the sed portion, so that sed can be removed. all done using rename.
Code:
/usr/bin/rename: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
Not true about not needing sed. rename will only replace the first instance of space, not all of them.
ta0kira

Last edited by ta0kira; 04-17-2008 at 05:36 PM.
 
Old 04-17-2008, 05:39 PM   #18
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ararus View Post
This is completely redundant, rename translates patterns itself.
Code:
> touch "1 2 3"
> rename " " "_" "1 2 3"
> ls 1_*
1_2\ 3
ta0kira
 
Old 04-18-2008, 08:16 AM   #19
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
Quote:
Originally Posted by ta0kira View Post
Not true about not needing sed. rename will only replace the first instance of space, not all of them.
ta0kira
Bugger, I never noticed that before, my bad.

There is also the mmv command, though it's not standard.

A more efficient way might to be use a perl wrapper with xargs, e.g.:

find -type f -print0 | xargs -0 rmspace.pl

rmspace.pl:
Code:
#!/usr/bin/perl -w

for $ARGV (@ARGV) {
        $new = $ARGV;
        $new =~ s/ /_/g;
        rename $ARGV,$new;
}
(My Perl is pretty limited so this might no be the best way to do this...)
 
Old 04-19-2008, 11:18 AM   #20
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
You can also use the shell of course:

Code:
# foo="a b c"
# echo ${foo// /_}
a_b_c
 
Old 04-19-2008, 11:34 AM   #21
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
you don't have to rename all your folders subfolders etc.. just do
Code:
cp "folder name with spaces"
 
Old 04-19-2008, 12:48 PM   #22
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by ta0kira View Post
Code:
> touch "1 2 3"
> rename " " "_" "1 2 3"
> ls 1_*
1_2\ 3
ta0kira
Nope, still redundant:
Code:
telemachus ~/files $ touch "1 2 3" "4 5 6"
telemachus ~/files $ ls
1 2 3  4 5 6
telemachus ~/files $ rename 's/ /_/g' *
telemachus ~/files $ ls
1_2_3  4_5_6
Just use the 'g' option in the rename command. Edit - unless your rename and mine are different. (Mine is the Debian version, ie a Perl script.)

Last edited by Telemachos; 04-19-2008 at 12:51 PM. Reason: Clarification
 
Old 04-19-2008, 02:48 PM   #23
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Yeah, that doesn't work at all for me. Mine is a binary.
ta0kira
 
Old 04-19-2008, 06:23 PM   #24
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by ta0kira View Post
Yeah, that doesn't work at all for me. Mine is a binary.
ta0kira
Another reason to choose Debian then.
 
Old 04-20-2008, 12:17 AM   #25
babag
Member
 
Registered: Aug 2003
Posts: 365

Original Poster
Rep: Reputation: 30
thankyou to all who've responded here. this will take me
some time to sort through. been out of town the past few
days. frankly, i'm astonished at the amount of response
this question has produced.

thanks again,
BabaG
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle file names with spaces in them bahadur Programming 14 04-04-2005 12:04 PM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 11:43 AM
Spaces in folder/file names ~ termial Garoth Linux - Software 1 11-12-2004 01:56 PM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM
Bash crashes ? File names with () and spaces Danodare Slackware 1 02-27-2004 02:50 PM


All times are GMT -5. The time now is 02:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration