ambevar-dotfiles/.scripts/dataindex

62 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
unset DATE
_printhelp ()
{
cat<<EOF
Usage: ${1##*/} [-f] FOLDERS
Create index of folder hierarchies. This is used as a small "backup" purpose. In
case of data loss, it is still possible to get the full file list thanks to the
indexes.
-f: Overwrite if output exists.
EOF
}
OPT_NOCLOBBER=true
while getopts ":dfh" opt; do
case $opt in
h)
_printhelp "$0"
exit 0
;;
f)
OPT_NOCLOBBER=false
;;
?)
_printhelp "$0"
exit 1
;;
:)
_printhelp "$0"
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_printhelp "$0"
exit 1
fi
for i ; do
base="$(basename "$i")"
[ ! -d "$i" ] && continue
OUTPUT="$base.index"
[ -e "$OUTPUT" ] && $OPT_NOCLOBBER && OUTPUT="$base-$(date +%F-%T).index"
echo "$OUTPUT"
## Find over '.*' and '*', is bad practice since if will fail if no existing
## files or for files beginning with a dash. The 'printf' command in find is
## for GNU find only.
## The two following lines do the same for the same time cost. The former is shorter.
(cd "$i" && find . -type f) | sed 's/^.\///' | sort > "$OUTPUT"
# find "$i" -type f | awk -v str="$i" '{l=length(str)+2; print substr($0, l)}' | sort > "$OUTPUT"
done