You are not logged in.

#1 2010-05-03 12:46:09

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Need help with a script (execute things dir by dir)

hi, i have a little awk script that creates a textfile containing informations of a given set of media files.

what i want now is some script that will execute my awk script on a dir by dir base. After each directory it should append the new tags to the textfile and write some keyword between them.

In practive i have this:

mutagen-inspect $1/* | grep -E '^artist=|^album=|^title|^date|^genre|^totaltracks' | sort | uniq | sed 's/=/::/' > list.txt

This will produce this text file:

artist::Alice In Chains
album::Dirt
date::1992
genre::Grunge
totaltracks::13
title::Them Bones
title::Dam That River
title::Rain When I Die
title::Sickman
title::Rooster
title::Junkhead
title::Dirt
title::God Smack
title::[Iron Gland]
title::Hate To Feel
title::Angry Chair
title::Down In A Hole
title::Would?

what i want now is that the script will walk through a given set of directories recursively and write each output to the same file. in between it should write a keyword.

so in the end i want to have something like this:

artist::Alice In Chains
album::Dirt
date::1992
genre::Grunge
totaltracks::13
title::Them Bones
title::Dam That River
title::Rain When I Die
title::Sickman
title::Rooster
title::Junkhead
title::Dirt
title::God Smack
title::[Iron Gland]
title::Hate To Feel
title::Angry Chair
title::Down In A Hole
title::Would?

EOI

artist::Alice In Chains
album::Sap
date::1992
genre::Grunge
totaltracks::4
title::BLA
title::BLUB
title::DADADA

Last edited by Rasi (2010-05-03 13:53:25)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2010-05-03 14:45:42

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Need help with a script (execute things dir by dir)

Maybe you should run the script in the given dirs, collect all the output files, add the keywords to the end of the file(s) and 'cat list1 list2 ...'?

Offline

#3 2010-05-03 14:47:49

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,306

Re: Need help with a script (execute things dir by dir)

I think the find command will do just what you need.  It will look recursively through directories starting at some specified root for files that meet some RE criteria.  It can then perform an operation on each of those files (print the name, pass the name as an argument into an executable, etc...)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#4 2010-05-03 14:54:19

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Need help with a script (execute things dir by dir)

echo -e "\nEOI\n" >> list1.txt

should give you the keyword and newlines. Of course you can script this part too :-)
The last part is just gluing the lists back to back 'cat list1 list2 ...'

At least this is how I understand it. Feel free to suggest another way.

Last edited by karol (2010-05-03 14:55:04)

Offline

#5 2010-05-03 14:57:58

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Need help with a script (execute things dir by dir)

You should use tee for the script, so you can enter something into read for the keyword, although I didn't really understand that part of your question. This way you can also see the list before you enter the keyword.

 { mutagen-inspect "$1"/* | grep -E '^artist=|^album=|^title|^date|^genre|^totaltracks' | sort | uniq | sed 's/=/::/'
  echo
  read -p "KEYWORD: "
  echo $REPLY
  echo
 } | tee -a list.txt

Like ewaller said, just put it in a loop like `for file in $(find -type d)` or something like that.

Offline

#6 2010-05-03 15:06:28

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: Need help with a script (execute things dir by dir)

Procyon wrote:

You should use tee for the script, so you can enter something into read for the keyword, although I didn't really understand that part of your question. This way you can also see the list before you enter the keyword.

 { mutagen-inspect "$1"/* | grep -E '^artist=|^album=|^title|^date|^genre|^totaltracks' | sort | uniq | sed 's/=/::/'
  echo
  read -p "KEYWORD: "
  echo $REPLY
  echo
 } | tee -a list.txt

Like ewaller said, just put it in a loop like `for file in $(find -type d)` or something like that.

the keyword is just some fixed sequence of characters. The list is gonna be parsed by a programm afterwards and each keyword will tell the programm that a new item starts now.

karol wrote:

Maybe you should run the script in the given dirs, collect all the output files, add the keywords to the end of the file(s) and 'cat list1 list2 ...'?

thats more or less what i am asking about. Its about around 1000 directories so it needs to be automatic smile

Last edited by Rasi (2010-05-03 15:08:40)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#7 2010-05-03 15:08:18

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: Need help with a script (execute things dir by dir)

karol wrote:

Maybe you should run the script in the given dirs, collect all the output files, add the keywords to the end of the file(s) and 'cat list1 list2 ...'?

thats more or less what i am asking about. Its about around 1000 directories so it needs to be automatic smile


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#8 2010-05-03 15:10:21

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Need help with a script (execute things dir by dir)

> Its about around 1000 directories
Cool :-)

You can still use Procyon's script, just

echo -e "\nEOI\n"

Offline

#9 2010-05-03 15:17:09

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Need help with a script (execute things dir by dir)

write_tags() {
  # put your $1/* based tag logic here
}

prev_dir=''

while IFS=$'\n' read -r this_dir; do
  if [[ -n "$prev_dir" ]] && [[ "$this_dir" != "$prev_dir" ]]; then
    echo -e"\nEOI\n" >> file
  fi

  write_tags "$this_dir" >> file

  prev_dir="$this_dir"
done < <(find ./ -type d)

untested, and off the top of my head.  but it should work.

Last edited by brisbin33 (2010-05-03 15:17:40)

Offline

#10 2010-05-03 15:34:17

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Need help with a script (execute things dir by dir)

The dir structure can be tricky

[karol@black music]$ l cd_music/Queen/
total 12K
drwxr-xr-x 2 4.0K Sep  6  2009 Greatest Hits [Parlophone]
drwxr-xr-x 2 4.0K Sep  6  2009 Greatest Hits, Vol. 3
drwxr-xr-x 2 4.0K Sep  6  2009 Platinum Collection, Vol. 1-3 Disc 2

I keep my collection of ripped CD's in cd_music. One of them is a 3 CD album Queen - The Greatest Hits. The folder 'Queen' doesn't have any mp3s (as you can see on the listing), but the subfolders do. You may have a different scheme 'Queen - Greatest Hits, Vol. 3' etc. with no top 'Queen' folder - which will make things easier.

audiopath="/home/karol/music"
list="/home/karol/my_list"

for dir in $(find $audiopath -type d); do
        mutagen-inspect $dir/* | grep -E '^artist=|^album=|^title|^date|^genre|^totaltracks' | sort | uniq | sed 's/=/::/' >> $list
        echo -e "\nEOI\n" >> $list
done

It wouldn't work for me because of the spaces in filenames and foldernames ;P

Offline

#11 2010-05-03 16:29:54

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Need help with a script (execute things dir by dir)

Don't forget possible directories inside the album directory for instance for scans of the booklet.

It would be better to look for any tagged audio files and only use the directories those are in.

for mdir in $(

  for mfile in find -iname '*.mp3' -o -iname '*.ogg' -o -iname '*.flac' -o -iname '*.whatever I missed'
    do dirname "$mfile"
  done | sort | uniq

); do script "$mdir"; done

Offline

#12 2010-05-03 16:32:14

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Need help with a script (execute things dir by dir)

Procyon wrote:

It would be better to look for any tagged audio files and only use the directories those are in.

That's what I was talking about.

karol wrote:

The folder 'Queen' doesn't have any mp3s (as you can see on the listing), but the subfolders do. You may have a different scheme 'Queen - Greatest Hits, Vol. 3' etc. with no top 'Queen' folder - which will make things easier.

Offline

#13 2010-05-03 16:41:58

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Need help with a script (execute things dir by dir)

 
while IFS=$'\n' read -r var; do 
  # stuff with $var
done < <(find ./ --all-those-options -exec dirname {} \; | sort -u)

would handle spaces and might be more efficient; though, the bikeshed's about the same smile.

Last edited by brisbin33 (2010-05-03 16:42:08)

Offline

#14 2010-05-03 16:49:25

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Need help with a script (execute things dir by dir)

@karol, yeah the first line was to you, but I forgot that later.
@brisbin33, nice one and certainly setting the IFS is important.

Offline

Board footer

Powered by FluxBB