Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 22, 2013, 04:01 PM   #1
appatight
macrumors newbie
 
Join Date: Aug 2008
script to delete empty files and folders

First off I just want everyone to know that I absolutely suck at programming. I know some UNIX commands and such but when it comes to scripting I will clearly get lost. I have an external HDD that has a lot of files and empty folders. I would like a script that will delete any files that is 0kb and also any empty folders. Can one of you scripting gurus be of any help? All help would be appreciated.
__________________
Black 32GB iPhone 3GS
Macbook Unibody
White Macbook
Powerbook G4
appatight is offline   0 Reply With Quote
Old Aug 22, 2013, 04:38 PM   #2
aaronvan
macrumors 6502a
 
aaronvan's Avatar
 
Join Date: Dec 2011
Location: Washington State
Quote:
Originally Posted by appatight View Post
I know some UNIX commands...
That puts you in the 1% of computer users.

Your task seems fairly trivial. Have you tried it in Automator?
__________________
Dixon lay sprawled like a broken spider-crab. His mouth had been used as a latrine by some small creature of the night, and then as its mausoleum. He felt bad.
aaronvan is online now   0 Reply With Quote
Old Aug 23, 2013, 10:52 AM   #3
pitaya
macrumors member
 
Join Date: Jun 2012
Be careful with find commands of this nature:

Code:
$ man find | grep empty
     -empty  True if the current file or directory is empty.
$ mkdir -p Temp/{Empty,NotEmpty}
$ man find > Temp/NotEmpty.txt
$ touch Temp/Empty.txt
$ touch Temp/NotEmpty/Empty.txt

$ find Temp
Temp
Temp/Empty
Temp/Empty.txt
Temp/NotEmpty
Temp/NotEmpty/Empty.txt
Temp/NotEmpty.txt

$ find Temp -empty
Temp/Empty
Temp/Empty.txt
Temp/NotEmpty/Empty.txt
$ find Temp -empty -delete
$ find Temp
Temp
Temp/NotEmpty.txt

$ mkdir -p Temp/{Empty,NotEmpty}
$ touch Temp/NotEmpty/Empty.txt
$ man find > Temp/NotEmpty/NotEmpty.txt

$ find Temp -empty
Temp/Empty
Temp/NotEmpty/Empty.txt

$ find Temp -empty -delete
$ find Temp
Temp
Temp/NotEmpty
Temp/NotEmpty/NotEmpty.txt
Temp/NotEmpty.txt
$
pitaya is offline   0 Reply With Quote
Old Aug 24, 2013, 07:24 AM   #4
subsonix
macrumors 68040
 
Join Date: Feb 2008
You can try something like this. Keep in mind that it can potentially cause a lot of damage if it's done on an entire directory, and even more so if it's done on all sub directories. I commented out the rm command here so that you can test it first, use at your own risk etc.

Code:
#!/bin/bash

function delete_if_empty {
    if [[ $(file -b "$1") == "empty" ]]
    then
        #rm "$1"
        printf "%-25s is an empty file\n" "$1"
    fi

    if [[ $(file -b "$1") == "directory" ]]
    then
        if [[ -z $(ls -A "$1") ]]
        then
            #rm -d "$1"
            printf "%-25s is an empty directory\n" "$1"
        fi
    fi
}

for i in * ; do
    delete_if_empty "$i"
done
This script applies the delete_if_empty function on the current directory, if you wish to include all sub directories you can replace the * in the loop with $(ls -R).
subsonix is offline   0 Reply With Quote
Old Aug 24, 2013, 01:04 PM   #5
jaduff46
macrumors member
 
Join Date: Mar 2010
You need to be really careful with this. Whatever you do try it on a small directory, then one with a subdirectory, then one with multiple subdirectories.

The reason for my caution is that I had a consultant who tried something similar a few years back on a multi-user (50+) Unix box (looking for files with zero length) and came close to cutting off the machine from the outside world. Turns out all the lines coming into /dev/tty (the outside lines into the machine) had zero length.
jaduff46 is offline   0 Reply With Quote
Old Aug 24, 2013, 01:08 PM   #6
peoplevoice
macrumors member
 
Join Date: Aug 2008
find /Volumes/ExternalHardDiskDriveName -empty -exec rm {} 2>/dev/null \;

Replace ExternalHardDiskDriveName for the real name of the HDD.

This name is being displayed in the Finder Window.
peoplevoice is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Cannot Delete folders/files from Windows. Washac Windows, Linux & Others on the Mac 8 Jan 21, 2013 05:08 PM
App or script that generates a list of files and folders TheGenerous Mac Applications and Mac App Store 2 Nov 14, 2012 10:58 AM
Issue with empty folders in Finder. JTToft OS X 10.8 Mountain Lion 11 Aug 25, 2012 09:51 AM
Folders empty when layout change Nsomnia34 Jailbreaks and iOS Hacks 0 Jul 31, 2012 11:31 AM
Empty folders james.payne Mac OS X 10.7 Lion 0 Jul 12, 2012 12:47 PM

Forum Jump

All times are GMT -5. The time now is 04:40 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC