Advertisement
killermist

spring-clean.sh

May 22nd, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.11 KB | None | 0 0
  1. #!/bin/bash
  2. # spring cleaning v0.0.1
  3. # written and otherwise cobbled together by:
  4. # killermist (killermist@gmail.com)
  5.  
  6. #Imports
  7. SELFLINK="$0"
  8. FILENAME="$1"
  9.  
  10. #Constants
  11. DEBUG="true"  #should probably turn this into a "-v" command-line option
  12. HASHDIR=".hashes"
  13. NESTINGCODE="NeStCoDe..nEsTcOdE--blah"  #Should be something fairly long and unlikely to be used as a filename
  14.  
  15. if [ "$FILENAME" = "" ]; then  #Let's get this out of the way early.
  16.   echo "Base path necessary.  Aborting."
  17.   exit
  18. fi
  19.  
  20. #too many if-degug tests.  Let's clean that up.
  21. function debug {
  22. if [ $DEBUG = "true" ]; then
  23.   if [ "$1" = "sleep" ]; then sleep 1; else echo -e $*; fi
  24. fi
  25. }
  26.  
  27. function trymkdir {
  28. if [ ! -d "$1" ]; then mkdir "$1"; fi
  29. if [ ! -d "$1" ]; then echo "$1" did not exist and failed to be created.  Aborting; exit; fi
  30. }
  31.  
  32. #filename disambiguation suite
  33. # as acquired from http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
  34. # included both to cite source and for reference later, if needed
  35. : <<'extract-filename-and-extension-in-bash'
  36. #======================================================================================================================
  37. #!/bin/bash
  38. for fullpath in "$@"
  39. do
  40.     filename="${fullpath##*/}"                      # Strip longest match of */ from start
  41.     dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
  42.     base="${filename%.[^.]*}"                       # Strip shortest match of . plus at least one non-dot char from end
  43.     ext="${filename:${#base} + 1}"                  # Substring from len of base thru end
  44.     if [[ -z "$base" && -n "$ext" ]]; then          # If we have an extension and no base, it's really the base
  45.         base=".$ext"
  46.         ext=""
  47.     fi
  48.     echo -e "$fullpath:\n\tdir  = \"$dir\"\n\tbase = \"$base\"\n\text  = \"$ext\""
  49. done
  50. #======================================================================================================================
  51. extract-filename-and-extension-in-bash
  52.  
  53. debug
  54. debug "========================"
  55. debug "$FILENAME:\n\tdir  = \"$FDSDIR\"\n\tbase = \"$FDSBASE\"\n\text  = \"$FDSEXT\""
  56.  
  57. trymkdir "$HASHDIR"
  58.  
  59. debug sleep
  60. debug "$FILENAME" should be filename or target directory
  61.  
  62. if [ "$2" = "$NESTINGCODE" ] #testing to see if self-summoned
  63. then
  64.   if [ -d "$FILENAME" ]; then debug echo running nested, skip directories; exit; fi
  65.  
  66.   #For safety/sanity, verify that filename passed does actually qualify as a file.
  67.   if [ ! -f "$FILENAME" ]; then echo "$FILENAME" is not a file.  Skipping.; exit; fi
  68.  
  69.   FDSFN="${FILENAME##*/}"                          # Strip longest match of */ from start
  70.   FDSDIR="${FILENAME:0:${#FILENAME} - ${#FDSFN}}"  # Substring from 0 thru pos of filename
  71.   FDSBASE="${FDSFN%.[^.]*}"                        # Strip shortest match of . plus at least one non-dot char from end
  72.   FDSEXT="${FDSFN:${#FDSBASE} + 1}"                # Substring from len of base thru end
  73.   if [[ -z "$FDSBASE" && -n "$FDSEXT" ]]; then     # If we have an extension and no base, it's really the base
  74.       FDSBASE=".$FDSEXT"
  75.       FDSEXT =""
  76.   fi
  77.  
  78.   MD5=($(md5sum "$FILENAME"))
  79.   SHA256=($(sha256sum "$FILENAME"))
  80.   SIZE=`stat --printf="%s" "$FILENAME"`
  81.  
  82.   if [ "$FDSEXT" = "" ]; then #things with no extension at all shouldn't have a dot following
  83.     TARGET="$HASHDIR"/"$SIZE"."$MD5"-md5".$SHA256"-sha256
  84.   else
  85.     TARGET="$HASHDIR"/"$SIZE"."$MD5"-md5".$SHA256"-sha256."$FDSEXT"
  86.   fi
  87.  
  88.   debug "$FILENAME" tested negative as directory
  89.   debug md5sum=\""$MD5"\"
  90.   debug sha256=\""$SHA256"\"
  91.   debug size=\""$SIZE"\"
  92.   debug linking \""$FILENAME"\" to \""$TARGET"\"
  93.  
  94.   ln "$FILENAME" "$TARGET"
  95.  
  96.   #It can be useful to know what filenames things with multiple links have used and/or are using
  97.   trymkdir "$HASHDIR"/"Seen-filenames"
  98.   TARGET="$HASHDIR"/"Seen-filenames"/"$FDSFN"
  99.   debug linking \""$FILENAME"\" to \""$TARGET"\"
  100.   ln "$FILENAME" "$TARGET"
  101.  
  102.   #job done for this file, dismiss thyself
  103.   exit
  104. else
  105.   debug not running nested, so executing find routine
  106.   find "$FILENAME" -exec "$SELFLINK" {} "$NESTINGCODE" \; #  The workhorse of the whole operation
  107. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement