Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
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.
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Rep:
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!
Click here to see the post LQ members have rated as the most helpful post in this thread.
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!
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
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Original Poster
Rep:
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.
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 $
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.
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Original Poster
Rep:
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.