Created
June 13, 2014 19:02
-
-
Save Resisty/24e89edee9c6070b0ca7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You have a proprietary file system type (myfs) with a custom set of command line tools. | |
# You need to clean up all files with a modification time older than 24 hours. | |
# Any directories left empty as a result of this cleanup should also be removed as well. | |
# | |
# The command to list a directory is 'myfs ls' | |
# The command to list a directory recursively is 'myfs lr' | |
# The command to delete a file or directory is 'myfs rm' | |
# | |
# The root uri of the filesystem is 'myfs://myserver/' | |
# | |
# For example, the output from running 'myfs lr myfs://myserver/' might result | |
# in the following output: | |
# | |
# drwxr-xr-x root root 0 2010-07-01 07:40 /mydirectory1 | |
# -rw-r--r-- root root 1352 2010-07-09 08:42 /mydirectory1/myfile1.txt | |
# -rw-r--r-- root root 129772 2010-07-09 09:31 /mydirectory1/myfile2.txt | |
# drwxr-xr-x root root 0 2010-07-01 07:46 /mydirectory2 | |
# -rw-r--r-- root root 1032 2010-07-09 10:42 /mydirectory2/myfile3.txt | |
# -rw-r--r-- root root 103522 2010-07-09 11:21 /mydirectory2/myfile4.txt | |
# -rw-r--r-- root root 103522 2010-07-09 11:21 /mydirectory2/a longer file name.txt | |
# drwxr-xr-x root root 0 2010-07-01 07:48 /mydirectory2/mydirectory3 | |
# -rw-r--r-- root root 1032 2014-07-13 10:46 /mydirectory2/mydirectory3/myfile5.txt | |
# -rw-r--r-- root root 103522 2010-07-09 11:00 /mydirectory2/mydirectory3/myfile6.txt | |
# drwxr-xr-x root root 0 2010-07-01 07:46 /mydirectory4 | |
# -rw-r--r-- root root 1032 2010-07-09 10:42 /mydirectory4/myfile7.txt | |
# -rw-r--r-- root root 103522 2010-07-09 11:21 /mydirectory4/myfile8.txt | |
# ... | |
# | |
# Write a script which cleans these old files from the myfs filesystem, and delete | |
# the directory if it's now empty (but not if the directory was already empty before cleanup). | |
#!/usr/bin/env bash | |
# /a/b/c/d.txt | |
# ...../a | |
# ...../a/b | |
# ...../a/b/c | |
# ...../a/b/c/d.txt | |
function empty_the_dir() { | |
notpreviouslyempty='' | |
fp=$(echo $1 | awk '{print $7}') | |
if [ $(myfs lr $fp | wc -l) -le 1 ] then | |
return | |
fi | |
for i in $(myfs lr $fp | tail --afterline 1) | |
ago=$(echo $i | awk '{print $5 $6}') | |
agoseconds=$(date +%s $ago) | |
now=$(date +%s) | |
fp=$(echo $i | awk '{print $7}') | |
if [ $i ~= /^-/ ] && [ $(expr now - agoseconds) -gt 86400 ] then | |
myfs rm fp | |
notpreviouslyempty='true' | |
fi | |
if [ $i ~= /^d/ ] then | |
empty_the_dir($i) | |
fi | |
ago=$(echo $1 | awk '{print $5 $6}') | |
agoseconds=$(date +%s $ago) | |
now=$(date +%s) | |
if [ $(expr now - agoseconds) -gt 86400 ] && [ -n $notpreviouslyempty ] then | |
myfs rm fp | |
fi | |
} | |
for i in $(myfs lr myfs://mysever) | |
empty_the_dir($i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This took me almost 50 minutes to write. FML.