-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmakedrivetree
executable file
·72 lines (63 loc) · 3.55 KB
/
makedrivetree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
# a script to write the contents of a directory in tree form and save it to a .txt file
# load nmaahcmmfunctions into this script
SCRIPT_PATH="${0%/*}"
. "${SCRIPT_PATH}/nmaahcmmfunctions"
[[ -f "${SCRIPT_PATH}/nmaahcmmfunctions" ]] || { echo "Missing '${SCRIPT_PATH}/nmaahcmmfunctions'. Exiting." ; exit 1 ;};
_initialize_make # safe script termination process defined in nmaahcmmfunctions
### USAGE
_usage(){
echo
echo "$(basename "${0}")"
echo "This application will write the contents of a drive directory in tree form and save it to a .txt file. It takes one or more drives as input."
echo "The output is sent to the directory set in nmaahcmmconfig ('directory where trees of your drives will be created'). This directory is currently set to: ${DRIVE_CONTENTS_DESTINATION}"
echo "Usage: $(basename ${0}) /path/to/drive1 [ /path/to/drive2 ... ] "
echo
exit
}
[ "${#}" = 0 ] && _usage # if the command is run with no arguments then _usage is called
# getopts loop
OPTIND=1
while getopts ":h" OPT; do
case "${OPT}" in
h) _usage ;; # if the operator runs "[scriptname] -h" then the _usage text above will display in the terminal
*) echo "Invalid option -${OPTARG}" ; _usage ;; # if the operator tries to use an option other than the ones listed above, the _usage text will display in the terminal
esac
done
shift $(( ${OPTIND} - 1 ))
### FUNCTIONS
# log script beginning
_log -b
while [ "${*}" != "" ] ; do # loop over all the drives provided by the operator
DRIVE="${1}"
if [[ -d "${DRIVE}" ]] ; then # if the drive structure is recognized as a directory:
DRIVE_BASENAME="$(basename "${DRIVE}")"
OUTPUT="${DRIVE_CONTENTS_DESTINATION}/${DRIVE_BASENAME}_contents_${date}.txt"
if [[ -d "${DRIVE_CONTENTS_DESTINATION}" ]] ; then # if the output's parent DRIVE_CONTENTS_DESTINATION structure is recognized as a directory:
tree --filelimit 50 --si --du -U -Q -o "${OUTPUT}" "${DRIVE}" # create a tree of the drive's contents
if grep "0 directories, 0 files" "${OUTPUT}" ; then # if the results of "tree," sent to the output text file, show that the drive was empty:
_report -g "${DRIVE} is empty. Adding EMPTY to file name."
OUTPUT_EMPTY="${DRIVE_CONTENTS_DESTINATION}/${DRIVE_BASENAME}_contents_${date}_EMPTY.txt" && mv -v "${OUTPUT}" "${OUTPUT_EMPTY}"
_report -g "Moving ${OUTPUT_EMPTY} to ${DRIVE}" # tell operator the tree file is being moved to the drive
cp -v "${OUTPUT_EMPTY}" "${DRIVE}" # copy the output text file to the drive: -v=verbose
else
_report -g "Moving ${OUTPUT} to ${DRIVE}" # tell operator the tree file is being moved to the drive
cp -va "${OUTPUT}" "${DRIVE}" # copy the output text file to the drive: -v=verbose, -a="archive"
fi
else
_report -rt "ERROR: ${DRIVE_CONTENTS_DESTINATION} does not exist. Exiting..." >&2 # tell operator if the hardcoded "local DRIVE_CONTENTS_DESTINATION" cannot be found - e.g. no longer exists or is moved
_log -a "Process terminated by script (no directory at requested location)."
exit 1
fi
else
_report -rt "ERROR: ${DRIVE} does not exist. Exiting..." >&2 # tell operator if the drive cannot be found
_log -a "Process terminated by script (no drive at requested location)."
exit 1
fi
shift
done
## SCRIPT ACTIONS
# log script ending
_log -e
_report -g "makedrivetree process complete. Output tree can be viewed at ${OUTPUT}"
exit "$?"