Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
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.
|
 |
07-07-2005, 07:18 AM
|
#1
|
Senior Member
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288
Rep:
|
Batch Renaming in bash
Hi all, i'm having trouble with renaming files.. i have about 700 of them i need to rename =/ But in 2 parts..
1) There are about 150 files all in 1 folder named
Yannis & Joulia 001.jpg 002 003 004..........
I want to get rid of the & sign.. So its just
Yannis Julia 001.jpg 002 003 .........
2) There are 4 folders with pics that have random names such as
dscn0001.jpg
dscn0002.jpg
How can i just make a loop to rename the pics as
001.jpg
002.jpg
003.jpg
and so on?
And can i tell it to start from another number than 000 ? So when i move to the second folder, i can start from, say 100.jpg, 101.jpg.. etc...
I'm not much of a bash programmer, so i have no idea on how i can do this, although i do know enough that it's probably 1 line of code.. hehe
Thanks alot =)
|
|
|
07-07-2005, 07:23 AM
|
#2
|
Member
Registered: Apr 2004
Location: Edinburgh
Posts: 78
Rep:
|
Hi,
you should try a:
for i in *.jpg
do
mainPart=`echo "$i" | cut -f 1-2 -d '\&'`
mv "$i" "$mainPart"
done
mainPart contains the name of your picture. The cut function allows to do some 'curtting' and you get the field 1 and 2 (-f 1-2) after cutting around the delimiter & (-d '\&').
It should work but maybe the spaces are going to be an issue. Pay attention to that. For the other, you can use the same trick by tuning the cutting part.
PY
|
|
|
07-07-2005, 07:34 AM
|
#3
|
Senior Member
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288
Original Poster
Rep:
|
Thanks. I'll give it a try in a short while, and keep the thread updated.
|
|
|
07-07-2005, 07:58 AM
|
#4
|
Senior Member
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288
Original Poster
Rep:
|
Hmm, i'm getting this error message
Code:
cut: the delimiter must be a single character
Try `cut --help' for more information.
mv: cannot move `Yannis & Julia 136_jpg.jpg' to `': No such file or directory
Edit: As for the second part, i'm not sure how this would solve it unless i ad some $var += 1; somewhere
|
|
|
07-07-2005, 09:08 AM
|
#5
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
For the 1) I would do :
Code:
find /path/to/dir -name "*.jpg" | while read file; do
new=$(echo $file | tr -d '&')
[ ! -f "$new" ] && mv "$file" "$new"
done
The 2) would not be too different :
Code:
find /path/to/dir -name "*.jpg" | while read file; do
new=$(echo $file | sed -e 's/dscn//')
[ ! -f "$new" ] && mv "$file" "$new"
done
You could use the 2) givin the path to the parent dir
of your 4 jpeg's dir if they are in the same dir
[edit]
If you want 001.jpg become 101.jpg, 002.jpg become 102.jpg...
Code:
find /path/to/dir -name "*.jpg" | while read file; do
new=$(basename $file)
dir=$(dirname $file)
new=$(echo $new | sed -e 's/dscn//')
new=$(echo $new | cut -d'.' -f1)
let "new+=100"
new="$dir/$new.jpg"
[ ! -f "$new" ] && mv "$file" "$new"
done
Last edited by keefaz; 07-07-2005 at 09:17 AM.
|
|
|
07-07-2005, 03:10 PM
|
#6
|
Member
Registered: Apr 2004
Location: Edinburgh
Posts: 78
Rep:
|
 corrections:
mainPart=`echo "$i" | cut -f 1 -d '&'``echo "$i" | cut -f 2 -d '&'`d
Because with the previous syntax:
- & remained
- the delimiter was wrond '&' is sufficient.
Sorry about that 
|
|
|
07-07-2005, 03:24 PM
|
#7
|
Senior Member
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288
Original Poster
Rep:
|
Thanks. The scripts did the work. And i can move on
They all still look confusing to me, so i don't see myself remembering any of this for a next time :/
|
|
|
All times are GMT -5. The time now is 04:23 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
|
|