#!/usr/bin/env perl use strict; use warnings; use 5.10.0; use POSIX qw(strftime); use File::Basename; my $root = $ENV{HOME} . '/p1k3/archives'; my @t = localtime; # strftime doesn't have a non-zero-padded month/day? my $dayfile = strftime("%Y/%m/%e", @t); $dayfile =~ s{/[0 ]}{/}gx; # (Or: Use it anyway and apply an ad-hoc regex fix, because though we often # feel we know better in this day and age, we are not ashamed of text # transformation. not when the chips are down. text is the most powerful # computational abstraction i've ever come close to understanding, if i'm # honest, and past a certain point life is too short and too precious a span to # abandon the only tools you have ready to hand.) my $today = "$root/$dayfile"; if (-d $today) { print $today; } else { my $dirname = $today; until (-d ($dirname)) { # If a dir for the month or year doesn't exist, this moves one step up. # Should land in archives/ if no year. $dirname = dirname($dirname); } print $dirname; }