Bulk Rename with Preview
Rename files matching a glob using a pattern, with a dry-run preview.
$ webtoolist --script archive-old-files
Move files untouched for N days into a dated archive directory.
archivefilesretentionmoveDependencies: find, mv, date
#!/usr/bin/env bash
set -euo pipefail
src="${1:-}"
days="${DAYS:-90}"
archive_root="${ARCHIVE_DIR:-$HOME/archive}"
apply=0
[[ -n "$src" && -d "$src" ]] || { echo "usage: $0 SRC_DIR [--apply]" >&2; exit 1; }
[[ ${2:-} == --apply ]] && apply=1
dest="$archive_root/$(date +%F)"
mkdir -p "$dest"
mapfile -t files < <(find "$src" -maxdepth 1 -type f -mtime +"$days" -print)
for file in "${files[@]}"; do
target="$dest/$(basename "$file")"
if [[ -e "$target" ]]; then
echo "skip (exists): $target" >&2
continue
fi
if ((apply)); then
mv -- "$file" "$target"
echo "archived: $file -> $target"
else
echo "would archive: $file -> $target"
fi
done
((apply)) || echo "(dry run) re-run with --apply to move ${#files[@]} file(s)"
./archive-old-files ~/Downloads
DAYS=30 ./archive-old-files ~/Downloads --apply
Related scripts
Rename files matching a glob using a pattern, with a dry-run preview.
Locate files above a size threshold and show them largest first.
Create a compressed, timestamped tar archive of a directory and verify it.