Advertisement
Guest User

Sanitizing script

a guest
Jun 3rd, 2017
3,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.60 KB | None | 0 0
  1. #!/bin/bash
  2. #This script will rename files for the given directory. Removing all illegal characters.
  3. #Usage ./Script.sh /directory/to/run
  4. verbose=0
  5.  
  6. #Set/clear variables
  7. folder=$1
  8. loop=0
  9. loopnum=0
  10. #Rotate Logs
  11. logfile=~/Library/Logs/sanitizelog
  12. archlog=~/Library/Logs/sanitizelogarch
  13. progress=/private/var/tmp/sanitizeprogress
  14. cat $logfile >> $archlog
  15. rm $logfile
  16. touch $logfile
  17. touch $progress
  18.  
  19. osascript   -e 'tell application "Terminal" to do script "tail -f ~/Library/Logs/sanitizelog"'\
  20.             -e 'tell application "Terminal" to do script "tail -f /private/var/tmp/sanitizeprogress"'\
  21.             -e 'tell application "Terminal" to activate'\
  22.             -e 'tell application "Terminal" to set position of window 1 to {1, 1}'\
  23.             -e 'tell application "Terminal" to set position of window 2 to {600, 1}'
  24.  
  25. sleep 2
  26. date=$(date)
  27. echo Sanitize Script >> $logfile
  28. echo Script run date: $date >> $logfile
  29. echo >> $logfile
  30.  
  31. #Only line break on new line
  32. OIFS="$IFS"
  33. IFS=$'\n'
  34.  
  35. while [ $loop = 0 ]; do
  36.     for fulldir in $(find $folder | grep -v ".DS_Store"); do
  37.         old=$fulldir #$(echo $fulldir | sed -e 's/[ ]/\\ /g')
  38.         file=$(basename "$old")
  39.         rootdir=$(dirname "$fulldir")
  40.         extension=$(echo $file | awk -F . '{print $NF}')
  41.         noext=0
  42.         if [ $file = $extension ]; then
  43.             noext=1
  44.         fi      
  45.         # Swap Syntax  file=$(echo $file | sed -e 's/[FIND]/REPLACE/g')
  46.         # Remove #%*\/?
  47.         file=$(echo $file | sed -e 's/[#%*\\\/?\"]//g')
  48.         # Repeat of above line with " and ' inverted
  49.         file=$(echo $file | sed -e "s/[#%*\\\/?\']//g")
  50.         # Swap : and | for _
  51.         file=$(echo $file | sed -e 's/[:|]/_/g')
  52.         # Swap & with and
  53.         file=$(echo $file | sed -e 's/[&]/and/g')
  54.         # Swap < { with (
  55.         file=$(echo $file | sed -e 's/[<{]/(/g')
  56.         # Swap > } with )
  57.         file=$(echo $file | sed -e 's/[}>]/)/g')
  58.         # Swap ~ with -
  59.         file=$(echo $file | sed -e 's/[~]/-/g')
  60.         #escape trailing *
  61.         file=$(echo $file | sed -e 's/[*]/\\*/g')
  62.         #remove extension
  63.         file="${file%.*}"
  64.         #remove trailing and leading spaces
  65.         file=$(echo $file | xargs )
  66.         #remove trailing .
  67.         file=$(echo $file | sed -e 's/\.$//g')
  68.         #remove trailing and leading spaces again
  69.         file=$(echo $file | xargs )
  70.         #remove leading .
  71.         file=$(echo $file | sed 's/\.*//')
  72.         file2=$(basename $old)
  73.         if [ $noext = 1 ]; then
  74.             echo file has no extension
  75.         else
  76.             #re add extension if not a directory
  77.             file=$file.$extension
  78.         fi
  79.         # Construct new file path
  80.         new=$rootdir/$file
  81.         #Rename
  82.         if [ $old != $new ]; then
  83.             mv $old $new
  84.             echo renamed $old to $new >> $logfile
  85.         fi
  86.         if [ $verbose = 1 ]; then
  87.             echo "fulldir:$fulldir"
  88.             echo "file:$file"
  89.             echo "rootdir: $rootdir"
  90.             echo "old: $old" >> $progress
  91.             echo "new: $new" >> $progress
  92.             echo "extension: $extension"
  93.         fi
  94.         lastline=$(tail -n 1  ~/Library/Logs/sanitizelog)
  95.         echo Processed file $file >> $progress
  96.     done
  97.     if [ $lastline = "loopcomplete" ]; then
  98.         loop=1
  99.         echo Last loop modified 0 files - Exiting >> $progress
  100.     fi
  101.  
  102.     loopnum=$((loopnum+1))
  103.     echo Loop# $loopnum complete >> $logfile
  104.     echo loopcomplete >> $logfile
  105. done
  106. echo "
  107.  
  108. ----------Script Complete----------
  109. You can now close this window" >> $logfile
  110. killall tail
  111. rm $progress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement