Renames many files at once using a sed substitution, with dry-run preview.
1 views
Share
This script renames many files in one pass by applying a sed substitution expression to each filename, with a safe dry-run as the default. It takes a directory, a sed expression like 's/ /_/g' (replace spaces with underscores) or 's/\.jpeg$/.jpg/' (normalize extensions), and an optional --apply flag. After set -euo pipefail, it validates the directory and enables nullglob so an empty folder produces no spurious iterations. For each regular file it computes the new name via sed -E, skips files whose name is unchanged, and either previews or performs the move. Crucially it uses mv -n so an existing target is never silently clobbered, and -- so filenames beginning with a dash are handled safely. Run it as ./bulk_rename.sh ./pics 's/ /_/g' to preview, then add --apply. Example:
[DRY RUN] my photo.jpg -> my_photo.jpg
[DRY RUN] 1 file(s) would be renamed. Add --apply to commit.