scp of multiple files to remote server


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers scp of multiple files to remote server
# 1  
Old 02-18-2017
scp of multiple files to remote server

Hello,

I would like to scp multiple files to a remote server that requires a password for the process to be completed.

I have 30 folders (x_1, x_2 ... x_30), each containing 25 files. What I want to do is scp 1 out of the 25 files (file called bvals) for all my folders to a remote server and place each file to its respective folder (x_1, x_2 ... x_30 in the remote server).

I wrote this script:

Code:
for n in x_1 x_2 x_3 x_4 x_5 x_6 x_7 x_8 x_9 x_10 x_11 x_12 x_13 x_14 x_15 x_16 x_17 x_18 x_19 x_20 x_21 x_22 x_23 x_24 x_25 x_26 x_27 x_28 x_29 x_30
do
scp $HOME/data/${n}/bvals remote_server@remote.edu:/me/my_data/${n}
done

The problem with this script (of course) is that every time n takes a new argument (x_1 etc) I am asked to insert my password.

Is there any way to write this script in a way such that I am prompted to write my password only once?

Thank you!
Moderator's Comments:
Mod Comment Please use CODE tags (as required by the forum rules you agreed to when you joined this the UNIX & Linux Forums) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 02-18-2017 at 09:37 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 02-18-2017
Sure. Two logins okay?
Code:
# create a single tar file , scp the one file to the remote
cd /path/my/directories  # local place where directories are
tar cvf myfiles.tar  x_1 x_2 x_3 x_4 x_5 x_6 x_7 x_8 x_9 x_10 x_11 x_12 x_13 x_14 x_15 x_16 x_17 x_18 x_19 x_20 x_21 x_22 x_23 x_24 x_25 x_26 x_27 x_28 x_29 x_30
# scp tar file onto the remote box
scp myfiles.tar remote_server@remote.edu:/me/my_data/ 
#  extract all of the directories to the place they belong
ssh remote_server@remote.edu 'cd remote_server@remote.edu && tar xf myfiles.tar

If the tar file is gigantic you may want to compress it first, send it, uncompress on the other side, extract the new re-sized tar file.

There is actually another way to move the files directly, more complicated but for long transmissions runs into issues with crummy connections. This is my plan A when I know enough to feel okay about moving lots of data. I gave you plan B because it is less likely to bomb from a dropped connection.

The reason for this is that some ISP's in my area 're-establish' DHCP periodically, dropping the old connection.

Example:
Code:
cd /path/to/local/directories
tar cvf - . | ssh -e none  me@remote 'cd /path/to/remote  && ( tar xf - )'


Last edited by jim mcnamara; 02-18-2017 at 10:45 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-19-2017
Hi,

As an aside from the actual script: is it possible you could use SSH key-based authentication, rather than passwords ? This would enable you to connect without having to supply a password, with authentication taking place by means of a key exchange. This way you could transfer as many files as you liked as often as you liked without having to type in anything (though doing it in as few transfers as possible as per the solution you've been given is definitely a better way to go regardless).

If the source and destination servers are running OpenSSH, then the idea is that you would first generate a DSA or RSA public key for yourself. You might in fact find that you've got one or both of these already, stored as '.ssh/id_dsa.pub' or '.ssh/id_rsa.pub' underneath your home directory. Next, you must add the contents of your public key file to '.ssh/authorized_keys' in the home directory of the receiving account on the destination server.

After doing that you should be able to log in without being prompted for a password at all, so long as the permissions on all appropriate files and directories are secure enough at both ends. Essentially they must be accessible only by the user to whom the account belongs, more or less. Just a thought, if that's an option for you.
This User Gave Thanks to drysdalk For This Post:
# 4  
Old 02-19-2017
Did you consider using sftp (from the ssh suite) in "batch mode"?
This User Gave Thanks to RudiC For This Post:
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. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

3. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

4. Solaris

scp multiple files without pattern on Solaris

I need to transfer multiple files using scp between two solaris machines. I could not use pattern since they match with other files in the same directory. I tried the below command, it does not copy any files. $ scp -p user@machine1:/home/fid1/staging/\{a.ksh,b.ksh,c.ksh,d.ksh\} . But... (7 Replies)
Discussion started by: marecar
7 Replies

5. Shell Programming and Scripting

scp files that are 3 days older from remote server-

hello, i am trying to get a list of files to be scped from the remote server by running the below in my local unix server ( note - there is a passwordless connectivity setup made between the local and remote server) and, we use KSH. --- ssh $scp_host "find /a/b/c/*/ -iname "$remote_file"" >... (4 Replies)
Discussion started by: billpeter3010
4 Replies

6. Shell Programming and Scripting

scp or rsync multiple files in parallel from a remote host

Hi. I'm trying to speed up an rsync command by running it in parallel. There's no real option for this other than if the files are in multiple directories (which they're not). And even then there's no way of knowing if rsync has succeeded as the process is running in the background .. and... (4 Replies)
Discussion started by: Big_Jeffrey
4 Replies

7. Shell Programming and Scripting

ftp'ing multiple files to the remote server

unix shell script (2 Replies)
Discussion started by: giridhar276
2 Replies

8. UNIX for Dummies Questions & Answers

scp multiple files

Hi, I'm new to Linux. I would like to know how to scp a group of files. I have the below command, but it asks for remote password while sending each file. Is there a way to send all files (identified by - $ ls | grep '.*hrs0314a.*' | xargs -I {} grep -l '.*35663.*' {}) in one go? $ ls |... (6 Replies)
Discussion started by: don_tom
6 Replies

9. Shell Programming and Scripting

SCP multiple files

Hi , I am doing SCP for copying log files from different server(around 24 server) I need to copy these bulk logfiles every 15 min. How can i do multiple SCP at the same time? My current code is like this scp $CUSTCARE_USER@$CUSTCARE_SERVER:$CUSTCARE_HOME/$CUSTCARE_LOG.*... (2 Replies)
Discussion started by: scorpio
2 Replies

10. Shell Programming and Scripting

FTP multiple files from remote server to local server

Hi, I am facing a weired problem in my FTP script. I want to transfer multiple files from remote server to local server everyday, using mget * in my script. I also, want to send an email for successful or failed FTP. My script works for file transfer, but it don't send any mail. There is... (2 Replies)
Discussion started by: berlin_germany
2 Replies
Login or Register to Ask a Question