SFTP Multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SFTP Multiple files
# 1  
Old 04-15-2015
Hammer & Screwdriver SFTP Multiple files

Hi Smilie,
I am new to Unix and Shell Script
I have a urgent requirement, where i am new to shell script and Unix

my requirement is I have a files in a folder like
Code:
sales-prod-India-details.txt
sales-prod-japan-details.txt
sales-prod-china-details.txt

My SFTP server has
Code:
/Sales/location/India
/Sales/location/Japan
/Sales/location/China

each file to move to there respective folder using SFTP

India file must go to India folder under /sales/location
Japan file must go to Japan folder under /sales/location
China file must go to China folder under /sales/location

can any one help me with the shell script it will be much helpfull

thanks in advance

Moderator's Comments:
Mod Comment Please use code tags

Last edited by jim mcnamara; 04-15-2015 at 05:44 PM..
# 2  
Old 04-15-2015
Assuming you have ssh keys setup for passwordless sftp

Code:
send()
{
    fname="$1"
    dir="$2"
    sftp remotenode<<-EOF
    cd $dir
    put "$fname"
    exit
EOF
}

cd /path/to/sendfiles
bdir="Sales/location/"
for f in *details.txt
do
     country="$(echo "$f" | awk -F '-' {print $3}' )"
     send "$f"  "${bdir}${country}"
done

This assumes that there is no need to mess with capitalization of the country in the file name. Your example is not clear on that. UNIX commands are VERY picky about capital versus lowercase letters
# 3  
Old 04-15-2015
Untested, but something like...
Code:
#! /bin/bash

user="enter username here"
hostname="enter hostname here"

ls sales-prod-*-detail.txt | while read a
do
determinant=`awk -F\- '{print $3}'`
case ${determinant} in
        "[iI]ndia") 
                sftp ${a} ${user}@${host}:/Sales/location/India
                ;;
        "[jJ]apan")
                sftp S{a} ${user}@${host}:/Sales/location/Japan
                ;;
        "[cC]hina")
                sftp s{a} ${user}@${host}:/Sales/location/China
                ;;
esac

# 4  
Old 04-15-2015
Hi thanks it looks so simple

as i am new to scripts can i know how this works in line

Thanks

---------- Post updated at 08:05 PM ---------- Previous update was at 07:29 PM ----------

I tried writing this can some one correct this if itis wrong

Code:
#! /bin/bash

CURRENT_FILES_PATH = "<CURRENT SOURCE FILE PATH>"
B2B_FILE_PATH = "<DESTINATION FILE PATH>"
user="enter username here"
hostname="enter hostname here"

FILES=CD CURRENT_FILES_PATH/*
for f in $FILES
do
l=`echo $f | cut -d "-" -f3`
echo "$f -> $l
echo $l;

sftp ${user}@${host}
cd ${CURRENT_FILES_PATH}
get ${f}
put ${B2B_FILE_PATH}/*$l*

EXIT
done


Last edited by Don Cragun; 04-15-2015 at 10:37 PM.. Reason: Add CODE tags, again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename (move) multiple files on remote server using sftp

I want to rename (move) multiple files on remote server. I tried the following command to move all TXT files from my_dir directory to /new_dir. But it does not work. Any help? #!/bin/ksh sftp -dev3 << ABC cd my_dir $(for i in TXT; do echo "ls *.$i" ; rename $x /new_dir/$x;... (1 Reply)
Discussion started by: Soham
1 Replies

2. Shell Programming and Scripting

Sftp script to get multiple files at the same time

i have to log into an sftp server to get multiple files. im typing this post from a remote location and i dont have the script i wrote with me. but i got the sftp script to work by logging into the sftp server file by file. meaning, sftp to the server, "mget -p" or "get -p" one file at a time.... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

4. UNIX for Dummies Questions & Answers

Sftp multiple files in single command

Hi All, I would like to sftp 2 files with a single command. I tried the below options, sftp suer@test13:"/u01/home/oracle/SetDb.sh /u01/home/oracle/.profile" ./ But what actually happens is Fetching /u01/home/oracle/SetDb.sh to /u01/home/oracle/.profile /u01/home/oracle/SetDb.sh ... (3 Replies)
Discussion started by: sid2013
3 Replies

5. Shell Programming and Scripting

SFTP multiple options

Hi, I am trying to SFTP files in a script that i created. But the problem is i have to use -oPort and -b together. how can i get this done. I have tried as below command in my script but with no luck sftp -oPort=102 -b <batchfilename> username@server sftp -oPort=102 -ob... (1 Reply)
Discussion started by: ramkiran77
1 Replies

6. Red Hat

Chroot sftp users, remote sftp login shows wrong timestamp on files

Hello, I have a weird issue, I have RHEL 5.7 running with openssh5.2 where sftpgroup OS group is chroot. I see the difference difference in timestamp on files, when I login via ssh and SFTP, I see four hour difference, is something missing in my configuration. #pwd... (8 Replies)
Discussion started by: bobby320
8 Replies

7. Shell Programming and Scripting

help to upload multiple files through SFTP

Hi Experts, Please help me to write the expect script for uploading multiple files in one shot . Below is my program that I have written. #!/usr/local/bin/expect -f #/home/kulbhushan/sftp_prog.sh # procedure to attempt connecting; result 0 if OK, 1 otherwise proc connect {passw} { expect... (1 Reply)
Discussion started by: kulbhushan
1 Replies

8. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

9. Solaris

how to get multiple files using sftp from a windows server

I need to get multiple files from a windows server to a solaris server using sftp, I tried it but only can get one file at a time ( I'm unable to use a wild card character using sftp) hoe do i do this. any light on this is appreciated. Ram. (3 Replies)
Discussion started by: ramky79
3 Replies

10. UNIX for Dummies Questions & Answers

sftp multiple files and subdirectories

Hi, I'm new to using the sftp command prompt within the unix shell (usually I just use an FTP windows program). I've successfully connected to a remote server from within ssh. I've also been able to copy multiple files using the mget command e.g. mget *.html How do I copy all files,... (3 Replies)
Discussion started by: robbieg
3 Replies
Login or Register to Ask a Question