Fedora Linux Support Community & Resources Center
  #1  
Old 23rd September 2008, 10:14 PM
amranny Offline
Registered User
 
Join Date: Sep 2008
Posts: 21
rename command with regular expression

Hi,

1. I tried to use rename with regular expression like this:
rename 's/r/k/' *
and it doesn't work,
I use :
GNU bash, version 3.2.33(1)-release (x86_64-redhat-linux-gnu)
on fc9

is it supported?

2. I'm trying to remove spaces from file names and none of this work :
find -type f -exec rename 'y/\ /\_/' {} \;

or

for i in *.mp3; do j=`echo $i|sed 's/ /_/g'`;mv $i $j; done


can anybody help?

thanks
Amir

Last edited by Seve; 23rd September 2008 at 10:50 PM.
Reply With Quote
  #2  
Old 23rd September 2008, 10:49 PM
Seve Offline
Retired Community Manager
 
Join Date: Oct 2004
Location: The GTA, Ontario, Canada
Age: 56
Posts: 12,371
( Moved to General Support )

and fixed your overly large fonts
__________________
Registered Linux User: #384977
.................................................. ............
See the Links below for more Help and those much wanted extras ... :)
Reply With Quote
  #3  
Old 23rd September 2008, 10:52 PM
jose.carmona Offline
Registered User
 
Join Date: Jul 2008
Posts: 5
1. rename don't support regexp.

2. try krename graphical tool. (http://www.krename.net/)
Reply With Quote
  #4  
Old 23rd September 2008, 11:07 PM
icydog Offline
Registered User
 
Join Date: Nov 2005
Posts: 451
Removing spaces from files is simple:
Code:
$ rename ' ' _ *
Rename only replaces the first occurrence, so you may have to run this a bunch of times until all the spaces are gone.

For regular expressions, Ubuntu (and probably others) come with a perl version of rename instead of the one that comes with Fedora. It is attached. Just rename it to prename.pl or something and chmod +x it. To change spaces to underscores, you can then run it like:
Code:
$ prename 's/ /_/g' *
Attached Files
File Type: txt prename.txt (2.9 KB, 532 views)
Reply With Quote
  #5  
Old 24th September 2008, 02:50 AM
GrapeShot Offline
Registered User
 
Join Date: Jul 2007
Location: Virginia
Posts: 252
Run this in the folder that has the files with spaces and it will remove the spaces from all spaces. If you want to replace it with an underscore or a dash, put the symbol you want to to use in between the two slashes just before the letter g (ie sed -e 's/ /_/g')

Code:
ls | grep -e " " | while read i; do newname=`echo -n "$i" | sed -e 's/ //g'`; echo `mv "$i" "$newname"`; done
Reply With Quote
  #6  
Old 24th September 2008, 11:08 AM
amranny Offline
Registered User
 
Join Date: Sep 2008
Posts: 21
Hi,
First thanks a lot.
I took this
rename "s/ *//g" *.mp3
from: http://www.cyberciti.biz/tips/renami...ll-prompt.html

How come it is not supported? does every system has its own variation?

Thanks again
Amir
Reply With Quote
  #7  
Old 25th September 2008, 06:40 AM
icydog Offline
Registered User
 
Join Date: Nov 2005
Posts: 451
Amir,

Please read my post. Also, to see what "rename" does in Fedora, do "man rename".

Thanks.
Reply With Quote
  #8  
Old 25th September 2008, 10:55 AM
sideways Offline
Retired User
 
Join Date: Oct 2004
Location: London, UK
Posts: 4,999
Nice use of rename icydog, simplest command line removal of filename spaces I've seen.

To automate it for large directories (so you don't have to keep re-executing it) you can loop it like this (in bash)

Code:
while [ $(ls | grep -c ' ') -gt 0 ]; do rename ' ' _ *; done
Reply With Quote
  #9  
Old 26th September 2008, 07:52 AM
ghostdog74 Offline
Registered User
 
Join Date: Sep 2006
Posts: 52
Quote:
Originally Posted by amranny
Hi,

1. I tried to use rename with regular expression like this:
rename 's/r/k/' *
and it doesn't work,
I use :
GNU bash, version 3.2.33(1)-release (x86_64-redhat-linux-gnu)
on fc9

is it supported?

2. I'm trying to remove spaces from file names and none of this work :
find -type f -exec rename 'y/\ /\_/' {} \;

or

for i in *.mp3; do j=`echo $i|sed 's/ /_/g'`;mv $i $j; done


can anybody help?

thanks
Amir

you can use the Python script here
example usage:
Code:
# ls -1 "test artis"*.mp3
test artist1.mp3
test artist2.mp3
test artist3.mp3

# filerenamer.py -p " " -e "" -l "test artis*.mp3" #use -l to list
==>>>>  [ /home/test artist2.mp3 ]==>[ /home/testartist2.mp3 ]
==>>>>  [ /home/test artist3.mp3 ]==>[ /home/testartist3.mp3 ]
==>>>>  [ /home/test artist1.mp3 ]==>[ /home/testartist1.mp3 ]

# filerenamer.py -p " " -e ""  "test artis*.mp3" #remove -l to commit
/home/test artist2.mp3  is renamed to  /home/testartist2.mp3
/home/test artist3.mp3  is renamed to  /home/test/testartist3.mp3
/home/test artist1.mp3  is renamed to  /home/testartist1.mp3

# ls -1 "test artis"*.mp3
/bin/ls: test artis*.mp3: No such file or directory

# ls -1 "testartis"*.mp3
testartist1.mp3
testartist2.mp3
testartist3.mp3
Reply With Quote
Reply

Tags
command, expression, regular, rename

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The problem of grep command, i want to know how the expression match the result xtcsin Programming & Packaging 1 10th September 2009 04:26 PM
for file in *.(need regular expression) (?) nix Programming & Packaging 4 2nd July 2009 03:10 AM
Does shell script support regular expression? youhaodeyi Using Fedora 8 26th November 2008 11:49 AM
regular expression '\$ ' gold Using Fedora 2 1st January 2008 04:06 AM
Simple regular expression not working pradovic Using Fedora 1 20th February 2005 04:37 AM


Current GMT-time: 23:46 (Thursday, 18-09-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat