Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

randrn

Random filename renamer.

Use case #1

You have files with garbled names and want stable, sortable replacements. By default, generated names include:

  • Modified date and time
  • Counter from 0001 and up, sorted by mtime
  • 16 alphanumeric random characters
  • Original extension, or one chosen by you

Example:

$ randrn *.txt
'Some file name with &%¤# in it.txt' --> 2022-12-12_09-49-45_0001_cya5x6mk6spb19tt.txt

Use case #2

You have files with non-alphanumeric characters in the filename.

  • Use "-a or --auto" Only renames files which actually have garbage in the filenames.
  • Use "-S or --strip" Only strips out garbage and replaces it with alphanumerical randomness.

Example of --strip:

$ randrn --strip Some*
Some file name with &%¤# in it.txt --> Some_file_name_with__0i29kwlg_in_it.txt
Spaces are replaced with underscores, then garbage characters are removed and a short random suffix is added.

Unicode normalization

randrn now uses NFC normalization by default for generated filenames.
This keeps visually identical Unicode names in a consistent stored form across tools and platforms.

Use --unicode-normalization to override behavior:

  • nfc (default): composed form (recommended in most cases)
  • none: disable normalization
  • nfd: decomposed form
  • nfkc: compatibility-composed form
  • nfkd: compatibility-decomposed form

Normalization is applied to new filenames in both random rename mode and --strip mode.

Examples:

# Explicitly use the default NFC behavior
randrn --unicode-normalization nfc "*.txt"

# Disable normalization
randrn --unicode-normalization none "*.txt"

# Strip mode + normalization
randrn --strip --unicode-normalization nfc "Some*"

Installation:

Install with pipx:
pipx install . (--force)

To build with pyinstaller:
make
make install

To install as a modern Python CLI (recommended):
python3 -m pip install .

To just copy to /usr/local/bin/randrn:
./inst.sh

Testing:

python3 -m unittest -v

Usage:

python3 randrn.py -h
usage: randrn.py [-h] [-a] [-S] [-e EXTENSION] [-d] [-R] [-n] [--dry-run]
                 [--unicode-normalization {none,nfc,nfd,nfkc,nfkd}]
                 [--symlinks {skip,rename,follow}] [-y] [--manifest MANIFEST]
                 [--fail-on-external-follow]
                 [--rollback ROLLBACK]
                 [wildcard]

randrn - rename files with random names

positional arguments:
  wildcard              A wildcard pattern to match filenames (default: *)

options:
  -h, --help            show this help message and exit
  -a, --auto            Auto rename only filenames with non alphanumerical characters (default: False)
  -S, --strip           Strip mode: Just strip away non alphanumerical characters (default: False)
  -e EXTENSION, --extension EXTENSION
                        Extension to set for new filename (default: None)
  -d, --dir             Also rename directories (default: False)
  -R, --recursive       Recursive mode (default: False)
  -n, --now             Use datetime NOW iso mtime (default: False)
  --dry-run             Show changes without renaming (default: False)
  --unicode-normalization {none,nfc,nfd,nfkc,nfkd}
                        Normalize new filenames before rename (default: nfc)
  --symlinks {skip,rename,follow}
                        How to handle symlinks: skip, rename the link, or follow to target (default: skip)
  -y, --yes             Assume yes for safety prompts (default: False)
  --manifest MANIFEST   Write JSON manifest of attempted renames (default: None)
  --fail-on-external-follow
            Exit with error if follow mode encounters an external symlink target (default: False)
  --rollback ROLLBACK   Rollback renames from a previously written manifest file (default: None)

New features

  • Unicode-aware strip mode keeps Unicode letters and digits, and supports normalization with --unicode-normalization (default: nfc).
  • Explicit symlink policy via --symlinks:
    • skip: do not touch symlinks
    • rename: rename the symlink itself
    • follow: rename the symlink target
    • For follow, when a symlink points outside the current directory, a warning prompt is shown before renaming.
    • Use -y/--yes to auto-accept prompts in non-interactive automation.
    • Use --fail-on-external-follow for strict CI mode to hard-fail if external targets are encountered.
  • Manifest and rollback support:
    • Create: randrn --manifest /tmp/randrn_manifest.json "*.txt"
    • Rollback: randrn --rollback /tmp/randrn_manifest.json

Exit codes

  • 0: success
  • 1: general error (rename failures, invalid rollback manifest, etc.)
  • 2: strict external-follow block triggered by --fail-on-external-follow

CI example:

randrn --symlinks follow --fail-on-external-follow "*.txt"
rc=$?

if [[ $rc -eq 0 ]]; then
  echo "rename succeeded"
elif [[ $rc -eq 2 ]]; then
  echo "blocked external symlink target (strict mode)"
  exit 2
else
  echo "rename failed"
  exit $rc
fi

About

Random name renamer

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages