Skip to content

Instantly share code, notes, and snippets.

@tueda
Created December 25, 2025 05:17
Show Gist options
  • Select an option

  • Save tueda/16d646cb3650b9b65c87e75462096024 to your computer and use it in GitHub Desktop.

Select an option

Save tueda/16d646cb3650b9b65c87e75462096024 to your computer and use it in GitHub Desktop.
Script to deletes "*:Zone.Identifier" files. #bin #bash #wsl2
#!/bin/bash
#
# @file unzone
#
# Deletes "*:Zone.Identifier" files.
#
# Usage: unzone [OPTIONS...] [DIRECTORIES...]
# Options:
# -h, --help print this help
# -n, --dry-run show what would be deleted, but do nothing
# -xdev do not descend into other filesystems
#
set -euo pipefail
PROG="${0##*/}"
dry_run=0
use_xdev=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
awk -v prog="$PROG" '
/^# Usage:/ { show=1 }
show && /^# *$/ { exit }
show {
sub(/^# ?/, "", $0)
gsub(/unzone/, prog, $0)
print
}
' "$0"
exit 0
;;
-n|--dry-run)
dry_run=1
shift
;;
-xdev)
use_xdev=1
shift
;;
--)
shift
break
;;
-*)
echo "$PROG: unknown option: $1" >&2
exit 2
;;
*)
break
;;
esac
done
if (( $# == 0 )); then
target_directories=(.)
else
target_directories=("$@")
fi
find_opts=()
(( use_xdev )) && find_opts+=(-xdev)
for d in "${target_directories[@]}"; do
[[ -d "$d" ]] || { echo "$PROG: not a directory: '$d'" >&2; exit 2; }
done
if (( dry_run )); then
find "${target_directories[@]}" "${find_opts[@]}" -type f -name '*:Zone.Identifier' -exec printf "would remove '%s'\n" {} \;
else
find "${target_directories[@]}" "${find_opts[@]}" -type f -name '*:Zone.Identifier' -exec rm -v -- {} +
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment