Log in

Bob and his many photographs - unixish [entries|archive|friends|userinfo]
Unixish - Solving problems the UNIX way

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Bob and his many photographs [Sep. 1st, 2006|07:43 am]
Unixish - Solving problems the UNIX way

Okay finally first problem a date later that originally announced.

Problem 1:

Bob finally gave in and went digital a few years ago and since then he's accumulated a vast number of photographs. Sadly each week as he's taken more and more photos and his memory card has filled he's uploaded them to his PC without much thought. As a result Bob finds that his photos directory (~/photos) is now getting out of hand - one thousand files with randomally assigned names. Bob figures that at least having some sort of hierarchy so that the photos were in sub-directories by modified date would be a start.

Can you help Bob out?

Solutions in comments please. Please make sure you put your solution within a <code> or <pre>. Oh yeah and I'm not sure if I should screen the comments at first so other people have a chance to answer? What do people think? I'd like to hear general feedback too!

(Deleted comment)
(Deleted comment)
[User Picture]From: mdlbear
2006-09-01 02:26 pm (UTC)
I'd be inclined to use a multi-level directory structure like yyyy/mmdd or yyyy/mm/dd -- the latter looks more like a date, but the former uses a lot less space for directories. Both have the advantage that they sort properly.

You can get the second form, for example, by using --time-style=+%Y/%m/%d
(Reply) (Parent) (Thread)
From: (Anonymous)
2006-09-01 08:35 am (UTC)


Here's a pipelining approach.

find . -maxdepth 1 -type f -print0 | xargs -0 stat -c '%y!%n' | sed 's/^\(....-..-..\)[^!]*!\(.*\)/\1\n\2/'|tr '\n' '\0'|xargs -0 -n 2 sh -c 'mkdir -p "$0" && mv "$1" "$0/"'

I don't like the sh -c hack, and would rather simply construct the commands entirely in the sed expression, and then eval them, but I don't know how to do that without another sh -c hack... Point-free programming is cool. (Or was it pointless? :)

(Reply) (Thread)
[User Picture]From: lightning_rose
2006-09-01 02:30 pm (UTC)

Here's a handy online tool I wrote to convert any ascii text to html while preserving all formatting, including tabs.


(Reply) (Thread)
[User Picture]From: tjernobyl
2006-09-02 12:26 am (UTC)

Here's a quick and messy way to do it.

Figure out what we'll need to do for making directories
ls --time-style=+%Y-%m -l | cut -c 37- | cut -f 1 -d " " | sort | uniq | perl -ne 'print "mkdir $_"' >> cleanup

Figure out how we're gonna be moving the files around
ls --time-style=+%Y-%m -l | cut -c 37- | perl -pe 'chomp; s/(\d\d\d\d\-\d\d) (.+)/mv \"$2\" $1\n/g' >> cleanup

Ok, so now let's do a sanity check to make sure our changes are good
less cleanup

Looks good, let's make it happen.
chmod +x cleanup; ./cleanup

And we have files in directories ordered by month! Huzzah!

This is a really hackish way of doing things; it's dependant on the files in the directory being within a certain size range and other ickies. But as a one-time cleanup, it'll do the trick.
(Reply) (Thread)
(Deleted comment)