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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-18-2009, 07:17 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2005
Posts: 28
Rep:
|
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
|
|
|
|
03-18-2009, 07:36 AM
|
#2
|
|
Senior Member
Registered: Dec 2003
Location: Paris
Distribution: Slackware forever.
Posts: 2,178
Rep:
|
I use krename for such purpose.
http://www.krename.net/
|
|
|
|
03-18-2009, 07:44 AM
|
#3
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
03-18-2009, 07:58 AM
|
#4
|
|
Member
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252
Rep:
|
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!
|
|
|
|
03-18-2009, 08:19 AM
|
#5
|
|
Senior Member
Registered: May 2008
Posts: 2,839
|
Quote:
Originally Posted by openSauce
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.
|
|
|
|
03-18-2009, 08:35 AM
|
#6
|
|
Member
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252
Rep:
|
Haha, good point well-made. Yours also has the advantage of not processing files that have already been once through the grinder.
|
|
|
|
03-18-2009, 08:48 AM
|
#7
|
|
Senior Member
Registered: May 2008
Posts: 2,839
|
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.
|
|
|
|
03-18-2009, 09:56 AM
|
#8
|
|
LQ Newbie
Registered: Aug 2005
Posts: 28
Original Poster
Rep:
|
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
|
|
|
|
03-20-2009, 08:46 PM
|
#9
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by jlightner
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
|
|
|
|
03-21-2009, 07:18 PM
|
#10
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
03-22-2009, 04:28 AM
|
#11
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by jlightner
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.
|
|
|
|
03-25-2009, 04:37 AM
|
#12
|
|
LQ Newbie
Registered: Aug 2005
Posts: 28
Original Poster
Rep:
|
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
|
|
|
|
03-25-2009, 06:56 AM
|
#13
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,938
|
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 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:19 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|