copy and rename list of files | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

copy and rename list of files

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-25-2005
kinmak kinmak is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 3 July 2008, 5:15 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Data 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~!
Sponsored Links
    #2  
Old 02-25-2005
RTM's Avatar
RTM RTM is offline Forum Advisor  
Registered User
 
Join Date: Apr 2002
Last Activity: 17 October 2014, 8:46 PM EDT
Location: On my motorcycle
Posts: 3,093
Thanks: 1
Thanked 32 Times in 9 Posts
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.
Sponsored Links
    #3  
Old 02-25-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
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  
Old 02-25-2005
kinmak kinmak is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 3 July 2008, 5:15 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #5  
Old 02-25-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
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
Sponsored Links
    #6  
Old 02-26-2005
kinmak kinmak is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 3 July 2008, 5:15 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
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:
Sponsored Links
    #7  
Old 02-27-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with script to copy/rename files, then delete by date Lucid13 Shell Programming and Scripting 4 04-05-2012 02:10 PM
Files rename and copy efeijoo Shell Programming and Scripting 2 01-13-2010 05:22 PM
Copy files from folder and rename them zeker Shell Programming and Scripting 4 12-24-2009 04:49 AM
find list of files from a list and copy to a directory manishabh Shell Programming and Scripting 3 09-13-2009 06:03 PM



All times are GMT -4. The time now is 10:25 AM.