Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

BashbeginnercliAI generated

Bulk File Renamer with Regex Patterns

Build a Bash tool that renames many files at once using a regular expression: for example, turn IMG_20260315_142233.jpg into 2026-03-15_14-22-33.jpg for a whole folder in one command. It always shows a preview table of old and new names first (a dry run), refuses to go ahead when two files would get the same name or a new name already exists, and only renames after you confirm, writing a log so the changes can be undone. It's a good first shell scripting project: you'll learn options with getopts, finding files with find, regular expressions with sed, handling file names with spaces safely, careful use of mv, and testing scripts with bats.

Estimate
~6h
Steps
5
Completed by
0
Proposed by
codeseed.app

sed · getopts · find

Project roadmap

  1. 01

    Design the options

    ~1h

    Write rename.sh with set -euo pipefail, and use getopts for -d DIRECTORY (default: the current one), -p PATTERN (an extended regular expression), -r REPLACEMENT (with \1-style groups), -n for a dry run, -y to skip confirmation, -R to include subfolders and -u LOGFILE to undo. Validate the options and print usage for invalid ones. Done when: rename.sh -h prints the usage, and invalid options are rejected.

  2. 02

    List and match files

    ~1h

    List files with find (only the top level unless -R, and only regular files), reading names with find -print0 and read -d '' so names with spaces or newlines work. Keep only files whose names (not paths) match the pattern, testing each with grep -E or [[ =~ ]]. Done when: the matching files are listed correctly, including names with spaces.

  3. 03

    Compute and check the new names

    ~1.5h

    Compute each new name with sed -E "s/PATTERN/REPLACEMENT/" on the name only, and check the plan before touching anything: no empty names or names containing /, no two files getting the same new name, and no new name matching a file that already exists (unless that file is also being renamed away). Report every problem and stop. Done when: plans with collisions are refused with a clear explanation.

  4. 04

    Preview and rename

    ~1h

    Print an aligned table of old name and new name (skipping unchanged names). In dry-run mode, stop there. Otherwise ask for confirmation (unless -y), rename each file with mv -n (never overwriting), and write each successful rename to a log file. Add -u LOGFILE to undo a previous run by renaming files back in reverse order. Done when: renames happen only after confirmation, and undo restores the original names.

  5. 05

    Write tests

    ~1.5h

    Write tests with bats-core that create temporary folders of sample files: a normal rename with groups, dry run changing nothing, names with spaces, collisions refused, an existing target refused, recursive mode, and undo. Run shellcheck on the script. Done when: the tests pass, and shellcheck reports no warnings.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~6h · 5 steps

Tech stack

sedgetoptsfind