shtools/zdate/zdate
2020-10-26 11:26:13 +01:00

50 lines
1 KiB
Bash
Executable file

#!/bin/sh
fname="$(basename "$0")"
usage()
{
echo "$fname [options] [path...]
Display latest date of modification of any item in the path
Options:
-v Invert, display oldest instead
-s Format to UNIX epoch
-F <format> Use the following date format. See 'man date'
"
}
unset opt_s opt_v date_format
while getopts ":hsvF:" opt;
do
case $opt in
h) usage ; exit 0 ;;
s) opt_s=y ;;
v) opt_v=y ;;
F) date_format=$OPTARG ;;
*) echo "Unknown option: $OPTARG" >&2 ; usage ; exit 1 ;;
esac
done
shift $((OPTIND-1))
targets="."
[ $# -gt 0 ] && targets="$*"
if [ -n "$opt_v" ] ; then
ret=$(find $targets -printf '%T@\n' | sort -n | head -n1 | cut -d'.' -f1)
else
ret=$(find $targets -printf '%T@\n' | sort -n | tail -n1 | cut -d'.' -f1)
fi
if [ -n "$opt_s" ]
then
true # do nothing: already in seconds
elif [ -n "$date_format" ] ; then
ret=$(date --date="$date_format" 2>/dev/null)
else # resolve to default date
ret=$(date --date="@$ret" 2>/dev/null)
fi
[ -n "$ret" ] && echo $ret