CaseFix.sh

From Essential
Revision as of 15:28, 29 October 2019 by Tcepo (talk | contribs) (Created page with "<pre> #!/bin/sh # Init cachePath=/tmp/duples$$ # store paths find * ./.[^.]* ./..?* -xdev ! -type l |gzip >"${cachePath}".in.gz # Convert lower case zcat "${cachePath}".in....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
#!/bin/sh

# Init
cachePath=/tmp/duples$$

# store paths
find * ./.[^.]* ./..?* -xdev ! -type l |gzip >"${cachePath}".in.gz

# Convert lower case
zcat "${cachePath}".in.gz |tr '[:upper:]' '[:lower:]' >"${cachePath}".out2

mv -f "${cachePath}".out2 "${cachePath}".in2

# Get duples
inFile="${cachePath}".in2
outFile="${cachePath}".out2
triggerValue=1
awk 'NR == FNR {count[$0]++; next}; count[$0] > '"${triggerValue}" "${inFile}" "${inFile}" \
	| awk '!seen[$0]++' |tee "${outFile}"

mv -f "${cachePath}".out2 "${cachePath}".in2

# get original duplicates, new in
zgrep -xi -f "${cachePath}".in2 "${cachePath}".in.gz >"${cachePath}".out2
rm -f "${cachePath}".in.gz

mv -f "${cachePath}".out2 "${cachePath}".in

# fix
cat "${cachePath}".in2 |while read lineMy ;do
  filePath=""
  dirPath=""
  zgrep -xi "${lineMy}" "${cachePath}".in |while read lineMy2 ;do
    if [ -f "${lineMy2}" ] ;then
      if [ -z "${filePath}" ] ;then
        filePath="${lineMy2}"

        dirName="$(dirname "${filePath}")"
        oldName=$(basename "${filePath}" |sed -rn 's#^(.*)\.([^\.]*)|([^\.]+)$#\1\3#p')
        typeName=$(basename "${filePath}" |sed -rn 's#^(.*)\.([^\.]*)|([^\.]+)$#\2#p')
      fi
      md5File=$(md5sum "${lineMy2}" |cut -c1-4 )

      mv -nv "${lineMy2}" "${dirName}/${oldName}.${md5File}.${typeName}"
    else
      if [ -z "${dirPath}" ] ;then
        dirPath="${lineMy2}"
      else
        mv -vn "${lineMy2}"/* "${lineMy2}"/\.[^\.]* "${lineMy2}"/\.\.?* "${dirPath}"/.
        rmdir "${lineMy2}"
      fi
    fi
  done
done

rm -rf /tmp/duples$$*