useful-scripts/moveFiles.sh

61 lines
1.5 KiB
Bash
Raw Permalink Normal View History

2024-09-13 12:57:54 +00:00
#!/bin/bash
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$parent_path"
SERVER_IP=friday
DOWNLOADS=""
MOUNT=""
DELETE_EMPTY=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--downloads)
DOWNLOADS="$2"
shift 2
;;
--mount)
MOUNT="$2"
shift 2
;;
--delete-empty)
DELETE_EMPTY=true
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
if [ -z "$DOWNLOADS" ] || [ -z "$MOUNT" ]; then
echo "Usage: $0 --downloads <downloads_path> --mount <mount_path> [--delete-empty]"
exit 1
fi
# Extract the folder name from the last part of the MOUNT path
FOLDER_NAME=$(basename "$MOUNT")
ping -c1 -W2 "$SERVER_IP" > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
if mountpoint -q "$MOUNT"; then
echo "$FOLDER_NAME mounted. Moving files." | ts '[%Y-%m-%d %H:%M:%S]'
files=$(ls "$MOUNT") # Trigger drive spin up
sleep 5
output=$(rsync -avzh --remove-source-files --stats --exclude '.gitkeep' "$DOWNLOADS" "$MOUNT" | grep -A 1 'files transferred')
files=$(echo "$output" | awk '/files transferred/{print $NF}')
if [ "$DELETE_EMPTY" = true ]; then
find "$DOWNLOADS" -type d -empty -delete
fi
if [[ "$files" != 0 ]]; then
echo "Transferred $files files" | ts '[%Y-%m-%d %H:%M:%S]'
./slackit.sh "$output" "$FOLDER_NAME File Transfer"
fi
fi
fi