Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved] bash recursively renaming any jpg to cover.jpg
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
jasn
Guru
Guru


Joined: 05 May 2005
Posts: 439
Location: Maryland, US

PostPosted: Tue Aug 13, 2013 6:32 am    Post subject: [Solved] bash recursively renaming any jpg to cover.jpg Reply with quote

I'm trying to walk the tree of my music files and rename any jpg file in place to cover.jpg. I've tried;
Code:
find . -name '*.jpg' | xargs -I {} mv {} cover.jpg

on my test subdirs which contain;
Code:
1/1.jpg
2/2.jpg
3/3.jpg
4/4.jpg

and the only thing I managed to do was to move each jpg file to my current upper level directory, and change the name to cover.jpg. (overwriting it 3 times of course)
Code:
1/
2/
3/
4/
cover.jpg


Thanks..


Last edited by jasn on Tue Aug 13, 2013 7:52 pm; edited 1 time in total
Back to top
View user's profile Send private message
erikm
l33t
l33t


Joined: 08 Feb 2005
Posts: 634

PostPosted: Tue Aug 13, 2013 7:21 am    Post subject: Reply with quote

Try
Code:
user@host ~ $ for i in $(find . -name '*jpg'); do mv $i ${i%\/*.jpg}/cover.jpg;done
Back to top
View user's profile Send private message
Naib
Watchman
Watchman


Joined: 21 May 2004
Posts: 6051
Location: Removed by Neddy

PostPosted: Tue Aug 13, 2013 8:35 am    Post subject: Reply with quote

find . -name '*.jpg' -exec mv {} 'cover.jpg' \;
_________________
Quote:
Removed by Chiitoo
Back to top
View user's profile Send private message
jasn
Guru
Guru


Joined: 05 May 2005
Posts: 439
Location: Maryland, US

PostPosted: Tue Aug 13, 2013 2:39 pm    Post subject: Reply with quote

First off, thank you both for providing me with this noobish help. I read what must have been 20 "linux rename recursively.." webpages last night, and I couldn't figure out the syntax in order to do what I wanted. erikm, your assistance got me a littler closer. Your one liner achieved what I wanted when I ran it against my test subdirectory. Before;
Code:
1/1.jpg
2/2.jpg
3/3.jpg
4/4.jpg

Code:
for i in $(find . -name '*jpg'); do mv $i ${i%\/*.jpg}/cover.jpg;done

After;
Code:
1/cover.jpg
2/cover.jpg
3/cover.jpg
4/cover.jpg

but it fails when the jpg filename had spaces in the original filename. Before;
Code:
1/1 1.jpg
2/2 2.jpg
3/3 3.jpg
4/4 4.jpg

Code:
for i in $(find . -name '*jpg'); do mv $i ${i%\/*.jpg}/cover.jpg;done
mv: cannot stat './4/4': No such file or directory
mv: cannot stat '4.jpg': No such file or directory
mv: cannot stat './2/2': No such file or directory
mv: cannot stat '2.jpg': No such file or directory
mv: cannot stat './1/1': No such file or directory
mv: cannot stat '1.jpg': No such file or directory
mv: cannot stat './3/3': No such file or directory
mv: cannot stat '3.jpg': No such file or directory

After;
Code:
1/1 1.jpg
2/2 2.jpg
3/3 3.jpg
4/4 4.jpg

Naib, your one liner produced the same results as my previous one. Moving all jpgs to the upper level directory and renaming them to cover.jpg. Before;
Code:
1/1.jpg
2/2.jpg
3/3.jpg
4/4.jpg

Code:
find . -name '*.jpg' -exec mv {} 'cover.jpg' \;

After;
Code:
1/
2/
3/
4/
cover.jpg

It might just be a matter of a misplaced bracket or parentheses, but I'm still stuck trying to sort out the last few bits..
Back to top
View user's profile Send private message
jasn
Guru
Guru


Joined: 05 May 2005
Posts: 439
Location: Maryland, US

PostPosted: Tue Aug 13, 2013 7:47 pm    Post subject: Reply with quote

I tried consulting several more howto webpages but still can't do what I want. The thing is I can find the files in my test subdirectory structure;
Code:
find . -iname "*.jpg"
./4/4 4.jpg
./2/2 2.jpg
./1/1 1.jpg
./3/3 3.jpg
./1 1.jpg

and I can use the rename command to rename files with whitespaces in them by doing;
Code:
rename -v *.jpg cover.jpg *.jpg
`1 1.jpg' -> `cover.jpg'

I just can't combine the two to sweep through the subdirs and rename all jpg files to cover.jpg together;
Code:
find . -iname "*.jpg"
./4/4 4.jpg
./cover.jpg
./2/2 2.jpg
./1/1 1.jpg
./3/3 3.jpg

Any one see what I'm obviously missing?

Thanks..
Back to top
View user's profile Send private message
Atom2
Apprentice
Apprentice


Joined: 01 Aug 2011
Posts: 185

PostPosted: Tue Aug 13, 2013 7:48 pm    Post subject: Reply with quote

Why don't you just use
Code:
find . -name '*.jpg' -execdir mv {} cover.jpg \;


Provided there's only one .jpg file in every subdirectory, that should solve your problem - unless I'm missing something.

Regards,

Atom2
Back to top
View user's profile Send private message
jasn
Guru
Guru


Joined: 05 May 2005
Posts: 439
Location: Maryland, US

PostPosted: Tue Aug 13, 2013 7:51 pm    Post subject: Reply with quote

Atom2,

Thank you. That did it. I can stop pulling my hair out..

Jason
Back to top
View user's profile Send private message
Atom2
Apprentice
Apprentice


Joined: 01 Aug 2011
Posts: 185

PostPosted: Tue Aug 13, 2013 7:53 pm    Post subject: Reply with quote

jasn wrote:
I can stop pulling my hair out..

I'm sure your barber will be glad to hear about that ...

Regards Atom2
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum