Rename-orphans-files.sh

From Essential
Revision as of 12:55, 29 October 2019 by Tcepo (talk | contribs) (Created page with "<pre> #!/bin/sh find ./* ./\.[^\.]* ./\.\.?* -type d |while read MyPath ;do MyCount=$(ls -a1 "$MyPath" |grep -vE "^\.{,2}$" |wc -l) if [ "${MyCount}" -lt "4" ] ;then...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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