WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script bulk-rename

Bulk Rename with Preview

Rename files matching a glob using a pattern, with a dry-run preview.

category File Management shell Bash 4+ updated 2026-09-05
renamefilesglobsafety
Safety notes
  • Renames files. Dry run is the default; --apply performs the moves.
  • Refuses to overwrite an existing destination.

Dependencies: find, sed, mv

Usage

  • Pass a directory, a glob, and a sed expression applied to the basename.
  • Preview first, then repeat with —apply.

Script

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

Example

./bulk-rename . '*.jpeg' 's/\.jpeg$/.jpg/'
./bulk-rename --apply . '*.jpeg' 's/\.jpeg$/.jpg/'

Notes

  • Quote the sed expression so the shell does not expand it.
  • The glob is expanded by the script, so pass it quoted.

Related scripts

Archive Old Files

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

File Management

Find Large Files

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

File Management