Search code examples
bashpattern-matchingfile-renamebatch-renamemv

Renaming files: changing


Problem

Given a directory with a large set of files using a spacer characters, here -, how can they all be changed to use another spacer character, for example =?

Examples

run-script-one.shrun=script=one.sh

build-object-a.shbuild=object=a.sh

load-file-alpha.shload=file=alpha.sh


Solution

  • With prename (Perl's standalone rename command):

    prename -n 's/-/=/g' *.sh
    

    Output:

    build-object-a.sh renamed as build=object=a.sh
    load-file-alpha.sh renamed as load=file=alpha.sh
    run-script-one.sh renamed as run=script=one.sh
    

    If everything looks fine, remove -n (no action option).


    If prename is not available:

    for i in *.sh; do echo mv -v "$i" "${i//-/=}"; done
    

    Remove echo if everything looks fine.