Chankey Pathak Chankey Pathak - 10 months ago 31
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?


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