copy and rename list of files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers copy and rename list of files
# 1  
Old 02-25-2005
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~!
# 2  
Old 02-25-2005
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  
Old 02-25-2005
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
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  
Old 02-26-2005
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  
Old 02-26-2005
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  
Old 02-27-2005
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oop to copy and rename files through SQL Statement in shell Script

#!/bin/sh sqlplus -s "/ as sysdba" << EOF SET HEADING OFF SET FEEDBACK OFF Select pt.user_concurrent_program_name , OUTFILE_NAME FROm apps.fnd_concurrent_programs_tl pt, apps.fnd_concurrent_requests f where pt.concurrent_program_id = f.concurrent_program_id and pt.application_id =... (1 Reply)
Discussion started by: usman_oracle
1 Replies

2. Shell Programming and Scripting

Linux Script to copy and rename files through SQL statement

Hi, I require help to complete below requirement through Linux Script. I have a SQL query which shows two columns as output. One is Report Name and other is report path. Query return multiple rows. below is the output. Report Name Cotton Stock Report (Net Weight)- Customized Output... (3 Replies)
Discussion started by: usman_oracle
3 Replies

3. Shell Programming and Scripting

Is better way copy list of multiple files, rename and gzip

Is better way to write the script loop through one by one "Old_File_1: & New_File_1:" to copy 100 files to /staging/test folder then re-name & gzip all those files? I wrote this code below and don't like that much. Thanks I have a control_file under /tmp/test folder like below 100 files and... (10 Replies)
Discussion started by: dotran
10 Replies

4. Shell Programming and Scripting

Copy down remote files and rename them to include the server name with full path

I need to pull down a good bit of files for another support team for an upgrade project. I have a server.list with all of the server names. I need to do two parts: FIRST: I have this example, but it does not list the server name in front of each line. #! /bin/bash for server in $(<... (10 Replies)
Discussion started by: asnatlas
10 Replies

5. Shell Programming and Scripting

How to Rename List of files in a directory

How can i rename list of files in a directory? (4 Replies)
Discussion started by: knip
4 Replies

6. Shell Programming and Scripting

Help with script to copy/rename files, then delete by date

Hi All, I am new to scripting and am looking for some assistance setting up a script. Basically I need the script to scan a folder for the newest files and make a copy of those files, adding a month to the date stamp. I also need this script to delete the previously copied files to save space.... (4 Replies)
Discussion started by: Lucid13
4 Replies

7. Shell Programming and Scripting

Files rename and copy

hello, I am write a Script and i would listing all Files from Path1 out with DSR*.txt and give a new name an copy to the Path2. I have problems with that to rename. Someone can help me? Sorry, for my english. My english is not gut. I hope you understand my. That is my Script. ... (2 Replies)
Discussion started by: efeijoo
2 Replies

8. Shell Programming and Scripting

Copy files from folder and rename them

hello, I need to build a shell script that receives the folder to copy by parameter and copy all files except thumb.db to another folder and rename them like, file.jpg renamed to file_bb1.jpg. can someone help me Thanks (4 Replies)
Discussion started by: zeker
4 Replies

9. UNIX for Dummies Questions & Answers

script to rename files with current date and copy it.

I have few webservers logs like access.log. which would be growing everyday. what i do everyday is, take the backup of access.log as access.log_(currentdate) and nullify the access.log. So thought of writing a script... but stuck up in middle. My requirement: to take the backup and nullify... (6 Replies)
Discussion started by: logic0
6 Replies
Login or Register to Ask a Question