Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Clean Up Empty Directories

by GotToBTru (Prior)
on Feb 16, 2018 at 21:50 UTC ( #1209337=CUFP: print w/replies, xml ) Need Help??

The code somebody else wrote cleans out old files, but leaves the directories behind. This cleans up the directories.

#!/usr/bin/perl use strict; use warnings; chomp(my @list = `du -kh /mnt/edi/si51/documents`); my $dltd = 0; foreach my $line (@list) { my ($size,$path) = split /\t/, $line; $size =~ s/\D//g; if ($size == 0) { rmdir $path && $dltd++ } } printf "%d directories deleted.\n",$dltd;

UPDATE: There are several things that were in an earlier version of this script that didn't make the second cut, but only because I got lazy. My original got deleted somehow, and I had foolishly not kept a copy, so I wrote the above quickly.

The directory structure is documents/4digityear/abbreviatedcardinalmonth/2digitday/hour/minute. At first I restricted deletions to directories above some number of days old, but rmdir updates the directory time information, meaning a directory that was now empty because all its empty constituent directories were gone looked like it was brand new. This made it useless to run consecutively. I came up with a calculation that used the directory tree to come up with the age, and that worked. I just didn't bother with it when I rewrote the script this time. Some of the alternate solutions don't have that limitation.

But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Replies are listed 'Best First'.
Re: Clean Up Empty Directories
by choroba (Archbishop) on Feb 16, 2018 at 23:35 UTC
    I usually just run
    find -type d -empty -exec rmdir {} +
    several times until there's no empty directory.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      GNU find has a -delete action. It implies -depth and as such, can do the clean-up in one go.

      I'm pretty sure the command that does the file cleanup is a find command somewhere. I think that there should have been a way to write it such that it cleaned out the directories too.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Clean Up Empty Directories
by Tux (Canon) on Feb 17, 2018 at 10:18 UTC
    use 5.14.2; use warnings; our $VERSION = "1.01 - 20180217"; my $cmd = $0 =~ s{.*/}{}r; sub usage { my $err = shift and select STDERR; print "usage: $cmd [ --rmdir ] [ dir ... ]\n"; exit $err; } # usage use Getopt::Long qw(:config bundling nopermute); GetOptions ( "help|?" => sub { usage (0) }, "V|version" => sub { say "$cmd [$VERSION]"; exit 0; }, "d|delete|rm|rmdir" => \my $opt_d, ) or usage (1); use File::Find; no warnings "File::Find"; my @dir = @ARGV ? @ARGV : ("."); grep { ! -d $_ } @dir and usage (1); finddepth (sub { ! -d $_ || m/^\.\.?$/ and return; my ($dh, @d); unless (opendir $dh, $_ and @d = readdir $dh) { warn "Cannot read $File::Find::name\n"; return; } closedir $dh; if (@d == 2) { print "$File::Find::name\n"; $opt_d and rmdir $_; } }, @dir);

    Used and tested on HP-UX, AIX, and Linux. Don't know if it works on Windows.

    The find/exec has the problem that it needs to be run many times to cope with deep empty trees, and I had trees of 20+ levels on systems with 4Tb. It is no fun to run that many times.

    Another advantage of above script is that it first reports. Actually removing is an option.

    No warnings File::Find, because I don't care if folders are removed while running or symbolic links still exist or NFS (if a user chooses to do so) falls inactive.

    Enjoy, Have FUN! H.Merijn

      Too much typing! ;) I use my own as part of a space recovery script that runs in background, so I didn't plan or allow for any interaction. The du command often takes more than 20 minutes to return in my application.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re:Clean Up Empty Directories
by tybalt89 (Prior) on Feb 16, 2018 at 22:15 UTC
Re: Clean Up Empty Directories
by morgon (Priest) on Feb 17, 2018 at 02:24 UTC
    On what system does this actually work?

    On linux even empty directories take up some space so "du -kh" reports 4.0K for them and therefore never would be deleted by your logic as far as I can see...

    This is simply (not even talking about the hard-coded directory) broken code.

      It works on the AIX system where I run it. I get 0s from du -kh for empty directories. I was going to ask if it was unreasonable to assume that posted code works, but then I remembered about a million cases where that wasn't the case, so I can't fault you for asking.

      You can talk about the hardcoded directory, if you want. I'm pretty sure you can even change it or rewrite it without it.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1209337]
Approved by davies
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-05-28 14:52 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (99 votes). Check out past polls.