Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


rename multiple files in a directory with one command

rename multiple files in a directory with one command

rename multiple files in a directory with one command

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.
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.

RE: rename multiple files in a directory with one command

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:


# -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

$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.

RE: rename multiple files in a directory with one command

And using the script listed above, you could rename the files by using the command:

CODE --> shell

rename.pl  "s/\.dbf$/\.arc/i" *.dbf 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close