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 ...
- 03-30-2008 #1Just 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
- 03-30-2008 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
I suggest using the find command as follows:
for CMD in $(find . -name "*.mp3")
- 03-31-2008 #3Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
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".
- 03-31-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Remove the blank space.
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.direc="/home/dominic/Desktop/full albums/"
for CMD in "`ls -R |grep .mp3`"
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:
Alternatively, use the -exec feature of find (though for multiple commands, the while way is more convenient):find "$direc" -name "*.mp3" | while read file
do
.....
done
In the <whatever> part, goes the command. You can use '{}' on that part to reference the current mp3 file.Code:find "$direc" -name "*.mp3" -exec <whatever> \;
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.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
- 03-31-2008 #5Just Joined!
- Join Date
- Mar 2008
- Posts
- 2
Thanks all. I'll try all of the above. They look much better than my slapdash effort.


Reply With Quote
