Created
February 28, 2024 07:29
-
-
Save steffen-wirth/a4b00b2d0f3a1509f280535550426470 to your computer and use it in GitHub Desktop.
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
#https://unix.stackexchange.com/questions/570743/cp-rsync-overwrite-if-smaller-backup-original-and-then-overwrite-if-larger | |
# get the size and filnames of files in '/tmp/A' directory and loop through each file found | |
ls /tmp/A | while read filename | |
do | |
# get the size of file in 'A' directory | |
sizeA=$( ls -l "/tmp/A/${filename}" | awk '{print $5}') | |
# get the size of corresponding file in 'B' directory | |
sizeB=$(ls -l "/tmp/B/${filename}" | awk '{print $5}') | |
# compare file sizes and perform appropriate action | |
if [ ${sizeB} -gt ${sizeA} ] | |
then | |
echo "file in B named \"${filename}\" is larger than A file" | |
# Backup and overwrite the file | |
cp "/tmp/B/${filename}" /tmp/C/ | |
cp -f "/tmp/A/${filename}" /tmp/B/ | |
else | |
if [ ${sizeB} -lt ${sizeA} ] | |
then | |
echo "file in B named \"${filename}\" is smaller than A file" | |
# overwrite the file | |
cp -f "/tmp/A/${filename}" /tmp/B/ | |
else | |
echo "The files \"${filename}\" are the same size" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment