Issue with copying files into dir inside for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with copying files into dir inside for loop
# 1  
Old 10-01-2013
Issue with copying files into dir inside for loop

Hi ,
I'm trying to move/copy the files inside the loop into a directory .
I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file

Code:
file_path="/home/etc"
 Last_Day=20130930
 
mkdir $file_path/ARC_${Last_Day}
 
  for files in $file_path/AB*$Last_Day*
  do 
    dest_dir=$ARC_${Last_Day}
    cp $file_path/$Last_Data $file_path/$dest_dir
 
   done

When i check with set -x the last cp command is interpreted as
cp /home/etc/AB_20130930.log /home/etc/20130930

Please suggest.. Thank You
# 2  
Old 10-01-2013
If I understood correctly , you mean to say copy files from /home/etc/AB*20130930* to /home/etc/ARC_20130930/

Code:
file_path="/home/etc"
Last_Day=20130930
 
if [ ! -d "$file_path/ARC_${Last_Day}" ]
then
	mkdir -P $file_path/ARC_${Last_Day}
fi
 
dest_dir=${file_path}/ARC_${Last_Day} 
for files in $file_path/AB*${Last_Day}*
do 
    echo "cp $files ${dest_dir}/
	#cp $files ${dest_dir}/
 
done

First please check output of echo command, if it is satisfy your requirement, then only remove echo part and uncomment copy command which is after echo command.
This User Gave Thanks to pravin27 For This Post:
# 3  
Old 10-01-2013
Thank You Pravin. It worked
# 4  
Old 10-01-2013
The directory you created is not pointed to by the dest_dir variable:
Code:
mkdir $file_path/ARC_${Last_Day}   --> = /home/etc/ARC_20130930
dest_dir=$ARC_${Last_Day}          --> = ""20130930

as variable ARC_ is undefined. Why don't you use the same single variable to make the directory and then point the cp target to it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL - Copying ONLY files from one dir to another

I'm writing a Perl script which has its 1st step as to copy files from one directory to another directory. The Source directory has got files with extension, without extension, directories etc. But I want to copy ONLY files with no extension. The files with extensions and directories should not get... (2 Replies)
Discussion started by: jhamaks
2 Replies

2. Shell Programming and Scripting

Issue with accessing value inside while loop, outside it

Hi, GetName() { if then echo " Please enter the name: " read Name tempvar=0 while read line do if then tempvar=`expr $tempvar + 1` echo $tempvar ... (10 Replies)
Discussion started by: rituparna_gupta
10 Replies

3. Shell Programming and Scripting

Issue with copying files

Hi I have to write a shell script which copy files from one folder to another. When I try to do it directly from command prompt cp filename.dat /outgoing/filename.dat its working fine. But when I put the same command inside a shell script say test.sh its not getting copied. and when I check $?... (3 Replies)
Discussion started by: ravinunna
3 Replies

4. Shell Programming and Scripting

Copying files to new dir structure.

I am trying to figure out a way to script copying specific files from one dir structure to another. I have a dir structure like this: dira/author 1/book 1/file a.epub /book 2/file b.epub /author 2/book 1/file c.epub /author 3/book 1/file d.epub /book 2/file... (2 Replies)
Discussion started by: arcanas
2 Replies

5. Shell Programming and Scripting

issue while copying file dynamically whith in loop?

I need to copy the log file dynamically and that should run in loop , which means it should pick what ever the latest file is updated in that directory. I am able to display the list and copy to directly but i have no idea on how to pick the dynamically updated files. when i use this code, i... (1 Reply)
Discussion started by: johninweb
1 Replies

6. Shell Programming and Scripting

Copying specific files from one dir to another

Hi Folks, I have one curious case. There are list of following files placed in one directory such as... And updated each month. files.JAN09.csv files.FEB09.csv files.MAR09.csv ..... Now, I need to move a specific files; i.e, For this month, I need to move only OCT09, NOV09, DEC09,... (1 Reply)
Discussion started by: Jerald Nathan
1 Replies

7. UNIX for Dummies Questions & Answers

Permissions issue after copying files

Hi everyone, I am using mac os x 10.6, and I just copied over a project from a machine with 10.5... And I noticed my ls color is very funky in this directory... I found that my permissions are all messed up, and am wondering if there is a way to recursively fix permissions? This is how they... (3 Replies)
Discussion started by: patrick99e99
3 Replies

8. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 Replies

9. Shell Programming and Scripting

copying files to new dir

Hello, I'm new to shell scripting so I don't really understand what I'm doing wrong. The script I'm trying to do saves all the files (*.c) on the current dir to a list and, one by one, copies them to a new one called Backup. The thing is, if there are already other versions of the files I'm... (6 Replies)
Discussion started by: G3nn
6 Replies

10. Shell Programming and Scripting

copying a file from one dir to another dir

hi i have a script compareFiles() { find /tmp/Satya -type f | \ while read filename1 do echo "----------------------------------------$filename1" find /tmp/Satya -type f | \ while read filename2 do if diff $filename1 $filename2 then echo "Both files... (3 Replies)
Discussion started by: Satyak
3 Replies
Login or Register to Ask a Question