Batch rename file extensions, including subdirecto

2019-04-19 08:45发布

I'm renaming empty file extensions with this command:

rename *. *.bla

However, I have a folder with hundreds of such subfolders, and this command requires me to manually navigate to each subfolder and run it.

Is there a command that I can run from just one upper level folder that will include all the files in the subfolders?

7条回答
小情绪 Triste *
2楼-- · 2019-04-19 09:13

You can easily do this and many more things with the Perl module File::Find.

#!perl

use strict;
use warnings;
use File::Find;

my $extension = 'bla';
my $directory = '/tmp/test';

print "Files renamed:\n";
find( \&wanted, $directory );

sub wanted {
    return if /\./;
    return unless -f $File::Find::name;

    if ( rename( $File::Find::name, "$File::Find::name.$extension" ) ) {
        print "    $File::Find::name -> $File::Find::name.$extension\n";
    }
    else {
        print "    Error: $! - $File::Find::name\n";
    }
}
查看更多
登录 后发表回答