UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old August 19th, 2004, 02:07 PM
Timtro Timtro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 30 Timtro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 56 m 30 sec
Reputation Power: 5
Simple Shell script question

Hi guys. Please forgive me as this is my first script of this nature. My previous scripting experience is limited to input file generation and auto execution of scientific software.

I have a directory full of stuff to rename. For such, I have written the following script:

Code:
num=0
basename="5FFA45-"

for curf in *
do	
	ftmp=`echo $basename$num`
	echo $ftmp         #This is where I will be putting the 'mv' command
	let "num += 1"
done


Looking at this, my intensions are clear for the most part. I need something that will go through all the files, and will rename them with a common base-name, and add a number that cooresponds to the order in the directory listing.

My problem is that I want to be able to preserve file extensions. How can I do this?

Reply With Quote
  #2  
Old August 19th, 2004, 02:49 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
Code:
#!/bin/ksh             
let count=0
basename="5FFA45-"

for file in *
do
  file_ext=${file#*.*}  
  echo $basename"$count".$file_ext  
  # change the above line to: mv $file $basename"$count".$file_ext
  let count=$count+1
done
exit  

-- this will work on files with a single . and with no spaces in their names.

Reply With Quote
  #3  
Old August 19th, 2004, 03:31 PM
Timtro Timtro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 30 Timtro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 56 m 30 sec
Reputation Power: 5
Hmmm. That looks interesting. I'm not sure that I understand some of it.

What do you think of this?

Code:
#! /bin/bash
#
#  A simple scipt to rename a bunch of files
#  in a directory, such that the extensions
#  are preserved.
#
#		by Timothy A.V. Teatro
#
#		Date: 08.19.04


num=0
basename="5FFA45-"

for curf in *
do	
   ext=`echo $curf | awk -F"." '{ print $2 }'`
	ftmp=`echo $basename$num.$ext`
	mv "$curf" "$ftmp"
	let "num += 1"
done


Can you explain in a little more depth exactly what that line does? I am intruiged by it.

Thank you very much for replying.

Reply With Quote
  #4  
Old August 19th, 2004, 04:55 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
Code:
file_ext=${file#*.*}  

removes everything up to and including the dot in the variable: file. It gets the extension just like your awk statement does, except it's a builtin korn/bash substitution function.

and..
Code:
let "num += 1"
should be:
let num+=1 

lose the spaces spaces; you don't really need the quotes.

Reply With Quote
  #5  
Old August 27th, 2004, 02:21 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
timtro
did you already try to read&&write sh-scripts ?
i know, you && a lot other people like 'ksh' and 'ksh'
is still NOT standard, the only really standard is 'bourne'

your script is very dangerous, you don't check the existence of
the new file...

NOTA:
- basename is a unix-cmd, don't use it, could be very confusing.
- don't abuse of ", use it when it's needed.

try something like:
Code:
#!/usr/bin/sh
num=0
newname=5FFA45-

for curf
do	
	ftmp=`echo $curf | sed 's/^.*\(\..*\)/\1/`
        [ x$tmp = x ] && continue # extention not found
	[ -f $newname$num$ftmp ] && continue # file exists
        mv $curf $newname$num$ftmp

	num=`expr $num + 1`
        # or better num=`echo $num + 1 | bc`
        # it's faster by large op
done

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Simple Shell script question


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway