He aquí una idea para ti. Aunque no ha sido probado.
La idea es separar los archivos y directorios recién descomprimidos para cada archivo fuente único en su propia carpeta temporal. Esto se repite recursivamente, y cuando la recurrencia regresa desde las profundidades, los archivos y directorios se mueven a su destino adecuado y se elimina el directorio temporal.
function vacatetmp () {
echo "Vacating TMP folder ${1}...."
# You need a prefix for the new auxiliary temporary folders.
prefix=SomeGoodPrefix
for i in "$1"/*; do
if [ -f "$i" ]; then
# From your explanation:
# "Look up the mime type of "$i" and unzip the files into "$1"."
# Now, before you mix the new files and dirs with the old ones,
# unzip them to a special new directory with the prefix and the file name
# so that they're not mixed with the files and directories already present in "$1".
mkdir "$1"/"${prefix}""$i" &&
# How do you pass the target directory to "caseit"?
# However you do, pass it the temporary folder.
caseit "$i" "$1"/"${prefix}""$i" &&
# Now the new unzipped files and folders are in "$1"/"${prefix}""$i", so
# vacatetmp the auxiliary prefixed directory:
vacatetmp "$1"/"${prefix}""$i" &&
# and after you've finished with vacatetmp-ing that directory,
# move the contents of it to "$1".
mv "$1"/"${prefix}""$i"/* "$1" &&
# And finally remove the prefixed directory.
rmdir "$1"/"${prefix}""$1"
# This file is done. Recursively.
elif [ -d "$i" ]; then
vacatetmp "$i"
fi
done
}
Avísame si me equivoqué en alguna parte. No ha sido probado, como he advertido antes.
Iterar sobre los archivos primero, abrirlos y luego iterar sobre los directorios.
for i in "$1/*"; do [[ -f "$i" ]] && caseit "$i"; done;
for i in "$1/*"; do [[ -d "$i" ]] && vacatetmp "$i"; done
Sería más completo llamar también a vacatetmp()
desde dentro de caseit()
, al final. Pero dudo que sea necesario, y conduciría a un código menos mantenible.