71 lines
1.2 KiB
Bash
Executable File
71 lines
1.2 KiB
Bash
Executable File
|
|
#!/bin/bash
|
|
|
|
function usage {
|
|
echo -n "Usage: ${0}"
|
|
echo -n " -d <root_dir>"
|
|
echo -n " -D <older than time criteria> (example: '2 days ago')"
|
|
echo
|
|
exit 1
|
|
}
|
|
|
|
echo Starting $0 $*
|
|
|
|
|
|
# ---------------- cmdline
|
|
while getopts "d:D:" opt; do
|
|
case ${opt} in
|
|
d )
|
|
RootDir=$OPTARG
|
|
;;
|
|
D )
|
|
DateCriteria=$OPTARG
|
|
;;
|
|
\? )
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
usage
|
|
;;
|
|
: )
|
|
echo "Option -$OPTARG requires an argument." >&2
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ "${RootDir}" == "" ] || [ "${DateCriteria}" == "" ] ; then
|
|
usage
|
|
fi
|
|
|
|
declare -A Settings=()
|
|
Settings[Src]=${RootDir}
|
|
Settings[PruneDate]=$(date -d "${DateCriteria}" '+%Y-%m-%d')
|
|
|
|
src=${Settings[Src]}
|
|
prune_date=${Settings[PruneDate]}
|
|
|
|
echo Before Pruning....
|
|
df -hT ${src}
|
|
|
|
echo "Finding files older than ${prune_date}..."
|
|
Cmd="find ${src} -type f ! -newermt \"${prune_date}\""
|
|
echo ${Cmd}
|
|
files=($(eval ${Cmd}))
|
|
echo "Total files to be pruned: ${#files[*]}"
|
|
|
|
for file in "${files[@]}" ; do
|
|
Cmd="rm -f ${file}"
|
|
echo ${Cmd}
|
|
eval ${Cmd}
|
|
done
|
|
|
|
echo "Removing empty directories..."
|
|
Cmd="find ${src} -type d -empty -print -delete"
|
|
echo ${Cmd}
|
|
eval ${Cmd}
|
|
|
|
echo After Pruning....
|
|
df -hT ${src}
|
|
|
|
echo $0 Done
|
|
|