New Directories from Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New Directories from Files
# 1  
Old 01-20-2005
New Directories from Files

The Dilemma:
I have a huge directory filled with Music, in fact about 8,000 songs. I now want to separate all these songs into sub folders by the artist's name. Most all the song files are in the form of "Song Artist - Song Title." For example, Bach - Allegro ma non troppo.mp3. Now what I want is a script that would go through all these files and take the string before the "-" and use that to create a directory with that name, then place all the files which start with that string in the newly created sub folder. I have a small knowledge of shell scripting, but know Java and Cpp. I know in java that I could use a string tokenizer to accomplish some of this, but I have no idea how to shell script this problem.

Thanks in advance,
Robert

BTW, I am running Red Hat Linux 8.0, using bourne.
# 2  
Old 01-21-2005
Had done in k-shell ; Please modify it as per Bourner shell.


Code:
#!/usr/bin/ksh

ls *mp3 > songsList

exec  < songsList

while read line
do

   singer=`echo $line | cut -d"-" -f 1 | sed "s/.$//"`

   mkdir $singer  2>/dev/null

   cp "$line" "$singer/$line"


done

# 3  
Old 01-21-2005
This is bash but might work in ksh too.
Code:
#!/usr/bin/bash
cd "../My Music" #---or wherever
find . -maxdepth 1 -type f -name '*.mp3' -print | while read File
do
   Dest="${File%% -*}" #---remove everything after the first " -"
   echo "File:[${File}]" "Dest:[${Dest}]"  #----just a debug message
   if [[ ${File} = ${Dest} ]]
   then
      echo "File not moved. File name does not contain \" -\""
   else
      if [[ ! -d ${Dest} ]] #---check for non-existence of destination directory
      then
         mkdir "${Dest}" && echo "Created new directory: \"${Dest}\""
      fi
      mv "${File}" "${Dest}/" && echo "File moved"
   fi
   echo
done

Tested...
Code:
File:[./dummy.mp3] Dest:[./dummy.mp3]
File not moved. File name does not contain " -"

File:[./Pete Murray - Feeler - 06 - Please.mp3] Dest:[./Pete Murray]
Created new directory: "./Pete Murray"
File moved

File:[./Pete Murray - Feeler - 08 - My Time.mp3] Dest:[./Pete Murray]
File moved

File:[./Pete Murray - Feeler - 10 - No More.mp3] Dest:[./Pete Murray]
File moved


Last edited by Ygor; 01-21-2005 at 03:55 AM..
# 4  
Old 01-21-2005
Wow, thats great! Thanks a lot!

Robert
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

3. Shell Programming and Scripting

Rename the files in all the directories and sub-directories

Hi all, I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named “dat” or “gridded”. Dat sub-directories have files with extension “jpg.dat” and gridded sub-directories have files with extension “.jpg”. I need to... (1 Reply)
Discussion started by: AshwaniSharma09
1 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. Shell Programming and Scripting

How to access files from different directories and to perform search action in those files?

Hi, I want to access files from different directories (for example: /home/dir1/file1 , /home/dir2/file2 ...) Like this i have to access these files(file1, file2...). (3 Replies)
Discussion started by: bangarukannan
3 Replies

6. Shell Programming and Scripting

How to store files/directories in different directories!!

Hi legends, I am writing a script, in that my requirement is, if all the fill types stored in one directory from that we need to separate different different directories based on the file types. for example in a directory(anish). 5 different types files 1- directory 2- .txt files 2- .sh... (1 Reply)
Discussion started by: anishkumarv
1 Replies

7. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

8. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

9. Shell Programming and Scripting

mget * (obtein files from current directory but not the files form sub-directories)

Hello, Using the instruction mget (within ftp) and with "Interactive mode off", I want to get all files from directory (DirAA), but not the files in sub-directories. The files names don't follow any defined rule, so they can be just letters without (.) period Directory structure example: ... (0 Replies)
Discussion started by: Peter321
0 Replies
Login or Register to Ask a Question