Archive Old Files
Move files untouched for N days into a dated archive directory.
$ webtoolist --script bulk-rename
Rename files matching a glob using a pattern, with a dry-run preview.
renamefilesglobsafetyDependencies: find, sed, mv
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "usage: $0 [--apply] DIR GLOB SED_EXPR" >&2
echo "example: $0 --apply . '*.jpeg' 's/\\.jpeg$/.jpg/'" >&2
exit 2
}
apply=0
[[ ${1:-} == --apply ]] && { apply=1; shift; }
(($# == 3)) || usage
dir="$1"
glob="$2"
expr="$3"
[[ -d "$dir" ]] || { echo "not a directory: $dir" >&2; exit 1; }
shopt -s nullglob
for src in "$dir"/$glob; do
base="$(basename "$src")"
dest_base="$(printf '%s' "$base" | sed -E "$expr")"
[[ "$base" == "$dest_base" ]] && continue
dest="$(dirname "$src")/$dest_base"
if [[ -e "$dest" ]]; then
echo "skip (exists): $dest" >&2
continue
fi
if ((apply)); then
mv -- "$src" "$dest"
echo "renamed: $base -> $dest_base"
else
echo "would rename: $base -> $dest_base"
fi
done
((apply)) || echo "(dry run) re-run with --apply to rename"
./bulk-rename . '*.jpeg' 's/\.jpeg$/.jpg/'
./bulk-rename --apply . '*.jpeg' 's/\.jpeg$/.jpg/'
Related scripts
Move files untouched for N days into a dated archive directory.
Locate files above a size threshold and show them largest first.