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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming files. 1

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
How would I rename many files (over 100 total) in a directory.
Example of my files:
file1.word.1
file2.word.2
file3.word.3

I want them to be named:
File1_word_1
File2_word_2
File3_word_3

My goal is to get rid of the periods and put in underline bars for each file name.

Here is my script that didnt work->
#!/bin/ksh
for file in wer
do
mv *.*.* _._.__
done
 
#!/usr/bin/perl

use strict;

my $file_name;
my $new_file_name;

while(<*.*.*>){
[tab]$file_name = $new_file_name = $_;
[tab]$new_file_name =~ s/\./_/g;
#[tab]rename $file_name, $new_file_name;
[tab]print &quot;rename $file_name, $new_file_name;\n&quot;;
}

Try that out, uncomment the 'rename' line when you're comfortable it will do what you want with the files you wnat renaming. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I don't think I have perl on my Solaris 7 and I have no knowledge of perl language.
Is there anyway I can do this with Unix?
 
My version of your code probably will not work, but can you see what I'm trying to get at.

Ps. you want to get rid of the &quot;.&quot; but you tell the code to rename all the files to the same line with &quot;.&quot; ex. &quot;_._._&quot;

&quot;My version&quot;
#!/bin/ksh
for file in wer
do
mv *.*.* *_*_*
done
 
Teser,

Yes, this can be done in the standard shell (in UNIX) but the only way I know is not simple. When I get through you'll see why I did it in Perl. :) Hopefully someone else will have an easier solution.

The trouble with Netwrkengeer's approach (and yours, and mine the first time I tried it) is that it would need the UNIX shell wild card characters to work the same way as in the DOS systems. Unfortunately they don't work in the same way at all, and trying to make them work the same will have some unfortunate results.

In DOS (NT/Winxx/etc) the program itself does all of the evaluation of any wild card characters on the command line.

If the user types:

ren *.txt *.tex

then the ren program always sees the command line exactly as it's typed by the user; it has to interpret them itself.

Unix doesn't work like this, the shell program (/bin/ksh in this case) which calls the program (mv) interprets any * and ? chars on the command line; not the mv program. This is weird, I know, but perhaps a little example will make it clearer.

Suppose you have a directory that contains three files:

file.txt file2.txt another_file.1

and you type a command like this:

mv file*.txt *.1

The ksh shell interprets these wildcard chars and actually calls the mv program like this

mv file.txt file2.txt another_file.1

This will cause the mv program to do something that surprises most DOS based people.

It will behave as if you had typed:

cp file.txt another_file.1
rm file.txt
cp file2.txt another_file.1
rm file2.txt

so you end up with just the one file in your dir called another_file.1 - but this file is just a copy of file2.txt

Not what you meant to happen....

To do the renaming process you want, you will have to examine each filename, substitute the various bits using the builtin ksh pattern matching commands and the 'mv' the file.

The command I think you will need to use is the ${parameter%%word} command, here's an example from the solaris man page:

x=posix/src/std
y=${x%%/*}
echo $y

This will print:
posix

This command looks for a pattern ( /* in this case ) and takes the string *before* that and returns it (into $y in the example)

So, one approach would be too split each filename up by calling ${parameter%%word} a couple of times and then bulding the string from the results of that. Not easy, and I don't have a UNIX login in front of me - so I'm not even going to attempt an example....

Have a look at the section marked &quot;Parameter Expansion&quot; in the Solaris ksh man page, the examples are ok - work from those rather than the explanations. Sorry I can't be of more help. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I'm not at a unix box at the moment, but here's a thought which may help

for file in `ls file*`
do
NEWNAME=`echo $file|sed 's/./_/g'`
mv $file $NEWNAME
done

I'll try this tomorrow but it might just work!

Greg.

 
doh! Why didn't I think of that? I've used that stupid ${parameter%%word} thing a few times over the years as well......

Thanks Greg. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Teser, my post above almost works, but needs the following minor refinement .. you need to &quot;escape&quot; the period in the substitution string, so the code should read;

for file in `ls file*`
do
NEWNAME=`echo $file|sed 's/\./_/g'`
mv $file $NEWNAME
done

Greg.
 
Thanks (again) Greg for your help and also thanks to Mike. I appreciate all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top