LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rename recursively (https://www.linuxquestions.org/questions/linux-newbie-8/rename-recursively-809602/)

unihiekka 05-23-2010 07:31 AM

Rename recursively
 
How could I rename (lots of) files (recursively from the pwd) from *.jpg to cover.jpg? I have many different cover art files for my mp3s, but I want to rename them all to cover.jpg (in each folder). Thanks!

dmafcoi 05-23-2010 07:53 AM

Quote:

Originally Posted by unihiekka (Post 3978321)
How could I rename (lots of) files (recursively from the pwd) from *.jpg to cover.jpg? I have many different cover art files for my mp3s, but I want to rename them all to cover.jpg (in each folder). Thanks!

Hi unihiekka: This LQ post should help.

druuna 05-23-2010 07:57 AM

Hi,

for THISFILE in `find /mp3/base/dir/ -type f -name "*.jpg"`; do echo ${THISFILE} ${THISFILE%/*}"/cover.jpg"; done

This looks for jpg files in /mp3/base/dir/ (substitute with your mp3 base directory) and renames them to cover.jpg

If there are more the one jpg files in the same directory all will be renamed to cover.jpg and you will end up with 1 jpg file!! I do assume that all jpg's are in seperate dirs (one jpg per album).

I also assume that you have a base directory for mp3's. Something like this (names may differ):
/multimedia/mp3
/multimedia/mp3/Bauhaus
/multimedia/mp3/Japan


If a test run succeeds, change echo to mv.

Hope this helps.

druuna 05-23-2010 08:00 AM

Hi,

Quote:

Originally Posted by dmafcoi (Post 3978335)
Hi unihiekka: This LQ post should help.

Not in this case, although some of the techniques used are somewhat similar.

The thread you refer to changes JPG to jpg, which is easier then changing xxxx.jpg to cover.jpg.

dmafcoi 05-23-2010 08:14 AM

Quote:

Originally Posted by druuna (Post 3978343)
Hi,
Not in this case...

That's why I said 'should help' not 'should solve it for you' :)

Pleasant day,
Bob

unihiekka 05-23-2010 12:33 PM

Quote:

for THISFILE in `find /mp3/base/dir/ -type f -name "*.jpg"`; do echo ${THISFILE} ${THISFILE%/*}"/cover.jpg"; done
Thanks for that. Another question. Somehow there are some files that are hidden, so they are of the form ".*.jpg". How would I go about changing that line to accommodate for that difference (I know that at least I should change the "*.jpg" to ".*.jpg", but then I would like the files to become normal, not hidden. That alone won't do, because then it cannot stat the files...) Any help much appreciated.

druuna 05-23-2010 12:54 PM

Hi,

Did you try my command? It should pick up on the dot files as well:
Code:

/tmp/Temp $ ls -lAR
.:
total 16
drwxr-x--- 2 druuna internet 4096 May 23 19:52 a
drwxr-x--- 2 druuna internet 4096 May 23 19:52 b
drwxr-x--- 2 druuna internet 4096 May 23 19:52 c
drwxr-x--- 2 druuna internet 4096 May 23 19:52 d

./a:
total 0
-rw-r----- 1 druuna internet 0 May 23 19:46 .aaa.jpg

./b:
total 0
-rw-r----- 1 druuna internet 0 May 23 19:47 .bbb.jpg

./c:
total 0
-rw-r----- 1 druuna internet 0 May 23 19:47 zzz.jpg

./d:
total 0
-rw-r----- 1 druuna internet 0 May 23 19:52 zzz.jpg
druuna /tmp/Temp $

/tmp/Temp $ for THISFILE in `find /tmp/Temp/ -type f -name "*.jpg"`; do echo ${THISFILE} ${THISFILE%/*}"/cover.jpg"; done
/tmp/Temp/c/zzz.jpg /tmp/Temp/c/cover.jpg
/tmp/Temp/b/.bbb.jpg /tmp/Temp/b/cover.jpg
/tmp/Temp/d/zzz.jpg /tmp/Temp/d/cover.jpg
/tmp/Temp/a/.aaa.jpg /tmp/Temp/a/cover.jpg
druuna /tmp/Temp $

Bold lines are the dot files.

Hope this helps.

crts 05-23-2010 07:59 PM

Quote:

Originally Posted by unihiekka (Post 3978551)
Thanks for that. Another question. Somehow there are some files that are hidden, so they are of the form ".*.jpg". How would I go about changing that line to accommodate for that difference (I know that at least I should change the "*.jpg" to ".*.jpg", but then I would like the files to become normal, not hidden. That alone won't do, because then it cannot stat the files...) Any help much appreciated.

Hi,

try
Code:

shopt -s dotglob
and then run the command in the same shell again. Should now also match files that start with a dot.

unihiekka 05-26-2010 01:46 AM

Yes, druuna, you are right. I'm sorry. I did not see the hidden files in the list. There is however a catch. When I use echo, no problem. When I replace it with mv, it says cannot stat, probably because there are spaces in the folder names. Can that be an issue?

Tinkster 05-26-2010 01:54 AM

Quote:

Originally Posted by unihiekka (Post 3981413)
Yes, druuna, you are right. I'm sorry. I did not see the hidden files in the list. There is however a catch. When I use echo, no problem. When I replace it with mv, it says cannot stat, probably because there are spaces in the folder names. Can that be an issue?

Absolutely. You need to quote the names with spaces in them.
Code:

for THISFILE in `find /tmp/Temp/ -type f -name "*.jpg"`; do echo "${THISFILE}" "${THISFILE%/*}/cover.jpg"; done
... that *should* work (untested).

druuna 05-26-2010 01:55 AM

Hi,

A possible solution for the spaces would be to change this:

echo ${THISFILE} ${THISFILE%/*}"/cover.jpg"

into:

echo "${THISFILE}" "${THISFILE%/*}/cover.jpg"

Hope this helps.

Beaten by The Tinkster ;)


All times are GMT -5. The time now is 02:07 AM.