View previous topic :: View next topic |
Author |
Message |
jasn Guru
Joined: 05 May 2005 Posts: 439 Location: Maryland, US
|
Posted: Tue Aug 13, 2013 6:32 am Post subject: [Solved] bash recursively renaming any jpg to cover.jpg |
|
|
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 |
|
|
erikm l33t
Joined: 08 Feb 2005 Posts: 634
|
Posted: Tue Aug 13, 2013 7:21 am Post subject: |
|
|
Try Code: | user@host ~ $ for i in $(find . -name '*jpg'); do mv $i ${i%\/*.jpg}/cover.jpg;done |
|
|
Back to top |
|
|
Naib Watchman
Joined: 21 May 2004 Posts: 6051 Location: Removed by Neddy
|
Posted: Tue Aug 13, 2013 8:35 am Post subject: |
|
|
find . -name '*.jpg' -exec mv {} 'cover.jpg' \; _________________
Quote: | Removed by Chiitoo |
|
|
Back to top |
|
|
jasn Guru
Joined: 05 May 2005 Posts: 439 Location: Maryland, US
|
Posted: Tue Aug 13, 2013 2:39 pm Post subject: |
|
|
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 |
|
|
jasn Guru
Joined: 05 May 2005 Posts: 439 Location: Maryland, US
|
Posted: Tue Aug 13, 2013 7:47 pm Post subject: |
|
|
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 |
|
|
Atom2 Apprentice
Joined: 01 Aug 2011 Posts: 185
|
Posted: Tue Aug 13, 2013 7:48 pm Post subject: |
|
|
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 |
|
|
jasn Guru
Joined: 05 May 2005 Posts: 439 Location: Maryland, US
|
Posted: Tue Aug 13, 2013 7:51 pm Post subject: |
|
|
Atom2,
Thank you. That did it. I can stop pulling my hair out..
Jason |
|
Back to top |
|
|
Atom2 Apprentice
Joined: 01 Aug 2011 Posts: 185
|
Posted: Tue Aug 13, 2013 7:53 pm Post subject: |
|
|
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 |
|
|
|