How to upload a file from the command line with FTP or SSH?

30

27

I have never done this before and I am creating a bash shell script to do this for me. I will also be connecting via ssh to do some things (which I already know how to do). So maybe there is a way to upload files via ssh so I can do it all in one connection?

How can I do this?

#!/bin/sh

cd ./files-to-upload
#upload the files

Andrew

Posted 2009-12-12T20:49:15.820

Reputation: 11 982

That does not answer this question, but maybe this could help some people: https://curl.io/ permits you to upload a file through bash to a public http hosting (valid for few hours i think)

– Vincent Fenet – 2015-11-19T13:58:43.647

Answers

56

You can use scp to copy to a remote machine.

scp <file to upload> <username>@<hostname>:<destination path>

tangens

Posted 2009-12-12T20:49:15.820

Reputation: 1 230

1@Felix should have pointed out that if you don't have the ssh key authentication set up, you will be prompted for a password. That means, having the that setup is not a critical requirement to use this command – aexl – 2016-03-29T12:17:39.287

WORKS! scp ~/Desktop/image.jpg root@server.com:/var/tmp – YumYumYum – 2017-12-13T07:33:24.977

can the password be passed with this as well? Also, does this initialize an ssh session? Can I run other commands as well? – Andrew – 2009-12-12T21:39:27.187

2No you cannot pass the password. But you can set up ssh key authentication. That means you do not authenticate via a password but via a key that is stored in a file and automatically transmitted on connection. And no again, you cannot execute other commands with this. – Felix – 2009-12-12T22:33:37.793

i mean can you connect with ssh, then run scp? Or are they separate commands? – Andrew – 2009-12-12T22:34:52.660

2ssh and scp are different commands. For an introduction see http://www.ssh.com/support/documentation/online/ssh/adminguide/32/Introduction_to_SSH_Secure_Shell.html – tangens – 2009-12-13T11:22:55.117

6

You're probably looking for SCP or similar.

#!/bin/bash

cd ./files-to-upload
scp * user@host:/path/to/upload/files/to

of course this must be tweaked to your liking.Replace user@host with your real information. You will be prompted for a password to upload.

John T

Posted 2009-12-12T20:49:15.820

Reputation: 149 037

4

If you really must use ssh (instead of scp) you can do:

for filename in *; do
  cat $filename | ssh user@host "cd /path/to/upload/files/to; cat - > $filename"
done

but regular scp (like tangens suggestion) is the best.

Jimmy Hedman

Posted 2009-12-12T20:49:15.820

Reputation: 886

Im using an FTp connection, could you please tell how to transfer or copy files from local to remote, im using filezilla or winscp... i need to extract a zip or copy from local to remote – Sushivam – 2016-09-26T06:12:35.057

1

scp is the better answer since it would be encrypted over SSH.

However, if you do want to do it over standard ftp, look at ncftpput. It's designed specificly to upload a file:

NAME ncftpput - Internet file transfer program for scripts

SYNOPSIS ncftpput [options] remote-host remote-directory local-files...

DuPie

Posted 2009-12-12T20:49:15.820

Reputation: 151

0

For ec2 instance, you have to pass the .pem file also,

$scp -i ~/Desktop/amazon.pem ~/Desktop/file.zip  ubuntu@ec2-54-166-128-20.compute-1.amazonaws.com:~/data/

Elavarasan

Posted 2009-12-12T20:49:15.820

Reputation: 101

0

curl is a good program that handles several protocols.

Joe Internet

Posted 2009-12-12T20:49:15.820

Reputation: 5 145

0

When you use the scp (secure copy) command it connects to the client and if you don’t already have a fingerprint saved for the host device it will ask you for the host password otherwise it should auto connect to the host I believe.

David

Posted 2009-12-12T20:49:15.820

Reputation: 1