WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script archive-old-files

Archive Old Files

Move files untouched for N days into a dated archive directory.

category File Management shell Bash 4+ updated 2026-08-09
archivefilesretentionmove
Safety notes
  • Moves files. Preview with the default dry run before using --apply.
  • Keeps the directory structure? No — this script flattens collisions are refused.

Dependencies: find, mv, date

Usage

  • Pass a source directory; archived files go to ARCHIVE_DIR/YYYY-MM-DD/.
  • Default age is 90 days; change with —days.

Script

#!/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)"

Example

./archive-old-files ~/Downloads
DAYS=30 ./archive-old-files ~/Downloads --apply

Notes

  • Only top-level files are considered; nested trees need a different retention rule.
  • Tested behaviour: existing destinations are skipped rather than overwritten.

Related scripts

Find Large Files

Locate files above a size threshold and show them largest first.

File Management