Skip to content

Instantly share code, notes, and snippets.

@Dalethium
Last active January 4, 2024 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dalethium/1aa3acb994244376d142d3f7cfdb54c9 to your computer and use it in GitHub Desktop.
Save Dalethium/1aa3acb994244376d142d3f7cfdb54c9 to your computer and use it in GitHub Desktop.
Move And Copy
#!/bin/bash
# Check if both SRC and DST parameters are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 SRC DST"
exit 1
fi
SRC="$1"
DST="$2"
SRC="${SRC%/}"
DST="${DST%/}"
# Function to handle script interruption (Ctrl+C)
handle_interrupt() {
echo "Script interrupted by user. Exiting."
exit 1
}
# Set trap for SIGINT (Ctrl+C)
trap handle_interrupt SIGINT
# Navigate to the source directory or exit if it fails
cd "$SRC" || {
echo "Failed to cd into $SRC"
exit 1
}
# Replicate the directory structure from source to destination with permissions 777.
sudo find . -type d -exec bash -c '
for dir; do
sudo mkdir -p "'"$DST"'/${dir#./}" && sudo chmod 755 "'"$DST"'/${dir#./}"
done
' bash {} +
# Function to convert size to MB or GB
convert_size() {
local size=$1
if [ "$size" -lt 1048576 ]; then
echo "$(bc <<<"scale=2; $size/1024") KB"
elif [ "$size" -lt 1073741824 ]; then
echo "$(bc <<<"scale=2; $size/1048576") MB"
else
echo "$(bc <<<"scale=2; $size/1073741824") GB"
fi
}
# Calculate the total size of all files in bytes
total_size=$(du -cb "$(echo "$SRC")" | grep total | awk '{print $1}')
echo "Total size to transfer: $(convert_size $total_size)"
# Move files if the source file is larger or if the destination file does not exist.
find . -type f -exec bash -c '
handle_interrupt() {
echo "Interrupted. Exiting."
exit 1
}
convert_size() {
local size=$1
if [ "$size" -lt 1048576 ]; then
echo "$(bc <<< "scale=2; $size/1024") KB"
elif [ "$size" -lt 1073741824 ]; then
echo "$(bc <<< "scale=2; $size/1048576") MB"
else
echo "$(bc <<< "scale=2; $size/1073741824") GB"
fi
}
src_size=0
trap handle_interrupt SIGINT
method() {
file="$1"
dest_file="'"$DST"'/${file#./}"
if [[ -e "$dest_file" ]]; then
src_size=$(stat -c%s "$file")
dst_size=$(stat -c%s "$dest_file")
if ((src_size > dst_size)); then
echo "Source file $file is larger than destination. Moving and overwriting."
sudo mv -fv "$file" "$dest_file" && sudo chmod 755 "$dest_file"
else
#echo "Destination file $dest_file exists and is not smaller than the source. Skipping."
echo "Destination file $dest_file exists and is not smaller than the source. Removing source file."
sudo rm -v "$file" # Remove the source file since the destination is not smaller
fi
else
echo "Moving file $file to destination."
sudo mv -nv "$file" "$dest_file" && sudo chmod 755 "$dest_file"
fi
}
count=-1
total_size='"$total_size"'
for file in "$@"; do
if [ "$count" -lt 0 ]; then
count=$#
fi
method "$file"
# Update and display remaining size
if [[ -n "$src_size" ]]; then
total_size=$(($total_size - $src_size))
fi
count=$((count - 1))
echo "Remaining size to transfer: $(convert_size $total_size) in $count files"
done
' bash {} +
# Clean up empty directories from the source.
sudo find . -mindepth 1 -type d -empty -exec sudo rmdir {} +
cd ../ && [ -z "$(ls -A "$SRC")" ] && rm -rf "$SRC"
echo "All operations completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment