shtools/zdate/zdate
2023-06-22 12:14:41 +02:00

52 lines
1.1 KiB
Bash
Executable file

#!/bin/sh
[ "$DEBUG" = true ] && set -x
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 "$@" -printf '%T@\n' | sort -n | head -n1 | cut -d'.' -f1)
else
ret=$(find "$@" -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="@$ret" "$date_format" 2>/dev/null)
else # resolve to default date
ret=$(date --date="@$ret" 2>/dev/null)
fi
[ -n "$ret" ] && echo "$ret"