Tidying up your music files

You can grab the script “musicToFolders.sh” from here (a .zip file) or copy it from below. In either case, save the script as “musicToFolders.sh” to the direc­to­ry where you are putting the files cre­at­ed by XLD—possibly,your Music direc­to­ry (“~/Music” in the lan­guage of the OS X ‘shell’).

Open up your ‘Terminal.app’ (in the ~/Applications/Utilities fold­er) and nav­i­gate to the same direc­to­ry e.g. by typ­ing “cd ~/Music” if that’s where XLD is putting the files and where you’ve saved “musicToFolders.sh”. Hint: if you type just “cd ” (“cd” + a space) in the ter­mi­nal and then drag the icon of the fold­er where XLD has put the files out of the Find­er win­dow into the Ter­mi­nal win­dow, you’ll find the right loca­tion is set-out for you; just hit the Enter key. 

Now you’ve nav­i­gat­ed to the right direc­to­ry in the ter­mi­nal, type “chmod +x musicToFolders.sh” and Enter. This sets up the file per­mis­sions so you can exe­cute the shell script (you need to do this only once).

In the ter­mi­nal, type “sh musicToFolders.sh” and hit the Enter key. In a moment you should find that all your music files have been tidied away in fold­ers named more or less the same as your CD. The fold­er names are tak­en from XLD’s log files, which rely in turn on the CDData­base (e.g. FreeDB). They some­times err on the side of brevi­ty; you may wish to edit the fold­er names a lit­tle to clar­i­fy the con­tents of the folder.

 #!/bin/bash # # musicToFolders.sh # A bash shell script from http://www.petergallagher.com.au # This work is in the public domain # Creative Commons CC0 1.0 Universal # # Creates a sub-directory named for each XLD log file # found in the current directory # Puts the log file in the sub-directory # Creates a list of files (filelist.txt) from # the log, each with spaces escaped for f in *.log; do # dont continue if there are no .log files if [ "$f" != "*.log" ]; then mkdir "$"; mv "$f" "$"; # output lines in the log file that contain 'Filename : ' grep -h "Filename : " "$"/"$f" | # grab just the path name sed 's/ Filename : (.*)/1/' | # escape spaces in the path name # and output the line to a file list sed 's/ / /g'> "$"/filelist.txt; else echo "Oops! -- No XLD log files to process in this directory!" fi done # Find the path name of files contained # in list files in all sub-directories # Move the files in the list to the same # subdirectory as the list # process every filelist in sub-directories currDir=$PWD # enclose the dir var in quotes in case of spaces # read the result of find one-line-at-a-time find "$currDir" -name "filelist.txt" -print | while read -e f; do # Read the contents of the filelist.txt into # a new file variable and move the file # to the directory of filelist.txt cat "$f" | while read -e nf; do # Dont try to move files that dont exist if [ -f "$nf" ]; then mv "$nf" "$" fi done; # rename this filelist.txt when we're done mv "$f" "$f".proc; done 

Leave a Comment

Your email address will not be published. Required fields are marked *