Skip to content

Instantly share code, notes, and snippets.

@SrChach
Created October 28, 2019 18:51
Show Gist options
  • Save SrChach/9278c61b65e555a965d4706594ae0af8 to your computer and use it in GitHub Desktop.
Save SrChach/9278c61b65e555a965d4706594ae0af8 to your computer and use it in GitHub Desktop.
G214 shell file for find lost images and move it to main folder
# If you don't set "_usuario" and "_pass" for DB, this script won't work
# constants setted
_usuario=""
_pass=""
_input="images.txt"
_search_in="../Evidencia"
_carpeta_puente="__ejemplo"
_carpeta_salida="../storage/images"
_limit="LIMIT 5"
_listado_rescatadas="rescatadas.txt"
# Clean from past executions
rm -rf $_input $_carpeta_puente $_listado_rescatadas
mkdir $_carpeta_puente
# getting url from images from mysql
mysql --user="$_usuario" --password="$_pass" -e \
"SELECT URLIMAGEN FROM CFELUMINARIAS.IMAGEN WHERE EXISTE_OS=0 $_limit" \
| tail -n +2 > $_input
# For each image found in mysql not founded here, this
while IFS= read -r line
do
# (find "$_search_in" -name "$line" -print &) | (head -n 1) >> "$_carpeta_puente/encontradas.txt"
(find "$_search_in" -name "$line" -print &) |
(head -n 1) | xargs -I '{}' cp '{}' $_carpeta_puente
done < $_input
ls $_carpeta_puente/ > $_listado_rescatadas
while IFS= read -r line
do
mv $_carpeta_puente/$line $_carpeta_salida &&
mysql --user="$_usuario" --password="$_pass" -e \
"UPDATE CFELUMINARIAS.IMAGEN SET EXISTE_OS=2 WHERE URLIMAGEN='$line'" &&
echo "$line" >> movidas.txt
done < $_listado_rescatadas
# clean residual files
rm -rf $_input $_listado_rescatadas $_carpeta_puente
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment