I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).
I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.
I have all of my music stored in the following structure
"/home/Music/artist name/" where "artist name" is the name of the artist. For example, there is a directory called "/home/Music/ACDC/" with all of my ACDC.
I have a directory of picture files for all of the artists in my music library i.e "/home/artists/" and all of the files have the extension .jpg. Each artist has one file that is named exactly as the directory in my "/home/Music/" directory. That is, I have a file called ACDC.jpg to match "/home/Music/ACDC/"
Now, what I want to do is the following: move each of the picture files in "/home/artists/" to the matching directory in /home/Music/.
So it should move "/home/artists/ACDC.jpg" to "/home/Music/ACDC/". Maybe this requires regular expressions of some kind, I don't know.
THEN:
After the files are moved, I want ALL of the newly moved files stored in each of the artist directories in "/home/Music/" to be renamed to "folder.jpg".
Thus "/home/Music/ACDC/ACDC.jpg" would become "/home/Music/ACDC/folder.jpg"
Is this possible? I really hope it is, because it would take me days, if not weeks, to manually copy and paste all of the files and then rename them one by one.
I've tested this and it worked fine for me on your setup
I hope this helps solve your problem, please test it before running on the actual folders.
#!/bin/bash
for image in `ls -t /home/artists/`
do
ARTISTNAME=$(echo $image | sed 's/.jpg//g')
MOVARTIST="/home/artists/$image"
MOVDESTINATION="/home/Music/$ARTISTNAME/folder.jpg"
# moving the file
/bin/mv $MOVARTIST $MOVDESTINATION;
done
I haven't made any comparison as to, if the image doesn't exist or if the folder of the artist doesn't exist, this is assuming you have all ARTISTS.jpg and the folders ARTISTS also created.
# do the following for each jpg file ( hopefully execute this from artists directory, else give path to find command ).
for i in `find . -type f -name '*.jpg'`
do
# extract the folder name
foldername=`basename $i .jpg`;
# move the file as you specified. ( give correct path in the following. )
# mv $i /home/music/$foldername/folder.jpg;
done
Give correct path in all places ( find & mv ).
Use it at your own risk, kindly test & use.
Uncomment the 'mv' command.
marcozd, you are welcome, great to be of help. And yes programming is always different in written, there are thousands of ways of coding the same program, but the logics are always the same.
And, yes momo.reina, but the for loop give us a benefit of using a variable that has the filename as a value, so that we can manipulate it to do whatever we want with each found result. Everything inside the for loop will be done for the first line of the find, then the next loop comes and the variable will be replaced with the next line from find, etc.
Hope that is clear.
Maybe this is not the place to post it, but I don't know where else to ask...
I would really like to understand exactly how scripts work (in terms of the the syntax etc.) I don't know where to start. I would like to ultimately be able to understand why this script works and how.
Can someone recommend either a website or book (like "scripting for dummies") that I could go to?
marcozd, I read Unix Shell Programming while ago, but I've learned the most online and by trying myself. Plus, this forum has been of great help so far on practicing simple scripting, by doing for others.