A script that will move a file to a directory with the same name and then rename that file

Hello all.

I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b:

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.:confused:

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.

Let me know if this helped.

# 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.

WOW!

They both work, although they look different!

I guess that is the nature of scripting, though - there is always more than one way to do it!

Again, many thanks.:smiley:

why the for loops? the script will read the output of find line by line anyway right?

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.

Take care.

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?

Thanks in advance.

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.

You can use this link here: Linux Shell Scripting Tutorial It explains the syntax and also give you great examples.

Hope this helps. If you have any programming questions, feel free to post them on a new topic under Shell Programming and Scripting

See you around.