Skip to content

Instantly share code, notes, and snippets.

@evilchili
Created March 22, 2013 17:00
Show Gist options
  • Save evilchili/5222964 to your computer and use it in GitHub Desktop.
Save evilchili/5222964 to your computer and use it in GitHub Desktop.
Deleted the N oldest directories in the current directory
#!/usr/bin/env bash
# find . -maxdepth 1 -type d -printf '%T@ %p\0'
# -- For every directory in the current directory, print a numeric timestamp, a space, the filename, and a null
#
# sort -z -n
# -- Split the output on nulls (-z) and sort numerically (-n)
#
# gawk 'BEGIN {RS="\0";ORS="\0";FS=" "} NR<=3 {print $2}' | \
# -- Print first (ie, oldest) three filenames in the list, separated by nulls.
#
# xargs -0 rm -rf
# -- for each filename separated by a null (-0), execute 'rm -rf'
#
find . -maxdepth 1 -type d -printf '%T@ %p\0' | \
sort -z -n | \
gawk 'BEGIN {RS="\0";ORS="\0";FS=" "} NR<=3 {print $2}' | \
xargs -0 rm -rf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment