LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-18-2009, 07:17 AM   #1
ltodd2
LQ Newbie
 
Registered: Aug 2005
Posts: 28

Rep: Reputation: 0
rename multiple files with random extension


Afternoon

Im having a bit of a do trying to rename some files. We get 3 types of edi files in from the tax office but there system is adding a random 17 digit number to the end of the file which I need to remove. the files look like:
COMPANY-P6.mf.29550362467065025
COMPANY-P9.mf.29550362468597026
COMPANY-SL1.mf.29550362467066028

We need it to end in the .mf and remove the numbers.
I've had a go at it and gave up. I've been sent a command using sed and it does what i want but its only in the view on screen and not actually changing the files.
ls | sed 's/\(.mf*\).*/.mf/'

any clues please

Lee
 
Old 03-18-2009, 07:36 AM   #2
Linux.tar.gz
Senior Member
 
Registered: Dec 2003
Location: Paris
Distribution: Slackware forever.
Posts: 2,227

Rep: Reputation: 86
I use krename for such purpose.
http://www.krename.net/
 
Old 03-18-2009, 07:44 AM   #3
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,009
Blog Entries: 5

Rep: Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787
You could do this with your sed as well but I prefer awk for quick and dirty:

Code:
for file in `ls COMPANY*`
do filenew=`echo $file |awk -F. '{print $1"."$2}'`
   mv $file $filenew
done
This is a loop that will make each file a variable called $file and then create a new variable $filenew based on the name of the file name. It then renames the original ($file) to the new name ($filenew).
The "awk -F." tells awk to use the dot as a delimiter. It then prints the 1st field (item before the first dot in file name), a literal dot (the awk strips out the dot so you have to add it back - notice the quotes around the dot) and the 2nd field which is the item after the first dot. Since we stop there it doesn't add the final dot or the random number.
 
Old 03-18-2009, 07:58 AM   #4
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
If it's always 17 digits plus a '.' you could also just do
Code:
mv $file ${file:0:$((${#file} - 18))}
That's untested but it looks about right. God, bash is ugly!
 
Old 03-18-2009, 08:19 AM   #5
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by openSauce View Post
If it's always 17 digits plus a '.' you could also just do
Code:
mv $file ${file:0:$((${#file} - 18))}
That's untested but it looks about right. God, bash is ugly!
That bash certainly is.


There's a number of ways to do it in bash, which one is best I guess depends how flexible your filename matching is going to be.

here's 2 more (there are most likely some others too)

Code:
mv "$file" "${file/.mf.*/.mf}"

mv "$file" "${file%%.mf.*}.mf"

Last edited by GazL; 03-18-2009 at 08:23 AM. Reason: changed filename/file for consistency with prior postings.
 
Old 03-18-2009, 08:35 AM   #6
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Haha, good point well-made. Yours also has the advantage of not processing files that have already been once through the grinder.
 
Old 03-18-2009, 08:48 AM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Just as an afterthought. There was someone doing something along these lines in a post a while back, It was a massive bulk rename and it had been running for days, literally! and was still only a fraction of the way through.

In the end someone suggested he use perl instead which turned out to be much better suited to doing this sort of thing. For a few hundred or so files, I guess bash will suffice, but if you're going into thousands then you probably should be looking at something like perl.
 
Old 03-18-2009, 09:56 AM   #8
ltodd2
LQ Newbie
 
Registered: Aug 2005
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlightner

[CODE
for file in `ls COMPANY*`
do filenew=`echo $file |awk -F. '{print $1"."$2}'`
mv $file $filenew
done[/CODE]

This is a loop that will make each file a variable called $file and then create a new variable $filenew based on the name of the file name. It then renames the original ($file) to the new name ($filenew).
The "awk -F." tells awk to use the dot as a delimiter. It then prints the 1st field (item before the first dot in file name), a literal dot (the awk strips out the dot so you have to add it back - notice the quotes around the dot) and the 2nd field which is the item after the first dot. Since we stop there it doesn't add the final dot or the random number.
This one work great. thanks for the help. Thanks eveyone else for yours
 
Old 03-20-2009, 08:46 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jlightner View Post
You could do this with your sed as well but I prefer awk for quick and dirty:

Code:
for file in `ls COMPANY*`
do filenew=`echo $file |awk -F. '{print $1"."$2}'`
   mv $file $filenew
done
be careful of using "ls" in for loop as it might break with spaces in filenames. Also, using "ls" like that is called "useless use of ls". No need to use "ls". Just use shell expansion.
Code:
for file in "COMPANY*"
do 
 mv "$file" "${file%.*}"
done
 
Old 03-21-2009, 07:18 PM   #10
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,009
Blog Entries: 5

Rep: Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787
ls COMPANY* isn't quite as "useless" as posting after the problem is resolved. I don't mind you making a point but you might be more careful of the way you phrase it.
 
Old 03-22-2009, 04:28 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jlightner View Post
ls COMPANY* isn't quite as "useless"
yes it is quite useless. How about this

Quote:
more careful of the way you phrase it.
I am not the inventor of the phrase. Many people have already seen and used it. this phrase comes out with google search too. Therefore i don't see anything wrong with the way i "phrase" it.
 
Old 03-25-2009, 04:37 AM   #12
ltodd2
LQ Newbie
 
Registered: Aug 2005
Posts: 28

Original Poster
Rep: Reputation: 0
Thanks all for the advise. Im not sure if I should add this next bit here or post new.
All the about works great but now the powers that be have said that the files may not be processed on a regular basis which would mean the next time the job runs and there are new files that have come in it will over write the ones it did last because the file names are now all the same. Is it possible that if it sees a file by the same name it can add a 1 to the name and then 2 etc. Once the files have been processed and moved the next can go in without a number since it will be the only file again.
im thinking this is too much but then my knowledge of scripting is very little.

thanks

Lee
 
Old 03-25-2009, 06:56 AM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Time to start reading: http://tldp.org/LDP/Bash-Beginners-Guide/html/ ...
The problem is people know/believe you can do this stuff now, so you might as well get a head start before they start asking for complex stuff
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script? how to rename files in random way? lefty.crupps Linux - Software 15 06-08-2014 03:47 PM
LXer: Rename multiple files to another extension in Linux LXer Syndicated Linux News 0 08-19-2007 03:10 PM
How to rename multiple files with the same extension rogueeve Linux - Newbie 3 05-18-2007 01:54 AM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 12:22 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 06:06 AM


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

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