2013-05-15 14:22:08 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2014-11-12 16:15:39 +01:00
|
|
|
_usage () {
|
|
|
|
cat<<EOF
|
2014-02-10 14:54:46 +01:00
|
|
|
Usage: ${1##*/} [OPTIONS] FOLDERS
|
|
|
|
|
|
|
|
Output index of folder hierarchies to stdout. 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.
|
2013-05-15 14:22:08 +02:00
|
|
|
|
2014-02-10 14:54:46 +01:00
|
|
|
Options:
|
2013-05-15 14:22:08 +02:00
|
|
|
|
2014-02-10 14:54:46 +01:00
|
|
|
-f: When outputting to a file, overwrite if it exists.
|
|
|
|
-w: Output each index to individual files in current folder. It uses the folder
|
|
|
|
name as basename.
|
2013-05-15 14:22:08 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:54:46 +01:00
|
|
|
opt_noclobber=true
|
|
|
|
opt_file=false
|
|
|
|
while getopts ":fhw" opt; do
|
2014-11-12 16:15:39 +01:00
|
|
|
case $opt in
|
|
|
|
f)
|
|
|
|
opt_noclobber=false ;;
|
|
|
|
h)
|
|
|
|
_usage "$0"
|
|
|
|
exit 0 ;;
|
|
|
|
w)
|
|
|
|
opt_file=true ;;
|
|
|
|
\?)
|
|
|
|
_usage "$0"
|
|
|
|
exit 1 ;;
|
|
|
|
esac
|
2013-05-15 14:22:08 +02:00
|
|
|
done
|
|
|
|
|
2016-06-23 23:18:36 +02:00
|
|
|
shift $((OPTIND - 1))
|
2013-05-15 14:22:08 +02:00
|
|
|
|
2013-08-14 02:33:34 +02:00
|
|
|
if [ $# -eq 0 ]; then
|
2014-11-12 16:15:39 +01:00
|
|
|
_usage "$0"
|
|
|
|
exit 1
|
2013-08-14 02:33:34 +02:00
|
|
|
fi
|
|
|
|
|
2014-02-10 14:54:46 +01:00
|
|
|
## 'realpath' is required in case argument is ending with '.' or '..'.
|
|
|
|
if ! command -v realpath >/dev/null 2>&1; then
|
2014-11-12 16:15:39 +01:00
|
|
|
echo "'realpath' not found in PATH. Exiting." >&2
|
|
|
|
exit 1
|
2014-02-10 14:54:46 +01:00
|
|
|
fi
|
2013-12-27 11:16:50 +01:00
|
|
|
|
2014-02-10 14:54:46 +01:00
|
|
|
for i ; do
|
2016-06-23 23:18:36 +02:00
|
|
|
[ ! -d "$i" ] && continue
|
2013-12-27 11:16:50 +01:00
|
|
|
|
2016-06-23 23:18:36 +02:00
|
|
|
## We strip "./" from find's listing since we don't need it. We could avoid
|
|
|
|
## printing it in the first place, but there are several shortcomings:
|
|
|
|
## - Find over '.*' and '*', is bad practice since if will fail on
|
|
|
|
## non-existing files or for files beginning with a dash.
|
|
|
|
## - The 'printf' command in find is for GNU find only.
|
2014-03-08 11:00:44 +01:00
|
|
|
|
2014-11-12 16:15:39 +01:00
|
|
|
## 'LC_ALL=C sort' is required to make sure to output is consistent across
|
|
|
|
## different systems.
|
2016-06-23 23:18:36 +02:00
|
|
|
|
|
|
|
## The two following lines do the same for the same time cost. The former is shorter.
|
|
|
|
# find "$i" -type f | awk -v str="$i" '{l=length(str)+2; print substr($0, l)}' | sort > "$OUTPUT"
|
2014-11-12 16:15:39 +01:00
|
|
|
(cd -- "$i" && find . -type f) | sed 's/^.\///' | LC_ALL=C sort | \
|
|
|
|
if $opt_file; then
|
|
|
|
i="$(realpath -- "$i")"
|
|
|
|
base="${i##*/}"
|
|
|
|
output="$base.index"
|
2016-06-23 23:18:36 +02:00
|
|
|
[ -e "$output" ] && $opt_noclobber && output="$base-$(date +%F-%T).index"
|
2014-11-12 16:15:39 +01:00
|
|
|
echo "$output"
|
|
|
|
cat > "$output"
|
|
|
|
else
|
|
|
|
cat
|
|
|
|
fi
|
2013-08-14 02:33:34 +02:00
|
|
|
done
|