Rename-orphans-files.sh

From Essential
Jump to navigation Jump to search
#!/bin/sh

find ./* ./\.[^\.]* ./\.\.?* -type d |while read MyPath ;do
  MyCount=$(ls -a1 "$MyPath" |grep -vE "^\.{,2}$" |wc -l)
  if [ "${MyCount}" -lt "4" ] ;then
    ls -a1 "$MyPath" |grep -vE "^\.{,2}$" |while read MyFile ;do
      filterMy=$(
        MyPatern='^(.*)/[^/]*$'
        echo "$MyPath" |\
        sed -rn "s#$MyPatern#\1#p" |head -1
      )
      MyNewPath="$filterMy"

      filterMy=$(
        MyPatern='^.*/([^/]*)$'
        echo "$MyPath" |\
        sed -rn "s#$MyPatern#\1#p" |head -1
      )
      MyDirName="$filterMy"

      mv -vn "${MyPath}/${MyFile}" "${MyNewPath}/${MyDirName}-${MyFile}"
    done
  fi
done
find . -xdev -empty -delete