LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-15-2010, 08:32 PM   #1
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Rep: Reputation: 164Reputation: 164
Mass Renaming Files


I have a batch of files that have the following pattern:
[variable length name] # ABC.ABC
[variable length name] can be anything, that need to remain unchanged.

I need to mass rename all of these files to remove the " # ABC" portion.
I've been trying to work with perl expressions but guess I haven't gotten it right. Could use some help.
Here are some of the patterns I've tried:
# rename 's/ # ABC\.ABC$/\.ABC/ *.ABC'
# rename 's/. # ABC$/ /' *.ABC
... and a various other attempts.

Ideas / hints?
 
Old 03-15-2010, 08:34 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
not sure the exact syntax but you could try reading the file into an array, loping through the array and performing the rename operation on the array elements
 
Old 03-15-2010, 09:09 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Not sure I exactly understand what your real filenames are, but this may help you get it to work:

Code:
sasha@reactor: touch "blah #ABC"
sasha@reactor: ls
blah\ #ABC
sasha@reactor: touch "blaRG #ABC"
sasha@reactor: touch "blooper #ABC"
sasha@reactor: 
sasha@reactor: 
sasha@reactor: 
sasha@reactor: ls
blaRG\ #ABC  blah\ #ABC  blooper\ #ABC

sasha@reactor: find ./ -name "*#ABC"
./blooper #ABC
./blaRG #ABC
./blah #ABC

sasha@reactor: find ./ -name "*#ABC" | while read file; do                              
> mv "$file" "$(echo "$file" | sed 's/ #ABC//')"; done
`./blooper #ABC' -> `./blooper'
`./blaRG #ABC' -> `./blaRG'
`./blah #ABC' -> `./blah'
sasha@reactor: ls
blaRG  blah  blooper
Sasha
 
Old 03-15-2010, 09:18 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
PS - If you're using Bash4 (I don't know if 3.x can do it) you can do this without the sed, just by using shell variable substitution; I haven't played with that much yet (I'm stuck in my ways) so didn't suggest it; other members are more versed in the newer shell features and can give you better advice on that when they come around (or you can check out the bash man page).

Cheers
Sasha
 
Old 03-16-2010, 03:43 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
The 'rename' command may come in handy here, I think, but I still don't get the exact nature of the problem. Maybe a few examples of input and output would help.
 
Old 03-16-2010, 01:39 PM   #6
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
(I'm replying with my cell, so grammar may be problematic.)

I am using 'rename'. I cited examples in my first post, but here are more filename examples:
My file # 123.ABC
Your file # 123.ABC
Note the ' # 123' is what I need to remove. I need to keep the .ABC at the end.
 
Old 03-16-2010, 02:04 PM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Ok, try:

Code:
rename "#ABC" "" *
or

Code:
rename "#123" "" *
that's it.
 
Old 03-16-2010, 04:12 PM   #8
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
H_TexMex_H ...
I hate you for very willingly pointing out my obvious stupidity.
I love you because you helped me past my obvious stupidity.

It worked like a champ!

And some creds for GrapefruitGirl, too! I've not done too much scripting, so you reminded me of what I can do with the bash shell.
 
Old 03-16-2010, 04:19 PM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
At least you got what you needed accomplished by whichever means.

Honestly, I should look into simpler means of doing things sometimes -- but what the heck, the more ways we have to choose from, the better!

Best regards,
Sasha

P.S> there was another thread within the last few weeks, where someone was trying to use `rename` and it became apparent that `rename` may or may not exist on some machines, and may or may not be the SAME `rename` as it is on other machines. Just something to keep in mind, in case it one day doesn't work on some machine you happen to be using.

Last edited by GrapefruiTgirl; 03-16-2010 at 04:21 PM.
 
Old 03-16-2010, 10:47 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
If you have Python 2.4++, you can try this script. example usage

Code:
$ ls -1
i want file anem here # ABC.DEF
[variable length name] # ABC.ABC

$  filerenamer.py -p "(.*)\s+\#\s+ABC\.(.*)$" -e "\1.\2" -l "*#*"
==>>>>  [ /home/[variable length name] # ABC.ABC ]==>[ /home/[variable length name].ABC ]
==>>>>  [ /home/i want file anem here # ABC.DEF ]==>[ /home/i want file anem here.DEF ]
remove the "-l" option to commit changes
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
mass renaming in order KBriggs Linux - Newbie 5 06-17-2009 12:02 PM
Mass renaming? yanik Linux - General 6 02-16-2009 03:25 PM
mass renaming script frieza Programming 25 12-10-2007 03:56 AM
mass renaming icons for use with XFE? Mr_Shameless Linux - Software 2 06-02-2007 05:55 AM
a question about renaming files in mass! zeltak Linux - Software 1 05-24-2006 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration