Copying and renaming files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying and renaming files
# 1  
Old 03-27-2014
Copying and renaming files

hi,

Code:
 
source directory as /home/home01
target directort as /home/home02

I have below files in source directory:
Code:
 
FrontOf_history.dat
FrontOf_history1.dat

In target directory have many files as:
Code:
 
Kront_2014.dat
Kront_2015.dat
Kront_2016.dat

Now i want to copy the two files present in source_dir to target_dir
after copying the two files i want to rename these two files as:
Code:
 
FrontOf_history_$date.dat
FrontOf_history1_$date.dat

where $date is a input parameter string

I have written below script for that:

Code:
 
date=`awk 'NR>5; END{print}' /home/home03/date.dat`
source_dir="/home/home01"
target_dir="/home/home02"
cd $source_dir
ls FrontOf_*.dat >>/tmp/abc.dat
file1=`grep "FrontOf*" /tmp/abc.dat
cp ${source_dir}/${file1} ${target_dir}
ls ${target_dir} | while read file1
do
        pre=`echo ${file1} | cut -d"_" -f1`
        post=`echo ${file1} | cut -d"_" -f3-4`
        new_name1="${pre}_${date}_${post}"
        mv $target_dir/$file1 $target_dir/$new_name1
done

Problem with this code is that it is renaming other files also that in present in target_dir.I just want to rename the two files that I copied from source to target directory.

Last edited by Vivekit82; 03-27-2014 at 01:17 PM..
# 2  
Old 03-27-2014
If I understand your problem statement correctly, this should do what you want.

Code:
date=`awk 'NR>5; END{print}' /home/home03/date.dat`
source_dir="/home/home01"
target_dir="/home/home02"

## Here the core logic.
for i in `ls ${source_dir}/FrontOf_history*.dat`;do
  fname=`basename $i|sed "s/\(.*\)\.*/\1"`
  cp $i ${target_dir}/${fname}_${date}.dat
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to a directory, renaming it if a file with the same name already exists

Hi All, I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory. THanks, Dev (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

2. Shell Programming and Scripting

Copying files from one directory to another, renaming duplicates.

Below is the script i have but i would like simplified but still do the same job. I need a script to copy files not directories or sub-directories into a existing or new directory. The files, if have the same name but different extension; for example 01.doc 01.pdf then only copy the .doc file. ... (1 Reply)
Discussion started by: Gilljambo
1 Replies

3. UNIX for Dummies Questions & Answers

Renaming files with part of their pathname and copying them to new directory

Hi I think this should be relatively simple but I can't figure it out. I have several files with the same name in different folders within a directory (the output of a program that I ran). Something like this: ./myAnalysis/item1/round1/myoutput.txt ./myAnalysis/item1/round2/myoutput.txt... (2 Replies)
Discussion started by: jullee
2 Replies

4. Shell Programming and Scripting

Copying and Renaming script

Hey all! Sorry for the noobish question.. I am wondering how to I copy multiple files, and rename them based on their directory at once. So, I have files like this /logs/server/2011-10/server-1/log/file/folder/some.log.gz /logs/server/2011-10/server-2/log/file/folder/some.log.gz... (3 Replies)
Discussion started by: m3power206
3 Replies

5. UNIX for Dummies Questions & Answers

Copying and Renaming file through standard input

Hi Geeks, I am relatively new to Unix. Trying out to achive a shell script by hard learning. Here is my requirment. 1. I have to search for specified strings that are given in .csv file in the directory to find the files for matching strings in the .csv file. 2. If match is found, copy... (1 Reply)
Discussion started by: uunniixxuusseer
1 Replies

6. Shell Programming and Scripting

Renaming and copying the file from one to another

Hi, There is file generated automatically at /usr/files as fileYYYYMM(e.g file201005 and so on). I need a script that will i)pick up the latest file from that path ii)rename the copied file to fileYYYYMM and then iii)copy to another server at path /usr/dest.If the file name with same name... (1 Reply)
Discussion started by: Alok Ranjan
1 Replies

7. Shell Programming and Scripting

Copying files and renaming

The goal is to read names of files defined ouside in upload.conf and rename them using date, time and proper extension. I have made short script while read; do cp "$RELAY" "$RELAY(date +%Y-%m-%d_%H:%M:%S)_DEPLOYED.ear" done < upload.conf but unfortunatelly it fails printiong the... (9 Replies)
Discussion started by: Michal Janusz
9 Replies

8. Shell Programming and Scripting

Need help copying and renaming files

I would like to copy files from one directory to another directory while renaming them at the same time. Does anyone have any ideas on how to do this in a script? Basically, would like to copy files from /user/data/prod/*.txt to /user/data/bck/*.txt.bck (3 Replies)
Discussion started by: thunderkiss65
3 Replies

9. Shell Programming and Scripting

copying files and Renaming them + shell script

Hi, I have a problem. I have some text files in a folder. The names can be like: emp.txt emp1.txt emp3.txt 32emp4.txt What i need is i have to copy all the files which have "emp" string in their filename to a different folder and those file names... (7 Replies)
Discussion started by: pathanjalireddy
7 Replies
Login or Register to Ask a Question