Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 26 Nov 2001 10:58:18 -0600
From:      Mike Meyer <mwm@mired.org>
To:        "Totally Jayyness" <Jayyness@mindspring.com>
Cc:        <questions@freebsd.org>
Subject:   Re: Scripting Problems please help
Message-ID:  <15362.29866.50899.442237@guru.mired.org>
In-Reply-To: <002401c17696$972dd5e0$0300a8c0@jayyness.com>
References:  <15362.21804.774942.105080@guru.mired.org> <002401c17696$972dd5e0$0300a8c0@jayyness.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Totally Jayyness <Jayyness@mindspring.com> types:
> #!/bin/sh
> find /usr/test -type d -print |
> while read songs do
>     (cd "$songs"; find -iname "*.mp3" > "$songs.m3u")
> done

-iname is nice touch. They just keep adding hooks. The "-print" on the
first find is irrelevant.

That version should create a file with the directory name in each
directory, that list the mp3 files in that directory and all of it's
subdirectories, recursively. You said that was Ok, but didn't say it
was a requirement.

> Hmmm, ok, this looks like it will put just the songs in the output file,
> though, in order for the playlist to work, it needs the path to the file
> also... so I make this change...
> 
> (find "$songs" -iname "*.mp3" > "$songs.m3u")


No. That one will just create a songs.m3u in the top directory
containting the files found in the last directory you searched.

Let's get the problem statement straight. You have a tree of files,
some of which are mp3's, possibly called Mp3 or MP3 or mP3.

Do you want a playlist created in each directory containing 1) the
mp3s in that directory, 2) the mp3s in that directory and all
subdirectories of it, or 3) something I've overlooked?

In each playlist, do you want the path to the file from a) the
directory the playlist is in, b) the top of the tree you're runnings
this from, c) absolute or d) something I've overlooked.

1a:

find . -type d |
while read songs
  do
    playlist=$(echo "$songs.m3u" | sed 's;.*/;;')
    (cd "$songs"; ls | grep -i '.mp3$' > "$playlist")
  done

1b:

find . -type d |
while read songs
  do
    playlist=$(echo "$songs.m3u" | sed 's;.*/;;')
    find "$songs" -maxdepth 1 -iname '*.mp3' > "$songs/$playlist"
  done	

1c:

find /mp3dir -type d |
while read songs
  do
    playlist=$(echo "$songs.m3u" | sed 's;.*/;;')
    find "$songs" -maxdepth 1 -iname '*.mp3' > "$songs/$playlist"
  done	

2a:

find . -type d |
while read songs
  do
    playlist=$(echo "$songs.m3u" | sed 's;.*/;;')
    (cd "$songs"; find . -iname '.mp3$' > "$playlist")
  done

2b:

find . -type d |
while read songs
  do
    playlist=$(echo "$songs.m3u" | sed 's;.*/;;')
    find "$songs" -iname '*.mp3' > "$songs/$playlist"
  done	

2c:

find /mp3dir -type d |
while read songs
  do
    playlist=$(echo "$songs.m3u" | sed 's;.*/;;')
    find "$songs" -iname '*.mp3' > "$songs/$playlist"
  done	


ls is the wrong tool for listing filenames with the path. So the only
case it works for is when all you want is files in the current
directory with the path relative to that directory (i.e. - none). Find
always lists files with full path information. So we create playlist
by the path information off - being careful to preserve multiple
spaces in the names. To get names relative to the current directory,
you cd into that directory and run find (or ls for 1a) just putting
the output in the playlist. Whether you start the outer find with "."
or "/mp3dir" is irrelevant, because the inner find is going to start
at "." after the cd. To get paths relative to the top directory, you
run the outer find on ., the inner find on the directory name, and
redirect the output appropriately. To get full paths, do the same
except that you start the outer find with the full path. If you want
to keep find from descending into subdirectories, use "-maxdepth 1".

If you need option 3 or d, it's going to wait until after I get some
sleep.

	<mike
--
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Q: How do you make the gods laugh?		A: Tell them your plans.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15362.29866.50899.442237>