FTP multiple files to different directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FTP multiple files to different directories
# 1  
Old 08-30-2004
FTP multiple files to different directories

The script below is written to ftp files to different directories on the destination server, but I'm not sure whether I have written the code correctly or not.

Can anyone help me on this?

#!/bin/sh

FILE_NAMES="FileA
FileB
FileC"

SERVER=xxxx
USERID=abcd
PASSWD=xxxxx
FOLDER="/folders/sub1/
/folders/sub2/
/folders/sub3/"

DIRECTORY=1;

(
echo "
open ${SERVER}
user ${USERID} ${PASSWD}
for folder in ${FOLDER}; do
cd ${folder}
COUNT=1;
for filename in ${FILE_NAMES}; do
if [ ${COUNT} -eq ${DIRECTORY} ]; then
put ${filename}
break;
fi
COUNT=`expr $COUNT + 1`
done
DIRECTORY=`expr $DIRECTORY + 1`
done
close
"
) | ftp -i -n

if [ $? -ne 0 ]; then
echo "File Upload Failed"
else
echo "File Upload Completed"
fi

exit 0

Last edited by abrd600; 08-30-2004 at 05:33 AM..
# 2  
Old 08-30-2004
Not gonna fly. You cannot echo sh statements into a ftp client. The ftp client knows "open" and "user" , so that makes sense. But you can't type "for" loops into an ftp client. So you can't echo a "for" loop in either and expect it to work. To see my solution to this sort of thing, navigate:

our home page -> Answers to Frequently Asked Questions -> Automate FTP / Scripting FTP Transfers -> For Loops Within Ftp Comands
# 3  
Old 08-31-2004
Thank you Jim.

Can I check whether the ftp was successful using the script you have given as example?

#! /usr/bin/ksh

HOST=remote.host.name
USER=whoever
PASSWD=whatever

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p binary
for job2 in 3t 11 12 13 ; do
print -p cd $job2
CFNAME="$job2".chk
print -p put /home/salazar/chk_data/$CFNAME $CFNAME
done
print -p bye
wait
exit 0
# 4  
Old 08-31-2004
Um...who's Jim? The code looks good, but why not try it? That's how I determine if code works or not.

Checking the return code does not work. The ftp clients seem to return 0 if the connection attempt was successful. You could cd to a second local directory and run a second ftp job to retrieve the files you just transferred. Then compare the retrieved files with the original files. This would be expensive if the files are large. You could also use the ftp dir command and parse the output. This is quite a chore but the recursive ftp script linked in that faq article shows how I did it.
# 5  
Old 08-31-2004
Sorry Perderabo, I accidentelly typed that.

Now I have got the concept.

Thank you so much for your explanation.
# 6  
Old 09-21-2004
The code given below is not working when the target (HOST) server is
"Machine hardware: sun4u
OS version: 5.8"

#! /usr/bin/ksh

HOST=remote.host.name
USER=whoever
PASSWD=whatever

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p binary
for job2 in 3t 11 12 13 ; do
print -p cd $job2
CFNAME="$job2".chk
print -p put /home/salazar/chk_data/$CFNAME $CFNAME
done
print -p bye
wait
exit 0

I'm getting a message like "Connection Timeout".
Can anyone tell me the reason why it is not ftping?
# 7  
Old 09-21-2004
I tried your script and it worked for me....mostly. Either you have a bug or a very odd directory structure.

After you do
print -p cd $job2
after the first iteration through the loop, you are in the 3t subdirectory. The next time you will be in 3t/11 then in 3t/11/12 and so on.

Maybe you want a
print -p cd ..
or maybe not... who knows?

But this is not a "connection timeout". Can you ftp manually to the host in question? I would expect that to fail too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compress Files in Multiple Directories

I would like to compress the files in multiple directories. For some reason, it only compress the first directory (/Sanbox/logs1) but not the rest of the other directories ("/Sanbox/logs2" "/Sanbox/logs3" "/Sanbox/logs4" ). Any help would be appreciated. Here's my code: #!/bin/bash... (1 Reply)
Discussion started by: Loc
1 Replies

2. Shell Programming and Scripting

Count files between multiple directories

Hi All, Everyday we will receive 33 files in our source directory(/prd/pk) with the current date. Once our jobs are completed all the 33 files immediately will be moved to backup folder (/prd/pk/backup). Now, I need to check between source file directory (/prd/pdk) and backup file directory... (3 Replies)
Discussion started by: suresh_target
3 Replies

3. Shell Programming and Scripting

Renaming files in multiple directories

Hi I have the following file structure and I want to rename all the abc.jar files to abc_backup.jar rock@server:~/rakesh> ls -R .: test1 test2 test3 ./test1: abc.jar ./test2: abc.jar ./test3: abc.jar (2 Replies)
Discussion started by: rakeshkumar
2 Replies

4. Shell Programming and Scripting

Allow FTP user to access multiple directories

Hi Experts, I am in urgent need of your suggestions. I have below two users in my system: xyz:x:101:101:XYZ System Account:/export/home/xyz:/bin/bash abc:x:2009:10:ftp user only:/export/home/abc:/bin/false Where "xyz" is the crucial one and "abc" is only introduced for FTPing the... (2 Replies)
Discussion started by: sugarcane
2 Replies

5. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

6. Shell Programming and Scripting

FTP multiple files from multiple directories

I have multiple files that starts as TRADE_LOG spread across multiple folders in the given structure.. ./dir1/1/TRADE_LOG*.gz ./dir2/10/TRADE_LOG*.gz ./dir11/12/TRADE_LOG*.gz ./dir12/13/TRADE_LOG*.gz when I do ftp uisng mput from the "." dir I am getting the below given error mput... (1 Reply)
Discussion started by: prasperl
1 Replies

7. UNIX for Advanced & Expert Users

ftp files with directories

hi, i am trying to ftp files from one server to another. how do i do it with all directories.(i mean i need the same directory structure) mget -r * doesnt seem to work properly..pls help. (5 Replies)
Discussion started by: dnat
5 Replies

8. Shell Programming and Scripting

cd to multiple directories and gzipping files within

I am working on a Korn shell script that will cd into multiple directories that are listed in a flat file and gzip the contents of each directory. So far, this is what I have: 1) Find the 4 largest directories and place them into the file foo du -sk /some/directory/* | sort -rn | head -4... (0 Replies)
Discussion started by: sunsysadm2003
0 Replies

9. Shell Programming and Scripting

get files from multiple directories using FTP

Hi, I need to download multiple files in multiple directores from other Unix server. The files could have been created by differnet user. I can use root account but I want the preserve the owner and time of the remote file. Need help on how I can do this using FTP. Thanks, Amit (2 Replies)
Discussion started by: amit1209
2 Replies

10. UNIX for Dummies Questions & Answers

copy multiple files in different directories

I have a report file that is generated every day by a scheduled process. Each day the file is written to a directory named .../blah_blah/Y07/MM-DD-YY/reportmmddyy.tab I want to copy all of this reports to a separate directory without having to do it one by one. However, if I try cp... (3 Replies)
Discussion started by: ken2834
3 Replies
Login or Register to Ask a Question