Find the answer to your Linux question:
Results 1 to 5 of 5
Hi I've been trying to write a very basic script for the last 2 weeks. The script i want should search for mp3's in the current folder and all subdirectories ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2

    basic scripting help

    Hi

    I've been trying to write a very basic script for the last 2 weeks. The script i want should search for mp3's in the current folder and all subdirectories then it should get the name and artist from the id3 tags and then check to see if there is a folder for that artist and album and if not create them then move the file to the directory. i managed to get it to work if i only want mp3's in one folder but i cant get `ls -R *.mp3` to work so have had to use grep. below is what i have so far.

    #! /bin/bash
    direc="/home/dominic/Desktop/full albums/"
    for CMD in "`ls -R |grep .mp3`"
    do
    artist=`id3ed -i "$CMD" | grep artist: | sed s/"artist: "//g`
    album=`id3ed -i "$CMD" | grep album: | sed s/"album: "//g`
    if test -d "$direc$artist"
    then
    mkdir "$direc$artist"/"$album"/
    else
    mkdir "$direc$artist"/
    mkdir "$direc$artist"/"$album"/
    fi
    cp "$file" "$direc$artist"/"$album"/
    done

    At the moment it treats the output from ls as one block when i need to use each line individually.

    any help would be massively appreciated

    Dom

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    I suggest using the find command as follows:

    for CMD in $(find . -name "*.mp3")

  3. #3
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    Quote Originally Posted by dommccas View Post
    Hi

    I've been trying to write a very basic script for the last 2 weeks. The script i want should search for mp3's in the current folder and all subdirectories then it should get the name and artist from the id3 tags and then check to see if there is a folder for that artist and album and if not create them then move the file to the directory. i managed to get it to work if i only want mp3's in one folder but i cant get `ls -R *.mp3` to work so have had to use grep. below is what i have so far.

    #! /bin/bash
    direc="/home/dominic/Desktop/full albums/"
    for CMD in "`ls -R |grep .mp3`"
    do
    artist=`id3ed -i "$CMD" | grep artist: | sed s/"artist: "//g`
    album=`id3ed -i "$CMD" | grep album: | sed s/"album: "//g`
    if test -d "$direc$artist"
    then
    mkdir "$direc$artist"/"$album"/
    else
    mkdir "$direc$artist"/
    mkdir "$direc$artist"/"$album"/
    fi
    cp "$file" "$direc$artist"/"$album"/
    done

    At the moment it treats the output from ls as one block when i need to use each line individually.

    any help would be massively appreciated

    Dom
    The problem is that you did: for CMD in "`ls -R |grep .mp3`"

    The use of the quotes (") around the command execution is a single string. Remove the quotes. A better solution is the find commend. If you have or will have filenames with one or more spaces within the name you should look at the option "-print0".

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by dommccas View Post
    #! /bin/bash
    Remove the blank space.

    direc="/home/dominic/Desktop/full albums/"
    for CMD in "`ls -R |grep .mp3`"
    Using these kind of things can be very problematic, overall if the ansi escape sequences for colors and many others are not correctly parsed, plus you will have problems with spaces and probably many more.

    Use find, and instead of for, use while on this fashion, because they way someone posted above will fail if the file list is very big:

    find "$direc" -name "*.mp3" | while read file
    do
    .....
    done
    Alternatively, use the -exec feature of find (though for multiple commands, the while way is more convenient):

    Code:
    find "$direc" -name "*.mp3" -exec <whatever> \;
    In the <whatever> part, goes the command. You can use '{}' on that part to reference the current mp3 file.

    do
    artist=`id3ed -i "$CMD" | grep artist: | sed s/"artist: "//g`
    album=`id3ed -i "$CMD" | grep album: | sed s/"album: "//g`
    if test -d "$direc$artist"
    then
    mkdir "$direc$artist"/"$album"/
    else
    mkdir "$direc$artist"/
    mkdir "$direc$artist"/"$album"/
    fi
    cp "$file" "$direc$artist"/"$album"/
    done
    Just as a stylistic note: you should use / in the middle of $direc$artist, it's just to improve the readability, and to prevent problems if you later want to parametrize the search path and the user forgets to include the last slash. Note that having redundant slashes is no problem for bash.

  5. #5
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2
    Thanks all. I'll try all of the above. They look much better than my slapdash effort.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...