Created
August 19, 2023 06:04
-
-
Save Techcable/9265ad4181bec5b84ddcd75121263330 to your computer and use it in GitHub Desktop.
Simple shell script to list files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ "$#" -ne 2 ]]; then | |
echo "ERROR: Invalid number of arguments" >&2; | |
echo >&2; | |
echo "Usage: ./listfiles.sh <directory> <outfile>" >&2; | |
exit 1; | |
fi | |
TARGET_DIR="$1"; | |
OUTPUT_FILE="$2"; | |
if [[ ! -d "$TARGET_DIR" ]]; then | |
echo "ERROR: Target is not a directory: $TARGET_DIR" >&2; | |
exit 1; | |
fi | |
confirm() { | |
local prompt="$1"; | |
local default; | |
case "$2" in | |
true | True | yes) | |
default="True"; | |
;; | |
false | False | no) | |
default="False"; | |
;; | |
"" | None) | |
default="None"; | |
;; | |
*) | |
echo "ERROR: Invalid default \`$2\`" >&2; | |
exit 1; | |
;; | |
esac | |
python3 -c "import sys, rich.prompt; exit(not rich.prompt.Confirm.ask(sys.argv[1], default=$default))" "$prompt"; | |
return $?; | |
} | |
if [[ -f "$OUTPUT_FILE" ]]; then | |
echo "WARNING: Output file \`$OUTPUT_FILE\` already exists!"; | |
echo | |
if ! confirm "Do you want to override it?" no; then | |
exit 1; | |
fi | |
fi | |
export ABS_OUTPUT_FILE="$(realpath "$OUTPUT_FILE")" | |
pushd "$TARGET_DIR" || exit | |
echo "# List of files in $(pwd) at $(TZ=UTC date +'%F %T')" > "$ABS_OUTPUT_FILE" | |
fd --strip-cwd-prefix -uuu . | sort >> "$ABS_OUTPUT_FILE" | |
popd | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment