Advertisement
gheja

movesync.sh

Oct 18th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.37 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. sumfile_source="/tmp/md5sum_remote.txt"
  4. sumfile_current="/tmp/md5sum_current.txt"
  5.  
  6. dir_current="/tmp/a_current"
  7. dir_new="/tmp/a_new"
  8.  
  9. cat $sumfile_source | while read line; do
  10.     ### md5
  11.     sum=${line:0:32}
  12.     filename=${line:34}
  13.     filename_current=""
  14.    
  15.     # ### sha1
  16.     # sum=${line:0:40}
  17.     # filename=${line:42}
  18.    
  19.     # ### sha256
  20.     # sum=${line:0:64}
  21.     # filename=${line:66}
  22.    
  23.     if [ ! -e "${dir_new}/${filename}" ]; then
  24.         # check in the "current" dir
  25.         a=`cat $sumfile_current | grep -E "^${sum}  " | cut -d ' ' -f 3- | head -n 1`
  26.        
  27.         if [ "$a" != "" ] &&[ -e "${dir_current}/${a}" ]; then
  28.             filename_current="${dir_current}/${a}"
  29.             move=1
  30.         else
  31.             # check if a file with this sum was already moved
  32.             a=`cat $sumfile_source | grep -E "^${sum}  " | cut -d ' ' -f 3- | head -n 1`
  33.            
  34.             if [ "$a" != "" ] && [ -e "${dir_new}/${a}" ]; then
  35.                 filename_current="${dir_new}/${a}"
  36.                 move=0
  37.             fi
  38.         fi
  39.        
  40.         if [ "$filename_current" == "" ]; then
  41.             echo "WARNING: \"$sum  $filename\": missing from \"current\" and \"new\" location, skipping."
  42.             continue
  43.         fi
  44.        
  45.         # echo "${filename_current} -> ${dir_new}/${filename}"
  46.        
  47.         dir=`dirname "${dir_new}/${filename}"`
  48.         mkdir -p "${dir}"
  49.        
  50.         if [ $move == 1 ]; then
  51.             mv -v "${filename_current}" "${dir_new}/${filename}"
  52.         else
  53.             cp -xarv "${filename_current}" "${dir_new}/${filename}"
  54.         fi
  55.     fi
  56. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement