LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-07-2005, 07:18 AM   #1
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Rep: Reputation: 45
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 =)
 
Old 07-07-2005, 07:23 AM   #2
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
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
 
Old 07-07-2005, 07:34 AM   #3
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Thanks. I'll give it a try in a short while, and keep the thread updated.
 
Old 07-07-2005, 07:58 AM   #4
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
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
 
Old 07-07-2005, 09:08 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
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.
 
Old 07-07-2005, 03:10 PM   #6
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
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
 
Old 07-07-2005, 03:24 PM   #7
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
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 :/
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash help, renaming file extensions trey85stang Linux - General 8 07-21-2005 04:51 PM
bash help renaming files kahn Programming 6 06-16-2005 07:15 AM
How do I make a bash sheet for renaming files? Cyberian Linux - Newbie 16 03-16-2005 08:08 AM
renaming batch of files linux_ub Linux - Newbie 6 10-27-2004 09:41 PM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 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