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 > 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
How to List and copy the files containing a string redlotus72 UNIX for Dummies Questions & Answers 11 09-28-2007 11:58 AM
script to rename files with current date and copy it. logic0 UNIX for Dummies Questions & Answers 6 05-01-2007 05:13 AM
rename files mpang_ Shell Programming and Scripting 2 11-01-2006 01:17 PM
please help - script to list and rename happyv Shell Programming and Scripting 2 10-04-2006 03:50 AM
Mass Copy/rename lwilsonFG UNIX for Dummies Questions & Answers 6 11-03-2005 06:55 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-25-2005
kinmak kinmak is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 17
Unhappy copy and rename list of files

Hi all,

I am a newbie in writng unix..I am using ksh shell..Does anyone know how to copy a list o files from directory A to directory B with differnt names? i.e
in Dir A, I have
RPT101.555.TXT
RPT102.666.TXT

and I want to copy those files to dir B with new naming convention..
in Dir B, I wanna have those files names

RPT.101.555.20050225163012.TXT
RPT.102.666.20050225163013.TXT
...

where 20050225163012 is infact the timestamp when I copy those files
format is yyyymmddHHmmss

I try to use comand sed ..but I am in big mess right now..

please help.. Thx~!
  #2 (permalink)  
Old 02-25-2005
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
You posted two different file output in this post - wasn't sure which is correct.
Input file should be RPT101 or RPT.101 - script below assumes RPT101 - just add 'cutting' another field if that incorrect.

Code:
#!/bin/ksh
files=`ls -1 RPT*.TXT`
for xx in $files
do
    newname=`echo $xx|cut -d. -f1,2`
    now=`date '+%Y%m%d%H%M%S'`
    cp $xx ./junk/$newname.$now.TXT
done
Just change or add any specific directory path - should work if I understood what you are looking for.
  #3 (permalink)  
Old 02-25-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Here's one that'll work regardless of which input filename format (RPT101.*.TXT or RPT.101.*.TXT) is used....
Code:
$ cd /dirA
$ ls -1 *.TXT
RPT.102.666.TXT
RPT101.555.TXT
$ cat > rename.ksh
#!/bin/ksh

ls *.TXT | while read file
do
  cp "$file" /dirB/${file%.*}.`date '+%Y%m%d%H%M%S'`.TXT
done
^D
$ chmod +x ./rename.ksh
$ ./rename.ksh
$ ls -1 /dirB/*.TXT
RPT.102.666.20050226024347.TXT
RPT101.555.20050226024347.TXT
Cheers
ZB
  #4 (permalink)  
Old 02-25-2005
kinmak kinmak is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 17
Thx for all reply first. To be more specific, this is my data..

SSSBR101R01A123.D20041224.TXT

I want to copy this file to another directory with a new name

SSSBR101.R01.A123.RPTDUMMY.D20041224.20040226.183033.BIN
|________|
This is the current date and time yyyymmdd.HHmmss

=========================
converning the solution given above.. I try the follwing and put the news files in teh same dir after renaming and simpfy the rename convention..but I got this error when I try to run the script..

[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: ls
SFSBR101R01I123.D20050131.TXT SFSBR101R01ITLS.D20050131.TXT
SFSBR101R01IABF.D20050131.TXT rename.ksh
[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: ls
SFSBR101R01I123.D20050131.TXT SFSBR101R01ITLS.D20050131.TXT
SFSBR101R01IABF.D20050131.TXT rename.ksh
[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: cat rename.ksh
#! /bin/sh

ls *.TXT | while read file
do
cp "$file" ${file%.*}.`date '+%Y%m%d%H%M%S'`.TXT
done
^D[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: ls -l rename.ksh
-rwxr-xr-x 1 devuser dba 106 Feb 26 11:24 rename.ksh
[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: rename.ksh
ksh: rename.ksh: not found
[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt:

why?
  #5 (permalink)  
Old 02-26-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by kinmak
[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: rename.ksh
ksh: rename.ksh: not found
The current directory is not included in your path - invoke the script with

./rename.ksh

Cheers
ZB
  #6 (permalink)  
Old 02-26-2005
kinmak kinmak is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 17
ZB,

thx for yr reply but same problem even I re-run it like following,

[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: ls
SFSBR101R01I123.D20050131.TXT SFSBR101R01ITLS.D20050131.TXT
SFSBR101R01IABF.D20050131.TXT rename.ksh
[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt: ./rename.ksh
ksh: ./rename.ksh: not found
[devuser]/project/sfs/ostdev/ftp/snd/ondemand/rpt_txt:
  #7 (permalink)  
Old 02-27-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
For a start, make sure that you change the shebang line to
#!/bin/ksh
as there are korn shell specific builtins used....

Also, remove the ^D from the end of the script - that was in my output because I scripted on the fly redirecting cat to a file....

Try doing

ls -lb .

to check that no non-printing characters have been put into the rename.ksh filename as you entered it in.... if you see some octals in there - that's the culprit....

Let us know if that makes any difference.
Sponsored Links
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:38 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