Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:29:30+00:00 2026-06-02T09:29:30+00:00

$dir = /home/naveen/mp3tag/testfolder; opendir(DMP3, $dir) || die(Cannot open directory); @files= readdir(DMP3; foreach $f (@files)

  • 0
$dir = "/home/naveen/mp3tag/testfolder";
opendir(DMP3, $dir) || die("Cannot open directory");
@files= readdir(DMP3;
foreach $f (@files)
{
  unless ( ($f eq ".") || ($f eq "..") )
  {
    $oldfile = $f;
    $newfile = $f;
    $newfile =~ s/ /_/g;
    print "Old file: $oldfile \t";
    print "New file: $newfile";
    print "\n";
    rename ("$oldfile", "$newfile") or warn "Couldn't rename $oldfile to $newfile !\n";
  } 
}

I’m writing a simple program to add underscores to an existing file and rename it. This is how far ive gotten with the code. However its not able to rename the file and gives me a warning and i’m not sure where the mistake is.

Also when i tried the same line on the cmd line I get the following error msg.

$ rename Jacques\ Greene\ -\ Clark\ \(Original\ Mix\).mp3 JG - C.mp3
Bareword "mp3" not allowed while "strict subs" in use at (eval 1) line 1.

$ rename Jacques\ Greene\ -\ Clark\ \(Original\ Mix\) JG - C
Can't locate object method "Original" via package "Mix" (perhaps you forgot to load "Mix"?) at (eval 1) line 1.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:29:32+00:00Added an answer on June 2, 2026 at 9:29 am

    You’re trying to rename all the files in the directory, not just one file. The error could be a great many things, since you did not mention it, I could only guess.

    rename is, as I recall, a bit wonky, and using move from File::Copy is a safer bet. Also, you might want to avoid renaming directories. Using a more intuitive interface would probably not be a bad idea either.

    One of your biggest mistakes is not using use strict; use warnings;. The amount of trouble you bring on yourself by leaving these out cannot be underestimated.

    use strict;
    use warnings;
    use File::Copy qw(move);
    
    for (@ARGV) {
        my $org = $_;
        tr/ /_/;
        move($org, $_) or warn "Couldn't move $org to $_: $!";
    }
    

    Usage:

    perl script.pl /home/naveen/mp3tag/testfolder/*.mp3
    

    So, as long as you give a proper glob as argument, your script will only affect those files. You can add more checks to make it stricter.

    If that commandline attempt of yours is meant to be with using the tool from /usr/bin/rename, I would hazard a guess that your error can simply be avoided by using quotes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have imported a local directory in my svn repository using import /my/home/dir file://my/home/svn/repo/trunk
In my bash script I need to change current dir to user's home directory.
File: /home/USER/DIR/a http://www.here.is.a.hyper.link.net/ /home/USER/DIR/b http://www.here.is.another.hyper.link.net/ Need to remove all the odd lines in this
In the bash command line, I set a variable myPath=/home/user/dir . I created a
Directory is something like: home/ file1.html file2.html Another_Dir/ file8.html Sub_Dir/ file19.html I am using
I have the following bash script: #DIR is something like: /home/foo/foobar/test/ without any whitespace
I have a directory Dir in which I have about 30 sub-directories. From these
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
we do: useradd -s /bin/rbash -d /home/dir/user1 -m user_1:user_1 rbash is not even a
I'd like to login to a remote machine, and mount my home dir there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.