Jeremiah Sturgill

code etc.

Find the mystery meat

I’ve taken over management of a server that is full of… stuff. Some stuff is good! We all need stuff.

But we probably have more stuff than we need.

Since no one knows what’s being used and what isn’t being used, it can be tough to decide what is safe to back up and remove from the server. Server logs are one indication; another good question I’ve been asking is, “When is the last time anyone touched this directory?”

Here’s a bash script to check subdirectories of the current working directory and print out the date of the most recently modified file:

find $(pwd) -mindepth 1 -maxdepth 1 -type d | while read line; do sudo find $line -type f -exec stat --format '%Y :%y %n' "{}" ; | sort -nr | cut -d ':' -f2- | cut -d ' ' -f1,4 | head -1; done

One of the “important” bits is find $(pwd). The output of pwd is equivalent to the full path of the current directory. When find lists its results, they will be listed as absolute paths rather than the relative paths that would be returned if we used a dot.

The rest is relatively straightforward. It could be made faster by checking each directory in parallel, but I haven’t had any need to optimize yet.