Moving files from several directories into parent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files from several directories into parent
# 1  
Old 05-28-2011
Moving files from several directories into parent

I am fairly new to bash(but am proficient in C++), and have only completed a few simple scripts. This is my first script that I actually need to do a serious task.

All of my audiobooks are stored in traditional MP3 format: Music/Artist/Album/*.mp3 (which in this case is Music/Author/Book/Disc/*
I need to move all of the mp3s into their respective Book directories, rename them to Book1.mp3, Book2.mp3, etc , and delete the Disc directories. I have figured out move the contents of a single Disc into it's parent and rename them correctly, and how to delete the directory.

My question is this: How do I parse through the different Disc directories? I am currently running the script from the Disc directory, but would like to run it from the Author directory. I'm thinking that I'm gonna have to use some nested for loops, but can't figure out how to make them work.

Here's is my crude idea:

Code:
let num=1

for Book in *; do
       for Disk in "$Book"/*; do

               for song in "$Book"/"$Disc"/*.mp3; do
                       mv "$song" "$Book"/$num"$Book".mp3
                        let num=$num+1
               done
               rmdir "$Book"/"$Disc"
       done
done

I think that my problem lies in the first two for loops. I know that those work for files, but I'm not sure about directories.

Thanks in advance for the help!

---------- Post updated at 02:01 PM ---------- Previous update was at 11:55 AM ----------

Nevermind, I figured it out:

Code:
#!/bin/bash

let num=1

for book in *
do
    for disc in "$book"/*
    do
        for file in "$disc"/*.mp3
        do
            if [ $num -lt 10 ]; then
                mv "$file" "$book"/"$book"0"$num".mp3

            else
                mv "$file" "$book"/"$book$num".mp3
            fi
            let num=$num+1
        done
        rm "$disc"/*
        rmdir "$disc"
    done
done


Last edited by gamendorf; 05-28-2011 at 02:01 PM..
This User Gave Thanks to gamendorf For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

2. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

3. Shell Programming and Scripting

Need Help Moving Long List Of Files Into Directories

I am very new to BASH and I am having difficulties moving a long list of image files into similarly named directories. I've been trying to come with a script all night and no luck. Here is what my list of files looks like: DSC_0059_01.jpg DSC_0059_02.jpg DSC_0059_03.jpg DSC_0059_04.jpg... (5 Replies)
Discussion started by: jowens1138
5 Replies

4. Shell Programming and Scripting

moving files to different directories

im trying to move media and other files which are in a specified directory to another directory and create another one if it does not exits(where the files will go),them also create a directory will the remaining files with different extensions will go.my first problem is that my script is not... (8 Replies)
Discussion started by: elginmulizwa
8 Replies

5. Shell Programming and Scripting

moving files between directories !!

hi i have a list of directory in a text file with all directories name in a column.(this is not exactly a file but i need to do a grep and awk on a file to find that list) i have the source folders like abchome/abc/xxyz/nl_xxabc/mm// v01 ... (4 Replies)
Discussion started by: debu000
4 Replies

6. Shell Programming and Scripting

Copying a files from a filter list and creating their associated parent directories

Hello all, I'm trying to copy all files within a specified directory to another location based on a find filter of mtime -1 (Solaris OS). The issue that I'm having is that in the destination directory, I want to retain the source directory structure while copying over only the files that have... (4 Replies)
Discussion started by: hunter55
4 Replies

7. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

Hi, I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Discussion started by: renthead720
1 Replies

8. UNIX for Dummies Questions & Answers

Moving files between directories using SFTP

I want to connect to an SFTP server, GET some files, then move those files to a different directory on the SFTP server so I don't try to GET them next time. But there doesn't seem to be a way to move files between directories on the remote server from SFTP. I missing something obvious? And if... (6 Replies)
Discussion started by: cjhancock
6 Replies

9. Shell Programming and Scripting

Bash and Awk for creating directories and moving files

I have a security system that FTPs the camera files to my machine, however I want to sort the pictures (taken every 30s) into directories by hour. Every picture uses the following file format. yymmddhhmmsstt.jpg (where tt is the milliseconds) I am thinking the for loop is best for file... (11 Replies)
Discussion started by: Kiint
11 Replies

10. Shell Programming and Scripting

moving directories to new directories on multiple servers

Hi - I am new to unix scripts...I need to move several directories on multiple servers to new directories. (0 Replies)
Discussion started by: mackdaddy07
0 Replies
Login or Register to Ask a Question