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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A script that will move a file to a directory with the same name and then rename that file
# 1  
Old 11-11-2009
Question 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).Smilie

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

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.
# 2  
Old 11-11-2009
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.

Code:
#!/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.
# 3  
Old 11-11-2009
Code:
# 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.
# 4  
Old 11-11-2009
Computer THANKS

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.Smilie
# 5  
Old 11-11-2009
why the for loops? the script will read the output of find line by line anyway right?
# 6  
Old 11-11-2009
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.
# 7  
Old 11-12-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename file in directory using contents within each file

In the below there are two generic .vcf files (genome.S1.vcf and genome.S2.vcf) in a directory. There wont always be two genaric files but I am trying to use bash to rename each of these generic files with specfic text (unique identifier) within in each .vcf. The text will always be different, but... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

Move file from one directory and update the list file once moved.

Dears, I have a listfile contains list of files path. i need to read the line of the listfile mv the file to other directory and update the listfile by deleting the lines of the listfile. #!/bin/bash target=/fstest/INVESTIG/Sadiq/TEST_ARCH while read -r line || ]; do mv $line... (19 Replies)
Discussion started by: sadique.manzar
19 Replies

3. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

4. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

5. Shell Programming and Scripting

How to rename (move) most recent files in directory?

I'm using cygwin32 on Windows. DN is an environment variable pointed at my download directory. This command works to move the single most recent file in my download directory to my current directory: mv "`perl -e '$p = $ARGV; opendir $h, $p or die "cannot opendir $p: $!"; @f = sort { -M $a... (2 Replies)
Discussion started by: siegfried
2 Replies

6. Shell Programming and Scripting

Move file in to directory- script

Hi In directory /mnt/upload I have about 100 000 files (*.png) that have been created during the last six months. Now I need to move them to right folders. eg: file created on 2014-10-10 move to directory /mnt/upload/20141010 file created on 2014-11-11 move to directory /mnt/upload/20141111... (6 Replies)
Discussion started by: primo102
6 Replies

7. UNIX for Dummies Questions & Answers

Rename files in a directory and move them

I have a directory e2e_ms_xfer/cent01 this contains the multiple files some of which will be named below with unique date time stamps e2e_ms_edd_nom_CCYYMMDD_HHMM.csv What I want to do is in a loop 1) Get the oldest file 2) Rename 3) Move it up one level from e2e_ms_xfer/cent01 to... (1 Reply)
Discussion started by: andymay
1 Replies

8. Shell Programming and Scripting

Script to move latest zip file to another directory

Hi folks, In my application there is a job running which create a .dat file along with it zip file also at unix box location /opt/app/cvf/temp1 so in temp1 directory I have one .dat file and its zip file also. Now since this job runs every day so if a job runs today there will be two files... (5 Replies)
Discussion started by: punpun66
5 Replies

9. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

10. Shell Programming and Scripting

Move a file from windows directory to unix directory

Move a file from windows directory to unix directory, is this possible? if it is, can someone help me on this? Thanks! God bless! (1 Reply)
Discussion started by: kingpeejay
1 Replies
Login or Register to Ask a Question