De acordo com as Leis 12.965/2014 e 13.709/2018, que regulam o uso da Internet e o tratamento de dados pessoais no Brasil, ao me inscrever na newsletter do portal DICAS-L, autorizo o envio de notificações por e-mail ou outros meios e declaro estar ciente e concordar com seus Termos de Uso e Política de Privacidade.
Colaboração: Jansen Sena
Data de Publicação: 14 de Outubro de 2006
Em muitas oportunidades, administradores de servidores FTP e HTTP que dispobinilizam arquivos para download, necessitam disponibilizar o hash MD5 de cada arquivo disponibilizado para download. Essa é uma alternativa simples e interessante que permite com que o usuário verifique, após finalizar o download, se o arquivo copiado está íntegro e de acordo com o original disponibilizado no servidor.
Entretanto, após criar um novo diretório com vários arquivos a serem disponibilizados, gerar o hash MD5 para cada um pode ser uma tarefa repetitiva. Mesmo sendo possível resolver esse problema com uma única linha de comando, combinando alguns "pipes" ou um simples laço "for" em shell script, essa saída nem sempre é tão simples fundamentalmente para administradores menos experientes e não tão ambientados com os recursos mais avançados da shell.
Para resolver esse problema, você pode utilizar um simples shell
script que batizei de mkmd5sum4me.sh.
Basta executá-lo passando como
parâmetro os diretórios cujos arquivos devem ter os hashes MD5
criados. Em outras palavras, o script criará para cada arquivo de cada
diretório repassado um arquivo com o mesmo nome do original adicionado
da extensão ".md5sum" contendo o seu hash. Por exemplo:
$ mkmd5sum4me.sh /var/www/downloads/distros /var/www/myfiles
#!/bin/bash # # #Copyright Holder: Jansen Sena# #License: # # This package is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This package is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this package; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA. # # ################## # System utilities # ################## # MD5SUM="/usr/bin/md5sum" LS="/bin/ls" ECHO="/bin/echo" ALL_UTILS="MD5SUM LS ECHO" # # ############# # DEFINITIONS # ############# # MD5FILE_SUFFIX="md5sum" SYNTAX_MSG="Usage: mkmd5sum4me.sh [ [ ...]]" # # ################################### # Checking the number of parameters # ################################### # if [ $# -eq 0 ] ; then $ECHO $SYNTAX_MSG exit 2 fi # # ######################################################### # Checking if all the utilities aer present in the system # ######################################################### # for util in $ALL_UTILS ; do eval util_aux=\$$util if [ ! -x $util_aux ] ; then $ECHO "The $util_aux file is not present in your system or it doesn't have execution permission." $ECHO "Aborting." exit 1 fi done # # ########### # FUNCTIONS # ########### # # # This function check if a directory exists and if there is execute permission on it. # Syntax: CHECK_DIRECTORY # function CHECK_DIRECTORY() { DIRECTORY=$1 TYPE=$2 if [ ! -d $DIRECTORY ] ; then $ECHO "The $DIRECTORY $TYPE directory does not exist or it is not a directory." $ECHO "Aborting." exit 1 elif [ ! -x $DIRECTORY ] ; then $ECHO "The $DIRECTORY $TYPE directory does not have execute permission." $ECHO "Aborting." exit 1 fi } # # ###################### # Checking directories # ###################### # for dir in $* ; do CHECK_DIRECTORY $dir "" done # # ######################################################## # Calculting the md5 for all the files in each directory # ######################################################## # for dir in $* ; do cd $dir FILES=`ls` # # To be solved: it will not work with there is some directory including white spaces. # $ECHO -n "Calculating MD5 sum for $dir directory..." for file in $FILES ; do $MD5SUM $file > $file.$MD5FILE_SUFFIX 2>/dev/null if [ $? -ne 0 ] ; then $ECHO "There was some problem during the md5 calculation for $file in $dir directory." $ECHO "Ignoring." fi done $ECHO "done." done
Bem, espero que seja útil algum dia.
Saudações livres!
Jansen Sena
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comentários