This Perl script renames all the files in the current directory with a given prefix. The prefix can be passed as a command-line argument.
Method 1: Using rename function
#!/usr/bin/perl
my $prefix = shift @ARGV || '';
opendir my $dh, '.' or die "Couldn't open current directory: $!";
while (my $filename = readdir $dh) {
next if $filename =~ /^\./; # Skip dotfiles
my $newname = $prefix . $filename;
rename $filename, $newname or warn "Couldn't rename $filename: $!";
}
closedir $dh;
or
#!/usr/bin/perl
use strict;
use warnings;
use File::Glob;
my $prefix = 'new_';
my $pattern = '*.txt';
foreach my $file (glob $pattern) {
my $new_file = $prefix . $file;
rename $file, $new_file or die "Can't rename $file to $new_file: $!";
}
Save the script as rename.pl and run it with perl rename.pl newprefix_. This will add the prefix "newprefix_" to all the files in the current directory.
Method 2: Using File::Copy
This method uses the File::Copy module to copy files with a new name and then delete the old file. We can use the File::Glob module to get a list of files matching a pattern and then use the File::Copy module to copy each file with the new name.
#!/usr/bin/perl
use strict;
use warnings;
use File::Glob;
use File::Copy;
my $prefix = 'new_';
my $pattern = '*.txt';
foreach my $file (glob $pattern) {
my $new_file = $prefix . $file;
copy $file, $new_file or die "Can't copy $file to $new_file: $!";
unlink $file or die "Can't delete $file: $!";
}
Save the script as rename_files_method2.pl and run it with perl rename_files_method2.pl. This will rename all .txt files in the current directory with the prefix new_.
Method 3: Using regular expressions
This method uses regular expressions to modify the filenames with a prefix. We can use the File::Glob module to get a list of files matching a pattern and then use regular expressions to modify each filename.
use strict;
use warnings;
use File::Glob;
my $prefix = 'new_';
my $pattern = '*.txt';
foreach my $file (glob $pattern) {
my $new_file = $file;
$new_file =~ s/^/$prefix/;
rename $file, $new_file or die "Can't rename $file to $new_file: $!";
}
Save the script as rename_files_method3.pl and run it with `perl rename_files
all .txt files in the current directory with the prefix new_.
Method 4: Using File::Find
This method uses the File::Find module to recursively find and rename files with a prefix. The File::Find module provides a way to traverse a directory tree and apply a function to each file found. We can use the rename function within the wanted function to rename each file with the specified prefix.
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my $prefix = 'new_';
sub rename_file {
if (-f && /^.*\.txt$/) {
my $new_file = $prefix . $_;
rename $_, $new_file or warn "Can't rename $_ to $new_file: $!";
}
}
find(\&rename_file, '.');
Save the script as rename_files_method4.pl and run it with perl rename_files_method4.pl. This will recursively find and rename all .txt files in the current directory and its subdirectories with the prefix new_.
Method 5: Using opendir and readdir
This method uses the opendir and readdir functions to find and rename files with a prefix. The opendir function opens a directory, and the readdir function reads the contents of the directory. We can use regular expressions to match the filenames and then use the rename function to rename each file with the specified prefix.
#!/usr/bin/perl
use strict;
use warnings;
my $prefix = 'new_';
opendir(my $dh, '.') or die "Can't open current directory: $!";
while (my $file = readdir($dh)) {
next unless $file =~ /^.*\.txt$/;
my $new_file = $prefix . $file;
rename $file, $new_file or warn "Can't rename $file to $new_file: $!";
}
closedir $dh;
Save the script as rename_files_method5.pl and run it with perl rename_files_method5.pl. This will find and rename all .txt files in the current directory with the prefix new_.
Method 6: Using File::Copy
This method uses the File::Copy module to copy files with a prefix to a new directory and then rename the files. The File::Copy module provides functions for copying and moving files. We can use the copy function to copy the original files to a new directory with the prefix, and then use the rename function to rename the files with the specified prefix.
use strict;
use warnings;
use File::Copy;
my $prefix = 'new_';
my $new_dir = 'new_files';
mkdir $new_dir unless -d $new_dir;
opendir(my $dh, '.') or die "Can't open current directory: $!";
while (my $file = readdir($dh)) {
next unless $file =~ /^.*\.txt$/;
my $new_file = $prefix . $file;
copy($file, "$new_dir/$new_file") or warn "Can't copy $file: $!";
}
closedir $dh;
opendir($dh, $new_dir) or die "Can't open $new_dir: $!";
while (my $file = readdir($dh)) {
next unless $file =~ /^.*\.txt$/;
my $new_file = $prefix . $file;
rename("$new_dir/$file", "$new_dir/$new_file") or warn "Can't rename $file: $!";
}
closedir $dh;
Save the script as rename_files_method6.pl and run it with perl rename_files_method6.pl. This will copy all .txt files in the current directory to a new directory named new_files with the prefix new_, and then rename the files with the specified prefix.
Method 7: Using File::Find::Rule
This method uses the File::Find::Rule module to find and rename files with a prefix. The File::Find::Rule module provides a simple interface for finding files that match specified criteria. We can use the name method to match filenames and then use the rename function to rename each file with the specified prefix.
use strict;
use warnings;
use File::Find::Rule;
my $prefix = 'new_';
my @files = File::Find::Rule->file()->name('*.txt')->in('.');
foreach my $file (@files) {
my $new_file = $prefix . $file;
rename $file, $new_file or warn "Can't rename $file to $new_file: $!";
}
Save the script as rename_files_method7.pl and run it with perl rename_files_method7.pl. This will find and rename all .txt files in the current directory with the prefix new_.
Labels: perl program to Renaming Files with a Prefix