Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # spring cleaning v0.0.1
- # written and otherwise cobbled together by:
- # killermist (killermist@gmail.com)
- #Imports
- SELFLINK="$0"
- FILENAME="$1"
- #Constants
- DEBUG="true" #should probably turn this into a "-v" command-line option
- HASHDIR=".hashes"
- NESTINGCODE="NeStCoDe..nEsTcOdE--blah" #Should be something fairly long and unlikely to be used as a filename
- if [ "$FILENAME" = "" ]; then #Let's get this out of the way early.
- echo "Base path necessary. Aborting."
- exit
- fi
- #too many if-degug tests. Let's clean that up.
- function debug {
- if [ $DEBUG = "true" ]; then
- if [ "$1" = "sleep" ]; then sleep 1; else echo -e $*; fi
- fi
- }
- function trymkdir {
- if [ ! -d "$1" ]; then mkdir "$1"; fi
- if [ ! -d "$1" ]; then echo "$1" did not exist and failed to be created. Aborting; exit; fi
- }
- #filename disambiguation suite
- # as acquired from http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
- # included both to cite source and for reference later, if needed
- : <<'extract-filename-and-extension-in-bash'
- #======================================================================================================================
- #!/bin/bash
- for fullpath in "$@"
- do
- filename="${fullpath##*/}" # Strip longest match of */ from start
- dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
- base="${filename%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
- ext="${filename:${#base} + 1}" # Substring from len of base thru end
- if [[ -z "$base" && -n "$ext" ]]; then # If we have an extension and no base, it's really the base
- base=".$ext"
- ext=""
- fi
- echo -e "$fullpath:\n\tdir = \"$dir\"\n\tbase = \"$base\"\n\text = \"$ext\""
- done
- #======================================================================================================================
- extract-filename-and-extension-in-bash
- debug
- debug "========================"
- debug "$FILENAME:\n\tdir = \"$FDSDIR\"\n\tbase = \"$FDSBASE\"\n\text = \"$FDSEXT\""
- trymkdir "$HASHDIR"
- debug sleep
- debug "$FILENAME" should be filename or target directory
- if [ "$2" = "$NESTINGCODE" ] #testing to see if self-summoned
- then
- if [ -d "$FILENAME" ]; then debug echo running nested, skip directories; exit; fi
- #For safety/sanity, verify that filename passed does actually qualify as a file.
- if [ ! -f "$FILENAME" ]; then echo "$FILENAME" is not a file. Skipping.; exit; fi
- FDSFN="${FILENAME##*/}" # Strip longest match of */ from start
- FDSDIR="${FILENAME:0:${#FILENAME} - ${#FDSFN}}" # Substring from 0 thru pos of filename
- FDSBASE="${FDSFN%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
- FDSEXT="${FDSFN:${#FDSBASE} + 1}" # Substring from len of base thru end
- if [[ -z "$FDSBASE" && -n "$FDSEXT" ]]; then # If we have an extension and no base, it's really the base
- FDSBASE=".$FDSEXT"
- FDSEXT =""
- fi
- MD5=($(md5sum "$FILENAME"))
- SHA256=($(sha256sum "$FILENAME"))
- SIZE=`stat --printf="%s" "$FILENAME"`
- if [ "$FDSEXT" = "" ]; then #things with no extension at all shouldn't have a dot following
- TARGET="$HASHDIR"/"$SIZE"."$MD5"-md5".$SHA256"-sha256
- else
- TARGET="$HASHDIR"/"$SIZE"."$MD5"-md5".$SHA256"-sha256."$FDSEXT"
- fi
- debug "$FILENAME" tested negative as directory
- debug md5sum=\""$MD5"\"
- debug sha256=\""$SHA256"\"
- debug size=\""$SIZE"\"
- debug linking \""$FILENAME"\" to \""$TARGET"\"
- ln "$FILENAME" "$TARGET"
- #It can be useful to know what filenames things with multiple links have used and/or are using
- trymkdir "$HASHDIR"/"Seen-filenames"
- TARGET="$HASHDIR"/"Seen-filenames"/"$FDSFN"
- debug linking \""$FILENAME"\" to \""$TARGET"\"
- ln "$FILENAME" "$TARGET"
- #job done for this file, dismiss thyself
- exit
- else
- debug not running nested, so executing find routine
- find "$FILENAME" -exec "$SELFLINK" {} "$NESTINGCODE" \; # The workhorse of the whole operation
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement