The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to rename files with .1,.2,.3 ....ext respectively satyajit007 Shell Programming and Scripting 4 02-06-2009 06:43 AM
How to Rename/Convert Files in Shell Scripting? hanu_oracle Shell Programming and Scripting 11 10-21-2008 09:13 AM
Script to rename files cpreovol Shell Programming and Scripting 3 04-01-2008 11:45 AM
Script to rename files Dinkster UNIX for Dummies Questions & Answers 5 01-22-2008 08:55 AM
rename files using shell scripting gfhgfnhhn Shell Programming and Scripting 4 07-04-2006 04:37 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-21-2009
yakuzaa yakuzaa is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 1
Smile Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension

so for example, a directory containing the files


Hello.txt
Hello.t
Hello

should have the following changes,

Hello.txt should be converted to Hello_a1.txt
Hello.t should be converted to Hello_a2.t
Hello should be converted to Hello_a3

so it should put a _a and a number incrementing accordingly.
*********************************************************

Im new to tcsh scripting, though i tried implementing it, and this is where i get to.


#!/bin/tcsh
set n = 1

foreach f ( * )
echo file $f == ascii
if ( $? == 0 ) then
mv "$f:r" "$f:r"_a$n
@ n += 1
endif
end
echo $n changed

I am aware that some parts are incorrect, and that I might be missing something, thats where i need help.

thanks.
  #2 (permalink)  
Old 03-21-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,860
You know you should not use (t)csh for scripting

But if you insist you may try this:

Code:
#!/bin/tcsh

set n = 0

foreach f ( * )
  set _type = `file "$f"`
  switch ( "$_type" )
    case *ASCII*:
      @ n ++
      switch ( "$f" )
        case *.*:
          mv -- "$f" "$f:r"_a"$n"."$f:e"
          continue
          breaksw
      endsw
      mv -- "$f" "$f"_a"$n"
      breaksw
    endsw
end
echo "$n file(s) changed."
  #3 (permalink)  
Old 03-21-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by yakuzaa View Post
Hi, i need a bit of help writting a tcsh script
[t]csh is not recommended for scripting:

Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful
Quote:

which renames all ascii text files in the current directory by adding a number to their names before the extension

so for example, a directory containing the files

Hello.txt
Hello.t
Hello

should have the following changes,

Hello.txt should be converted to Hello_a1.txt
Hello.t should be converted to Hello_a2.t
Hello should be converted to Hello_a3

so it should put a _a and a number incrementing accordingly.
*********************************************************

Im new to tcsh scripting, though i tried implementing it, and this is where i get to.


#!/bin/tcsh
set n = 1
foreach f ( * )
echo file $f == ascii
if ( $? == 0 ) then
mv "$f:r" "$f:r"_a$n
@ n += 1
endif
end
echo $n changed

Code:
#!/bin/sh
n=1
for f in *
do
  ftype=$(file "$f")
  case $ftype in
     *ASCII*) ext=${f%%*.}
              mv "$f" "${f#.*}_a$n.$ext}"
              n=$(( $n + 1 ))
              ;;
  esac
done
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0