Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Created November 23, 2022 07:26
Show Gist options
  • Save oguz-ismail/46c914afab6f1cd3c13d2a0e3a242f52 to your computer and use it in GitHub Desktop.
Save oguz-ismail/46c914afab6f1cd3c13d2a0e3a242f52 to your computer and use it in GitHub Desktop.
wrapper=
status=0
badsrc() {
printf 'skipping %s\n' "$src" >&2
status=1
}
randstr() {
tr -cd '[:alnum:]' </dev/urandom | head -c 10
}
randdest() {
dest=$dir$pfx$(randstr)$ext
}
while getopts :n opt; do
case $opt in
(n)
wrapper=echo ;;
(*)
printf 'Usage: %s [-n] src...\n' "${0##*/}"
exit 1
esac
done
shift $((OPTIND - 1))
for src; do
dir=
pfx=
ext=
case $src in
(''|.|..|./|../|*/.|*/..|*/./|*/../)
badsrc
continue ;;
(*[!/]*)
;;
(*)
badsrc
continue
esac
case $src in
(*/)
tmp=${src%${src##*[!/]}}
dir=${tmp%"${tmp##*/}"} ;;
(*)
name=${src##*/}
dir=${src%"$name"}
case $name in
(.*)
pfx=. ;;
(*.*)
ext=.${name##*.}
esac
esac
if ! randdest; then
exit 1
fi
while test -L "$dest" || test -e "$dest"; do
randdest
done
if ! $wrapper mv -- "$src" "$dest"; then
status=1
fi
done
exit $status
# vi: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment