Author Topic: [bash] Find empty folders  (Read 6545 times)

0 Members and 1 Guest are viewing this topic.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
[bash] Find empty folders
« 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [bash] Find empty folders
« Reply #1 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.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [bash] Find empty folders
« Reply #2 on: May 30, 2010, 02:32:37 pm »
Maybe you should write a bash script to do that :P

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [bash] Find empty folders
« Reply #3 on: May 30, 2010, 02:56:43 pm »
http://getid3.sourceforge.net/

I'll toss together a quick command-line PHP program. :)
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [bash] Find empty folders
« Reply #4 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 :)


Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [bash] Find empty folders
« Reply #5 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";
        }*/
    
}

?>
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [bash] Find empty folders
« Reply #6 on: April 06, 2011, 05:07:25 pm »
Bump!

find -depth -type d -empty

What do I win?

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [bash] Find empty folders
« Reply #7 on: April 06, 2011, 05:27:17 pm »
Bump!

find -depth -type d -empty

What do I win?


haha, win.