Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: iago on May 30, 2010, 01:32:26 pm

Title: [bash] Find empty folders
Post by: iago on May 30, 2010, 01:32:26 pm
So, for some reason, at some point in the past some of my .mp3's disappeared. The folder structure was there, but the actual files vanished. I assume it's because I deleted them from a playlist in my music program and accidentally deleted them from disk.

Anyway, before I re-ripped all my disks, I wanted to figure out exactly which folders contained nothing but other folders (ie, a folder structure but no files).

Here's the script I wrote:
until [ -z "$1" ]
do
 if [ -d "$1" ]; then
  if [ `find "$1" -type f | wc -l` -ne 0 ]; then
   echo "$1 is not empty";
  else
   echo "$1 is empty";
  fi
 fi
 shift
done


You basically run it like this:
$ ~/tmp/is_empty.sh *
1208 is not empty
Acdc is not empty
Afterbeat is empty
Anti-Flag is not empty
Arch Enemy is not empty
Arol is not empty
...


Of course, you can grep for "is empty", or trivially modify the script to only display empty folders.
Title: Re: [bash] Find empty folders
Post by: iago on May 30, 2010, 02:03:42 pm
Then the second half.. I loaded up my iPod in the /mnt/ folde rand used this commandline to find where the .mp3 files are stored:
$ cd /mnt/ipod/iPod_Control/iTunes
$ cp `cat iTunesDB.ext | grep -A3 "filename_locale.*Afterbeat" | grep filename_ipod | sed "s|.*=|/mnt/ipod/|" | sed "s|:|/|g"` ~/tmp/Afterbeat/


After that, I had a bunch of misnamed files in the ~/tmp folder. The ID3 tags are intact, so it's just a matter of using a program that can rename files based on ID3 tags.
Title: Re: [bash] Find empty folders
Post by: rabbit on May 30, 2010, 02:32:37 pm
Maybe you should write a bash script to do that :P
Title: Re: [bash] Find empty folders
Post by: Joe on May 30, 2010, 02:56:43 pm
http://getid3.sourceforge.net/

I'll toss together a quick command-line PHP program. :)
Title: Re: [bash] Find empty folders
Post by: iago on May 30, 2010, 03:00:38 pm
Maybe you should write a bash script to do that :P
Heh, that'd be slick. I don't think there's an id3 reader that comes with Linux, though, otherwise I could. I'm sure there's a commandline one out there, though, that I could script up!

Either way, I wrote a program in Java many years ago that will rename files based on ID3 and set the ID3 of a file based on its filename, so I just used that. It's an ugly hack of a program, but it's been working for me for years :)

Title: Re: [bash] Find empty folders
Post by: Joe on May 30, 2010, 04:15:21 pm
This didn't turn out nearly as nice as I wanted it to, but if anyone wants to toy with it, be my guest.

Command:
Code: [Select]
php printid3info.php -i -l /Users/william/Documents/Programming/php/getid3-1.7.9/getid3/getid3.php -f %artist, %album, %year, %genre, %title < printid3list.txt
Input file:
Code: [Select]
/Users/william/Music/iTunes/iTunes Music/Music/Anberlin/Lost Songs/01 The Haunting.mp3
/Users/william/Music/iTunes/iTunes Music/Music/Journey/Journey's Greatest Hits/02 Don't Stop Believin'.m4a
/Users/william/Music/iTunes/iTunes Music/Music/Rick Astley/Whenever You Need Somebody/01 Never Gonna Give You Up.mp3

Output:
Code: [Select]
Library path switched to: /Users/william/Documents/Programming/php/getid3-1.7.9/getid3/getid3.php
Format set: %artist, %album, %year, %genre, %title
Enter absolute filename: Anberlin, Lost Songs, 2007, Other, The Haunting
Enter absolute filename: Journey, Journey's Greatest Hits, %year, %genre, Don't Stop Believin'
Enter absolute filename: Rick Astley, Whenever You Need Somebody, 1987, Other, Never Gonna Give You Up
Enter absolute filename: Error opening

Code: [Select]
#!/bin/php
<?php

    
//
    // ID3 Tag Info Printer
    // Written by joe[x86]
    // Based on getID3 by James Heinric
    // http://www.getid3.org
    //
    
    // set defaults
    
$libpath "/usr/include/getid3/getid3.php";
    
$format "%artist - %title";

    
// parse commandline input
    
for ($i 1$i $argc$i++) {
        if (
$argv[$i] == "-h") {
            echo 
"Usage: php printid3info.php [-h | -v |  -l libpath | -f format]\n";
            echo 
"-h: Help. Displays this message.\n";
            echo 
"-v: Verbose. Incomplete.\n";
            echo 
"-l: Library path.";
            echo 
"-f format: Replaces the following format codes:\n";
            echo 
"   %title, %artist, %album, %year, %track, %genre\n";
            echo 
"   Uses \"%artist - %title\\n\" by default. Must be last argument\n";
            die();
        } else if (
$argv[$i] == "-v") {
            echo 
"Verbose mode not complete.\n";
        } else if (
$argv[$i] == "-l") {
            
$i++;
            
$libpath $argv[$i];
            echo 
"Library path switched to: $libpath\n";
        } else if (
$argv[$i] == "-f") {
            
// begin accepting format
            
$format "";
            for (
$j $i 1$j $argc$j++) {
                
$format .= " " $argv[$j];
            }
            
$format trim($format);
            echo 
"Format set: $format\n";
        }
    }
    
    
// include library
    
if(@!include($libpath))
        die(
"Missing getID3 library at $libpath\n");
    
    
$getID3 = new getID3;
    
    while(
true) {
        echo 
"Enter absolute filename: ";
        
$filename trim(fgets(STDIN));
        
$fileinfo $getID3->analyze($filename);
        
getid3_lib::CopyTagsToComments($fileinfo);
        if (
array_key_exists('comments'$fileinfo)) {
            
$output $format;
            if (
array_key_exists("title"$fileinfo['comments']))
                
$output str_replace("%title"$fileinfo['comments']['title'][0], $output);
            if (
array_key_exists("artist"$fileinfo['comments']))
                
$output str_replace("%artist"$fileinfo['comments']['artist'][0], $output);
            if (
array_key_exists("album"$fileinfo['comments']))
                
$output str_replace("%album"$fileinfo['comments']['album'][0], $output);
            if (
array_key_exists("year"$fileinfo['comments']))
                
$output str_replace("%year"$fileinfo['comments']['year'][0], $output);
            if (
array_key_exists("track"$fileinfo['comments']))
                
$output str_replace("%track"$fileinfo['comments']['track'][0], $output);
            if (
array_key_exists("genre"$fileinfo['comments']))
                
$output str_replace("%genre"$fileinfo['comments']['genre'][0], $output);
            echo 
$output "\n";
        } else {
            echo 
"Error opening $filename\n";
            die();
        }
        
        
// display all variables
        /*foreach($fileinfo['comments'] as $key => $value) {
            echo "$key => $value\n";
        }*/
    
}

?>
Title: Re: [bash] Find empty folders
Post by: Camel on April 06, 2011, 05:07:25 pm
Bump!

find -depth -type d -empty

What do I win?
Title: Re: [bash] Find empty folders
Post by: Sidoh on April 06, 2011, 05:27:17 pm
Bump!

find -depth -type d -empty

What do I win?


haha, win.