Skip to content

Instantly share code, notes, and snippets.

@dmslabsbr
Last active November 5, 2023 02:32
Show Gist options
  • Save dmslabsbr/349f62ec7145c64a35b466b7edc71316 to your computer and use it in GitHub Desktop.
Save dmslabsbr/349f62ec7145c64a35b466b7edc71316 to your computer and use it in GitHub Desktop.
This guide provides a clear overview of what the script does, how to use it, and what to expect from it. Additionally, remember to adapt the license section as needed to suit the actual license under which you are releasing the script.
#!/bin/bash
# Nome: CompareSubdirSizesProgress.sh
# Defina o diretório padrão ou aceite via argumentos de linha de comando
DIR1=${1:-"/Users/dms/Library/CloudStorage/Dropbox"}
DIR2=${2:-"/Volumes/hd4tb/BKP_do_MAC/Dropbox"}
# Função para calcular o tamanho dos subdiretórios
get_dir_size() {
du -sm "$1" | cut -f1
}
# Função para comparar tamanhos de diretórios
compare_sizes() {
local size1=$(get_dir_size "$1")
local size2=$(get_dir_size "$2")
if [ "$size1" != "$size2" ]; then
echo "Diferença encontrada: $1 (${size1}MB) | $2 (${size2}MB)"
fi
}
# Função para verificar se o diretório é um link simbólico
check_if_symlink() {
if [ -L "$1" ]; then
echo "Erro: '$1' é um link simbólico. Por favor, use o caminho real." >&2
exit 1
fi
}
# Verificar se DIR1 ou DIR2 são links simbólicos
check_if_symlink "$DIR1"
check_if_symlink "$DIR2"
# Listar todos os subdiretórios de DIR1 e DIR2
subdirs1=$(find "$DIR1" -mindepth 1 -maxdepth 1 -type d | sort)
subdirs2=$(find "$DIR2" -mindepth 1 -maxdepth 1 -type d | sort)
# Comparar as listas de subdiretórios
comm -3 <(echo "$subdirs1") <(echo "$subdirs2") | while read subdir; do
# Verifica se a linha pertence ao DIR1 ou DIR2
if [[ $subdir == "$DIR1"* ]]; then
# Removendo o caminho do DIR1 para verificar se o subdiretório existe no DIR2
subpath="${subdir#$DIR1/}"
if [ ! -d "$DIR2/$subpath" ]; then
size=$(get_dir_size "$subdir")
echo "Apenas em $DIR1: $subpath (${size}MB)"
fi
elif [[ $subdir == "$DIR2"* ]]; then
# Removendo o caminho do DIR2 para verificar se o subdiretório existe no DIR1
subpath="${subdir#$DIR2/}"
if [ ! -d "$DIR1/$subpath" ]; then
size=$(get_dir_size "$subdir")
echo "Apenas em $DIR2: $subpath (${size}MB)"
fi
fi
done
# Para subdiretórios que existem em ambos os diretórios, compare seus tamanhos
comm -12 <(echo "$subdirs1") <(echo "$subdirs2") | while read common_subdir; do
# Obter caminhos completos para o subdiretório em ambos os diretórios
subdir_path1="$DIR1/${common_subdir#$DIR1/}"
echo "subdir1: $subdir_path1"
subdir_path2="$DIR2/${common_subdir#$DIR2/}"
echo "subdir2: $subdir_path2"
echo "Comparando $subdir_path1 e $subdir_path2..."
compare_sizes "$subdir_path1" "$subdir_path2"
done
@dmslabsbr
Copy link
Author

with error

Subdirectory Comparison and Size Script

This Bash script is used to compare subdirectories and their sizes between two provided directories. It checks for the presence of subdirectories in both directories and compares their sizes in megabytes. If the subdirectories exist in both but have different sizes, the script will print a message indicating the discrepancy. Furthermore, the script will also check and report if any of the provided directories is a symbolic link, issuing an error message and terminating the execution.

Features

  • Checks for the existence of subdirectories across both input directories.
  • Compares the size of corresponding subdirectories.
  • Detects and alerts about symbolic links, avoiding comparison of non-real paths.
  • Outputs the size of the subdirectories in megabytes during the comparison.
  • Provides detailed output on unique subdirectories and size differences.

Prerequisites

To run this script, you will need:

  • An operating system that supports Bash scripts (like Linux, macOS, or a tool like Cygwin for Windows).
  • Read access to the directories you want to compare.

Usage

You can run the script directly from the terminal. If no arguments are provided, the script will use default paths for comparison. You can provide your own paths as arguments:

./CompareSubdirSizesProgress.sh [path_to_dir1] [path_to_dir2]

Important Note

This script does not follow symbolic links due to the implemented security check. If you want to compare directories that a symbolic link points to, please provide the actual directory path.

License

This script is provided "As is," without any kind of warranty. Feel free to use, modify, and distribute it as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment