LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 06-02-2004, 10:45 PM   #16
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65

rename "2.dd-" "" *
rename "-dead-" "" *
for i in 0 1 2 3 4 5 6 7 8 9 ;do rename .gif$i .gif *;done

run that last one 5 times, then change the .gif's to .jpg's.
 
Old 06-02-2004, 11:08 PM   #17
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65
i didn't realize that > was part of the filename.
after all that above.
rename ".gif>" ".gif" *
 
Old 06-03-2004, 07:38 AM   #18
NiallC
Member
 
Registered: Feb 2003
Distribution: Suse 8.1
Posts: 102

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Dark_Helmet


You must remove the ".dd-" portion first.
This is most likely where I messed up. I'll re-try and report back.

Thanks for sticking with me on this one Dark Helmet!! you are a god-send.

whansard - I'll give yours a bash too...and see where it leads!thanks

N
 
Old 06-03-2004, 08:35 AM   #19
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65
i created the files you put in your first post and did what i had to to get it the way you wanted.

/test# ls
total 3
1 . 0 dennp2.dd-94795.jpg-dead-94795>
2 .. 0 dennp2.dd-94798.gif-dead-94798>
0 dennp2.dd-94783.gif-dead-94783> 0 dennp2.dd-94799.gif-dead-94799>
0 dennp2.dd-94784.gif-dead-94784> 0 dennp2.dd-94800.gif-dead-94800>
0 dennp2.dd-94787.gif-dead-94787> 0 dennp2.dd-94801.jpg-dead-94801>
0 dennp2.dd-94788.gif-dead-94788> 0 dennp2.dd-94802.gif-dead-94802>
0 dennp2.dd-94789.gif-dead-94789> 0 dennp2.dd-94804.jpg-dead-94804>
/test# rename "2.dd-" "" *
/test# rename "-dead-" "" *
/test# ls
total 3
1 . 0 dennp94788.gif94788> 0 dennp94800.gif94800>
2 .. 0 dennp94789.gif94789> 0 dennp94801.jpg94801>
0 dennp94783.gif94783> 0 dennp94795.jpg94795> 0 dennp94802.gif94802>
0 dennp94784.gif94784> 0 dennp94798.gif94798> 0 dennp94804.jpg94804>
0 dennp94787.gif94787> 0 dennp94799.gif94799>
/test# for i in 0 1 2 3 4 5 6 7 8 9 ;do rename .gif$i .gif *;done
/test# for i in 0 1 2 3 4 5 6 7 8 9 ;do rename .gif$i .gif *;done
/test# for i in 0 1 2 3 4 5 6 7 8 9 ;do rename .gif$i .gif *;done
/test# for i in 0 1 2 3 4 5 6 7 8 9 ;do rename .gif$i .gif *;done
/test# for i in 0 1 2 3 4 5 6 7 8 9 ;do rename .gif$i .gif *;done

/test# ls
total 3
1 . 0 dennp94788.gif> 0 dennp94800.gif>
2 .. 0 dennp94789.gif> 0 dennp94801.jpg94801>
0 dennp94783.gif> 0 dennp94795.jpg94795> 0 dennp94802.gif>
0 dennp94784.gif> 0 dennp94798.gif> 0 dennp94804.jpg94804>
0 dennp94787.gif> 0 dennp94799.gif>
/test# rename ".gif>" ".gif" *
/test# ls
total 3
1 . 0 dennp94788.gif 0 dennp94800.gif
2 .. 0 dennp94789.gif 0 dennp94801.jpg94801>
0 dennp94783.gif 0 dennp94795.jpg94795> 0 dennp94802.gif
0 dennp94784.gif 0 dennp94798.gif 0 dennp94804.jpg94804>
0 dennp94787.gif 0 dennp94799.gif

then a few more things to take care of the .jpg files


i guess as a script it could be

rename "2.dd-" "" *
rename "-dead-" "" *
for j in 1 2 3 4 5 ; do
for i in 0 1 2 3 4 5 6 7 8 9 ;do rename .gif$i .gif *; rename .jpg$i .jpg *;done
done
rename ".jpg>" ".jpg" *
rename ".gif>" ".gif" *


Last edited by whansard; 06-03-2004 at 08:48 AM.
 
Old 06-03-2004, 12:44 PM   #20
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
For good measure, I thought I'd throw this one in too. It's a perl script, but it still requires the find command format earlier.
Code:
#!/usr/bin/perl

