On Sun, 2 Mar 2025, Jeremy Drake via Cygwin wrote:

> > 2) assuming there is not, I want to make a script using only things
> > present in a "base system" to clean them up.
>
> Now that the mount points are escaped and contain the Windows volume roots
> starting with 3.6, here's my script.  It uses bash/find/gawk.  I'd welcome
> any ideas on improving it, I've only just started messing with gawk.  (I'd
> started out doing grep/cut then xargs printf "%b\0" to unescape, but with
> gawk I can do that all in one program).
>
> It's in
> https://gist.github.com/jeremyd2019/4984ff0fa1f6fd8c99d7b8b244c52088

I decided to try the grep/cut/xargs/printf method again, and found it
shorter, so here it is.  I also set up LC_CTYPE and LC_COLLATE to C.UTF-8
up front, in case some other locale/encoding is set up.  I still have to
override this to C for the find, because it doesn't seem to like the
invalid unicode used by older Cygwin/MSYS2 versions for the binned files.

#!/bin/bash -ex

export LC_CTYPE=C.UTF-8 LC_COLLATE=C.UTF-8

IFS=. read -r cygmajor cygminor _ < <(uname -r)

declare -a roots

if [ "${cygmajor}" -gt 3 -o "${cygmajor}" -eq 3 -a "${cygminor}" -ge 6 ]; then
  # as of cygwin 3.6, volume roots are parsable from /proc/mounts
  # (noumount is the option that indicates a cygdrive mount)
  readarray -t -d $'\0' roots < <(
    cut -d' ' -f2,4 /proc/mounts | grep '\<noumount\>[^ ]*$' |
    cut -d' ' -f1 | xargs -d'\n' printf '%b\0')
else
  # before that, just punt and look at the root of the drive / is mounted on
  root="$(cygpath -w /)"
  roots=("$(cygpath -u "${root:0:2}")")
  unset root
fi

declare -a trash
readarray -t -d $'\0' trash < <(
  LC_CTYPE=C LC_COLLATE=C \
  find "${roots[@]}" -maxdepth 1 -iname '$Recycle.Bin' -print0 |
  LC_CTYPE=C LC_COLLATE=C \
  find -files0-from - -maxdepth 2 \( -name $'.\uDC6D\uDC73\uDC79\uDC73*' -o \
                                     -name $'.\uF76D\uF773\uF779\uF773*' -o \
                                     -name '.msys*' -o \
                                     -name $'.\uDC63\uDC79\uDC67*' -o \
                                     -name $'.\uF763\uF779\uF767*' -o \
                                     -name '.cyg*' \) -print0)
if (( ${#trash[@]} )); then
  ls -la "${trash[@]}"
  read -r -p "Remove? (y/N) "

  if [[ "${REPLY^^}" == "Y" ]]; then
    rm -f "${trash[@]}"
  fi
fi

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to