The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help renaming files bbbngowc UNIX for Dummies Questions & Answers 5 04-23-2008 11:08 AM
Renaming Files abch624 Shell Programming and Scripting 2 03-19-2008 08:54 PM
Renaming files Tygoon UNIX for Dummies Questions & Answers 7 01-06-2008 06:59 PM
renaming files systemsb UNIX for Dummies Questions & Answers 3 05-24-2006 09:56 PM
renaming files raguramtgr UNIX for Dummies Questions & Answers 4 09-21-2004 07:57 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-19-2008
Registered User
 

Join Date: Feb 2008
Posts: 4
Renaming Files

Hi Alll,

I have a script that we use on the servers to change the name of files that have spaces in the name:

#!/bin/tcsh
set n = 0
foreach f ( * )
echo $f | grep " "
if ( $? == 0 ) then
mv "$f" `echo $f | sed -e "s/ /_/g"`
@ n += 1
endif
end
echo $n changed

I need to write a script which renames all ascii text files in the current directory by adding a number to their names before the “extension”. for example <name>_a<number>.<”extension”> for example creditA, creditB, creditC, debitA get changed to creditA1, creditB2, creditC3, debitA4. That is for changing all the files on the server to have a number in them......

I have this code which is wrong

#!/bin/tcsh
set n = 0
set m = 0
foreach f ( * )
file $f | grep "ascii text"
if ( $? == 0 ) then
mv "$f" `echo $f"_a"$m`
@ n += 1
@ m += 1
endif
end


I need to divide the $f into two parts one part before the . and the other part after the . but I dont know how. Any help

Any help...
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-19-2008
Moderator
 

Join Date: Dec 2003
Location: /ksh93
Posts: 875
How do you plan to identify the files with asciii text names from non-ascii text names?
grep "ascii text" is not going to do it for you.

Also do you want to rename the files to <name>_<number>.<extension> or <name><number>.<extension>? The description you give does not match your code.
Reply With Quote
  #3 (permalink)  
Old 03-19-2008
Registered User
 

Join Date: Mar 2008
Posts: 24
Quote:
Originally Posted by abch624 View Post
Hi Alll,

I have a script that we use on the servers to change the name of files that have spaces in the name:

#!/bin/tcsh
set n = 0
foreach f ( * )
echo $f | grep " "
if ( $? == 0 ) then
mv "$f" `echo $f | sed -e "s/ /_/g"`
@ n += 1
endif
end
echo $n changed

I need to write a script which renames all ascii text files in the current directory by adding a number to their names before the “extension”. for example <name>_a<number>.<”extension”> for example creditA, creditB, creditC, debitA get changed to creditA1, creditB2, creditC3, debitA4. That is for changing all the files on the server to have a number in them......

I have this code which is wrong

#!/bin/tcsh
set n = 0
set m = 0
foreach f ( * )
file $f | grep "ascii text"
if ( $? == 0 ) then
mv "$f" `echo $f"_a"$m`
@ n += 1
@ m += 1
endif
end


I need to divide the $f into two parts one part before the . and the other part after the . but I dont know how. Any help

Any help...
try this

ctr=1
for i in *
do
var1=`file $i |awk '{printf "%s\n",$3}'`
if [ $var1 = "text" ]; then
filename=`ls $i | cut -f1 -d "."`
ext=`ls $i |awk -F "." '{printf "%s\n",$2}'`
mv $i $filename$ctr.$ext
ctr=`expr $ctr + 1`
fi
done

Thanks
Antony cecil
~
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0