if ($#ARGV > -1 )
{
  $filename = shift;
  $new_filename = $filename;

  if ( $new_filename =~ /\.dd-/ )
  {
    $new_filename =~ s/\.dd-//g;
  }

  if ( $new_filename =~ /-dead-[0-9]*>/ )
  {
    $new_filename =~ s/-dead-[0-9]*>//g;
  }

  if( $filename ne $new_filename )
  {
    system mv, $filename, $new_filename;
  }
}
It will rename the file in one action. It'll take care of the ".dd-" and "-dead-" portions. Create the script (I named it file_rename.pl), make it executable, and then (assuming it's in the same dir as the files to rename) type this at your bash prompt (you're creating a mini-wrapper script):

Code:
 ls | while read filename; do ./file_rename.pl $filename; done
 
Old 07-03-2004, 05:01 PM   #21
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
Please, help!
I have a problem: plenty of Japanese files, with disguised names, such as: V^[N[X܂̓X@�5 .doc (invalid Unicode)
In some instances they have no extention, or the period before:
V^[N[X܂̓X@�5?doc
The lenght of the names is always different.
All these files were imported from Windzoo 97.

OpenOffice opens them beautifully (btw, the latest OpenOffice works like charm, and loads really fast! I also solved the problem of printing margins.), if the names look like something more civilized, say, 1.doc, 2.doc etc.

I need a script wich will batch rename all these files, replacing the names with a number and *.doc extention.
I read mv and rename man, but this task is beyond my skills.

Can anyone post here a ready to use script, or something I could start with? Thanks in advance.

P.S. rename ".doc (invalid Unicode)" ".doc" *
works, but I don't know what to do with the mess before the extension

Last edited by linuxfond; 07-03-2004 at 05:14 PM.
 
Old 07-03-2004, 05:24 PM   #22
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Here's a basic script:
Code:
#!/bin/bash

document_number=1

old_ifs=${IFS}
IFS=$'\n'

for filename in $( ls -1 *.doc ) ; do
  mv -v "${filename}" ${document_number}.doc
  ((document_number=document_number+1))
done

IFS=${old_ifs}

exit 0
Save it, and then make it executable (chmod u+x filename)

Cd to the directory with the documents to be renamed, and execute the script.

As I mentioned earlier, always try new scripts that move or delete files on a test set of data first. Verify that it does what you want it to do before running it on your originals.

Last edited by Dark_Helmet; 07-04-2004 at 11:25 AM.
 
Old 07-03-2004, 06:26 PM   #23
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
This looks nice. It should work and I will give it a try now.
BTW, Today while I was messing up with these scripts, I renamed all folders in the /root directory (incidentally I run the script as root -- luckily I have not donemore damage!)
Anyway, all folders in /root were renamed in 1.doc 2.doc 3.doc
I figured out that one of them was tmp folder, another was Desktop, the third folder contains log files, auto-install.pl script etc. I don't remember what was the name of this folder. Do you have any idea?
 
Old 07-03-2004, 06:34 PM   #24
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
bash-2.05b$ ./rename
`}}*&||':#$%^(&^.doc -> `number.doc'
`%^(&^.doc.doc' -> `number.doc'
`%^(&^^&^.doc.doc' -> `number.doc'
`%^^.doc.doc' -> `number.doc'
`Japan##$%(*&.doc' -> `number.doc'
bash-2.05b$

Unfortunately, it replaces all the files with one just file: number.doc
Something is missing...
 
Old 07-03-2004, 06:41 PM   #25
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
There was a typo in the script. Change this line:
Code:
  mv -v "${filename}" ${document-number}.doc
To this:
Code:
  mv -v "${filename}" ${document_number}.doc
As for the problem with renaming the directories in root, well, it all depends on your installation. There are some standard ones:
/bin
/boot
/dev
/sbin
/home
/usr
/tmp
/var
/etc
/lib
/opt
/proc
/root
/mnt

The one that contains log files is typically /var.
 
Old 07-04-2004, 10:45 AM   #26
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
Thanks! I figured out that the 2.doc was originally drakx.
That's what we have in MDK.

I will try the script now....


THANKS indeed! It works beautifully.

Last edited by linuxfond; 07-04-2004 at 10:54 AM.
 
  


Reply

Tags
md5sum, regex, rename, renamescript, script



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
renaming multiple files linuxhippy Slackware 10 01-21-2011 01:53 AM
shell renaming script paskari Programming 3 09-24-2005 02:15 PM
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 02:43 PM
directory renaming shell script? Apostasy Linux - General 15 02-22-2005 06:50 AM
Renaming multiple files with the same format Shr00mBoXx Slackware 7 06-20-2004 07:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:02 AM.

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
Open Source Consulting | Domain Registration