LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2004, 05:00 PM   #1
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Rep: Reputation: 15
Need to remove the first three characters in the name of a ton of files...


Ok basically I need to remove the first characters in the names of these files on my computer and I was wondering if anyone could help me out with a script or a command. Heres an example of what my problem is.

001 Blahblahbla.mp3

I need to get rid of the numbers in front BUT sometimes its just two like this:

02 Blahblahbleh.mp3

so if anyone could help me out I figure just remove all the characters before the first white space, but I don't have the time to research the commands so I came here

thanks in advance.
 
Old 01-23-2004, 05:02 PM   #2
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
Just before the first whitespace tho pleas, I was unclear about that
 
Old 01-23-2004, 05:14 PM   #3
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
Perl is your friend

Code:
#!/usr/local/bin/perl -w

$dirname = ".";

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {
    if ( $file =~ /^\d+\s+\w+\.mp3$/ ) {
      $newname = $file;
      $newname =~ s/\d+\s+(\w+).mp3/$1.mp3/;

      print("Matched $file, newname will be $newname\n");
      rename($file,$newname) or die "Failed to rename $file to $newname: $!";
    }
}
closedir(DIR);
The script will process the current directory recursively, clearly instead of the $dirname declaration you could easily read a comman line arg. It's rather rough as its an old script I've had lying around for years with a few changes.

Beware of file name clashes! "011 test.mp3" and "012 test.mp3" would clash and result in one file being lost as the resulting file names would both be "test.mp3".

HTH

Jamie...

Last edited by jharris; 01-24-2004 at 06:25 AM.
 
Old 01-23-2004, 05:47 PM   #4
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
Anyone who can help me out with on that won't delete a file with the same name like that?

All I really want to do is remove any characters before the first whitespace, and remove the whitespace too.

Last edited by ooagentbender; 01-23-2004 at 05:48 PM.
 
Old 01-23-2004, 06:13 PM   #5
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
Quote:
Originally posted by ooagentbender
All I really want to do is remove any characters before the first whitespace, and remove the whitespace too.
That's exactly what the script is doing but if you have two files called "001 something.mp3" and "002 something.mp3" if you remove the characters before the white space, and the white space they both become "something.mp3".

See the problem now?

cheers

Jamie...
 
Old 01-23-2004, 09:17 PM   #6
alar
Member
 
Registered: Sep 2003
Location: Canada
Distribution: CentOS 5.5, Ubuntu 10.04, Linux Mint 11 gnome 64 bit
Posts: 337

Rep: Reputation: 36
Quote:
Perl is your friend
I like that !
Still trying to find the motivation...

Will try script. Thanks
 
Old 01-23-2004, 10:11 PM   #7
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
Ok I am officially a moron

Thank you for the script I shall run it.
 
Old 01-23-2004, 10:25 PM   #8
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
Ps you are the man it worked perfect. I love these forums thanks jharris.
 
Old 01-23-2004, 10:59 PM   #9
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
ok that only worked if the file had one word in its name, I need something thaat will work on

001 blah blah.mp3
1 blah blah.mp3
02 blahblah.mp3

so they end up without the numbers in the front of them.

I know this will probably be alot of work but I would really appreciate it. I could do it in c++ but I don't know the system calls so well.

thanks all
 
Old 01-23-2004, 11:13 PM   #10
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
Oh I figured out a simple way to do it. Use ASCII and just get rid of characters at the begining of the filename string until you hit an ASCII character that has decimal equivelant between 65-90 and 97-122 (letters), now I just need to know how to do that in perl. Anyone?
 
Old 01-24-2004, 06:24 AM   #11
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
Code:
#!/usr/local/bin/perl -w

$dirname = ".";

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {
    if ( $file =~ /^\d+\s+([\w\s]+)\.mp3$/ ) {
      $newname = $file;
      $newname =~ s/\d+\s+([\w\s]+)\.mp3/$1.mp3/;

      print("Matched $file, newname will be $newname\n");
      rename($file,$newname) or die "Failed to rename $file to $newname: $!";
    }
}
closedir(DIR);
Should do you, although I haven't tested it.
 
Old 01-24-2004, 12:03 PM   #12
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
jharris! Thanks man. But I have one last request, can you make so it will handle files with odd characters in the string like

001 Paul - Jimmy (blank&smart).mp3

sorry for being such a pain and not specifying this earlier. Thank you for all your help so far.

Last edited by ooagentbender; 01-24-2004 at 12:14 PM.
 
Old 01-25-2004, 07:52 PM   #13
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Just in case you or someone else is still interested, here is some code which will remove the numbers and spaces in front of a file name without regard to the characters inside the file name.

For example:001 Paul - Jimmy (blank&smart).mp3
should become:Paul - Jimmy (blank&smart).mp3

___________________________________________
#!/usr/bin/perl

@files = qx {ls *.mp3};

foreach $i ( @files ) {
chomp $i;
$newname = $i;
$newname =~ s/^[\0-9\s]+//g;
print "The file '$i' is renamed to '$newname'\n";
qx {mv '$i' '$newname'};
}

____________________________________________
 
Old 01-26-2004, 06:16 AM   #14
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
That's a lot tidier than my version. Mine was originall to move characters from the start of the filename to the end, hence it going about things an odd way to strip the chars.

cheers

Jamie...
 
Old 01-26-2004, 08:19 AM   #15
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Cool!

If you change the lines:
( $file =~ /^\d+\s+([\w\s]+)\.mp3$/ )
$newname =~ s/\d+\s+([\w\s]+)\.mp3/$1.mp3/;

to include non-standard characters, it seems to work just fine.

Example:
( $file =~ /^\d+\s+([\w\W\s]+)\.mp3$/ )
$newname =~ s/\d+\s+([\w\W\s]+)\.mp3/$1.mp3/;

Last edited by homey; 01-26-2004 at 08:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Where do I get dependencies? Missing a ton Ryle Dup Linux - Software 5 08-11-2005 03:14 AM
remove characters in c alaios Programming 2 07-10-2005 10:10 AM
unable ton install mandrake 10.01 fr@n-6 Mandriva 5 03-20-2005 01:44 PM
NTFS - files with CE font characters not displayed? zocy Mandriva 1 02-08-2004 10:56 PM
international characters in linux preventing me from accessing files hypermegachi Linux - General 2 10-29-2003 05:34 PM


All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration