Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename multiple files in a directory with one command

Status
Not open for further replies.
Apr 30, 2003
56
0
0
US
I have about hundreds of files under the directory /u01/stat/oradata/arch named from 1_1_880795724.dbf to 1_177_880795724.dbf I want to rename all these files to stat_1_1_880795724.arc etc. How can I achieve this in a single command?
I wrote a perl script, but it is not working even for just replacing the file type only.
#!/usr/bin/perl
opendir (DIR, "/u01/stat/oradata/arch");
foreach $file (readdir(DIR)) {
next if $file =~ /^\./; # skip ., .., etc
rename 's/\.dbf$/.arc/' *dbf
}
closedir (DIR);

Operator or semicolon missing before *dbf at ./replace_arch_format.perl line 11.
Ambiguous use of * resolved as operator * at ./replace_arch_format.perl line 11.
Not enough arguments for rename at ./replace_arch_format.perl line 11, near "dbf
}"
Execution of ./replace_arch_format.perl aborted due to compilation errors.

Can gurus advise? Thank you very much.
 
Not exactly what you're looking for since it's not a one-liner, but if you're going to be renaming files like this a lot -- this could come in handy:
Code:
#!/usr/bin/perl
# -w switch is off bc HERE docs cause erroneous messages to be displayed under Cygwin
#From the Perl Cookbook, Ch. 9.9
# rename - Larry's filename fixer
$help = <<EOF;
Usage: rename expr [files]

This script's first argument is Perl code that alters the filename (stored in \$_ ) to reflect how you want the file renamed. It can do this because it uses an eval to do the hard work. It also skips rename calls when the filename is untouched. This lets you simply use wildcards like rename EXPR * instead of making long lists of filenames.

Here are five examples of calling the rename program from your shell:

% rename 's/\.orig$//'  *.orig
% rename 'tr/A-Z/a-z/ unless /^Make/'  *
% rename '$_ .= ".bad"'  *.f
% rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i'  *
% find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/'

The first shell command removes a trailing ".orig" from each filename.

The second converts uppercase to lowercase. Because a translation is used rather than the lc function, this conversion won't be locale-aware. To fix that, you'd have to write:

% rename 'use locale; $_ = lc($_) unless /^Make/' *

The third appends ".bad" to each Fortran file ending in ".f", something a lot of us have wanted to do for a long time.

The fourth prompts the user for the change. Each file's name is printed to standard output and a response is read from standard input. If the user types something starting with a "y" or "Y", any "foo" in the filename is changed to "bar".

The fifth uses find to locate files in /tmp that end with a tilde. It renames these so that instead of ending with a tilde, they start with a dot and a pound sign. In effect, this switches between two common conventions for backup files
EOF

$op = shift or die $help;
# print STDERR "OP: $op\n";
# exit;
chomp(@ARGV = <STDIN>) unless @ARGV;

foreach my $pattern (@ARGV) {
	my @files = glob($pattern);
	
	# for (@ARGV) {
	foreach (@files) {
		$was = $_;
		eval $op;
		die $@ if $@;
		print STDOUT "$was => $_\n";
		rename($was,$_) unless $was eq $_;
	}
}
And if you're using it on a windows machine in the cmd shell, you'll need to adjust the quotes around the patterns and all that. Also, windows has a native rename command, so you'll likely want to save the file as a slightly different name.
 
And using the script listed above, you could rename the files by using the command:
Code:
rename.pl  "s/\.dbf$/\.arc/i" *.dbf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top