Chankey Pathak Chankey Pathak - 7 months ago 13
Perl Question

Perl one-liner to add a string at the end of file name

I have some HTML and JavaScript files in a directory. I want to append "_backup" at the end of each JavaScript (name.js) file. What would be the one-liner for it?

Answer

Pick your poison:

  1. File::Copy

    use File::Copy 'move';
    for ( glob "*.js" ){  # Platform independent
        move( $_, "${_}_backup" ) or warn "Failed to copy '$_'";
    }
    
  2. rename function

    for ( glob "*.js" ){   # Not platform independent
        rename( $_, "${_}_backup" ) or warn "Failed to copy '$_'";
    }
    
  3. The *nix rename utility

    $ rename .js .js_backup *.js