Chankey Pathak Chankey Pathak - 1 year ago 66
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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download