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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
10-28-2007, 03:06 AM
|
#1
|
Member
Registered: Sep 2007
Location: Folsom, California
Distribution: Ubuntu, Mint, Debian, Suse
Posts: 307
Rep:
|
Renaming large number of files.
Hello ladies and gents,
I have a very large number (~500) of files I need to rename. I suppose I could write a perl script to do it... but I'm looking for something quick and dirty.
I need to rename files that look like this:
EM76540.pdf
to this:
76540EM.pdf
Can I use bash with mv or am I doing to have to write some perl script?
I wish you could just type mv EM*.pdf *EM.pdf
That would be awesome.
|
|
|
10-28-2007, 03:20 AM
|
#2
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
There is a rename command that can do bulk renames.
I will use variable expansion instead.
Code:
for file in EM*.pdf; do
tmp=${file#EM}
mv ${file} ${tmp%.pdf}EM.pdf
done
Or
Code:
for file in EM*pdf; do
mv $file ${file//[[:alpha:].]}EM.pdf
done
The latter deletes all alphabetic characters & the period from the variable.
|
|
|
10-28-2007, 03:59 AM
|
#3
|
Senior Member
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687
Rep:
|
I usually refer to this article for such tasks and can usually figure out how to come up with the right command through trial and error on some test subjects relatively quickly.
|
|
|
10-28-2007, 05:00 AM
|
#4
|
Member
Registered: Sep 2007
Location: Folsom, California
Distribution: Ubuntu, Mint, Debian, Suse
Posts: 307
Original Poster
Rep:
|
Thank you both!
jschiwal that simple script did exactly what I needed. I can see why you're a guru. I really appreciate it. For me to do that with a perl script would have taken me hours I'm sure.
I haven't had a chance to visit that site you mentioned Junior Hacker - it stated it was too busy. But it looks like something I will definitely look at later.
Man... I need to learn more scripting. That's a handy tool.
|
|
|
07-15-2008, 08:47 AM
|
#5
|
LQ Newbie
Registered: Jul 2005
Posts: 12
Rep:
|
Renaming in bulk, more complicated example
Can anyone provide some advice on a more complicated example to rename files...
I have files in the example format:
Babypicture_playing_with_spoon_date=20070602
Where the front of the file name is always a constant number of characters (here its 11 'Babypicture'), the middle is a descriptor, and the end is a constant number of characters for the date (14 characters, _date=20070602), with no end descriptor (.jpg is not there)
How can I rename these, using perl, to
Babypicture_date=20070602.jpg
Where I am cutting out the middle descriptor and adding the .jpg at the end?
Thanks
|
|
|
07-15-2008, 09:08 AM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
I don't know using Perl, but in Bash you can do something like
Code:
file="Babypicture_playing_with_spoon_date=20070602"
newfile=${file:0:11}${file:$((${#file}-14))}.jpg
basically using substring extraction and concatenation.
|
|
|
07-15-2008, 09:14 AM
|
#7
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
|
|
|
07-15-2008, 09:34 AM
|
#8
|
LQ Newbie
Registered: Jul 2005
Posts: 12
Rep:
|
Thanks Colucix.
Will try it, but hopefully someone can guide me on some perl code for this too. Seem like bash fails for me when the list of files is to long - I get the "argument list to long" error
|
|
|
07-15-2008, 11:09 AM
|
#9
|
LQ Newbie
Registered: Jul 2005
Posts: 12
Rep:
|
Perhaps a little more guidance...
I ran the commands on the command line and also in a file
#/bin/sh
file="Baby*"
newfile=${file:0:11}${file:$((${#file}-14))}.jpg
No file names are changed and there is no output of errors.
What am I doing wrong?
Thanks
|
|
|
07-15-2008, 11:12 AM
|
#10
|
LQ Guru
Registered: Jan 2001
Posts: 24,149
|
Quote:
Originally Posted by jmiter
Perhaps a little more guidance...
I ran the commands on the command line and also in a file
#/bin/sh
file="Baby*"
newfile=${file:0:11}${file:$((${#file}-14))}.jpg
No file names are changed and there is no output of errors.
What am I doing wrong?
Thanks
|
Umm.. well, you need more in your script than just:
Code:
file="Baby*"
newfile=${file:0:11}${file:$((${#file}-14))}.jpg
You need the mv or cp commands, however you're going to approach in renaming.
|
|
|
07-15-2008, 11:29 AM
|
#11
|
LQ Newbie
Registered: Jul 2005
Posts: 12
Rep:
|
yes, I actually had done:
mv file="Baby*" newfile=${file:0:11}${file:$((${#file}-14))}.jpg
but was returned an error:
mv: cannot stat `file=Baby*': No such file or directory
Recommendation on the placement of mv? Any usage with other file notation such as ()/\{} or others?
Thanks
|
|
|
07-15-2008, 11:43 AM
|
#12
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Of course. Mine was only a little guidance on how to build the new file name in BASH, based on your requirements. You have to embed it in a little script which retrives all the file names and rename them one at a time.
Note: at this point, since it looks like you are not experienced in shell programming, you have to do a backup of your pictures/files before trying to rename them!
A simple script could be
Code:
#!/bin/bash
for file in Baby*
do
newfile=${file:0:11}${file:$((${#file}-14))}.jpg
echo mv $file $newfile
done
This works by looping over the list of files whose name begin with "Baby" and assigns each file name to the variable "file", one at a time. Then build the new file name and finally perform the mv command to rename it. I intentionally put an echo in front of the mv command to let you verify what the script will do, before actually doing it: when you've verified that the mv commands are good, strip out the echo command and launch the script again.
This code works only 1) if the files are all in the working directory and 2) if file names don't contain blank spaces, otherwise you have to slightly modify the code to let it work properly.
|
|
|
07-15-2008, 11:46 AM
|
#13
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by jmiter
yes, I actually had done:
mv file="Baby*" newfile=${file:0:11}${file:$((${#file}-14))}.jpg
|
I strongly suggest to not try renaming/deleting/moving commands without knowing exactly what you're doing, that is without knowing the exact syntax! Unless you like living on the edge...
|
|
|
07-15-2008, 11:49 AM
|
#14
|
LQ Newbie
Registered: Jul 2005
Posts: 12
Rep:
|
Occasionally, I do attempt living on the edge . But in this case, I copied some sample files to a test directory in order to perfect the renaming command.
|
|
|
07-15-2008, 12:12 PM
|
#15
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
He he! Very good!
|
|
|
All times are GMT -5. The time now is 09:32 PM.
|
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
|
|