Upload files to server linux

How can I upload (FTP) files to server in a Bash script?

I’m trying to write a Bash script that uploads a file to a server. How can I achieve this? Is a Bash script the right thing to use for this?

11 Answers 11

Below are two answers. First is a suggestion to use a more secure/flexible solution like ssh/scp/sftp. Second is an explanation of how to run ftp in batch mode.

A secure solution:

You really should use SSH/SCP/SFTP for this rather than FTP. SSH/SCP have the benefits of being more secure and working with public/private keys which allows it to run without a username or password.

You can send a single file:

For more details on setting up keys and moving files to the server with RSYNC, which is useful if you have a lot of files to move, or if you sometimes get just one new file among a set of random files, take a look at:

You can also execute a single command after sshing into a server:

ssh [. snipped. ] hostname [command] If command is specified, it is executed on the remote host instead of a login shell.

ssh username@hostname.example bunzip file_just_sent.bz2 

If you can use SFTP with keys to gain the benefit of a secured connection, there are two tricks I’ve used to execute commands.

First, you can pass commands using echo and pipe

echo "put files*.xml" | sftp -p -i ~/.ssh/key_name username@hostname.example 

You can also use a batchfile with the -b parameter:

sftp -b batchfile.txt ~/.ssh/key_name username@hostname.example 

An FTP solution, if you really need it:

If you understand that FTP is insecure and more limited and you really really want to script it.

#!/bin/sh HOST='ftp.example.com' USER='yourid' PASSWD='yourpw' FILE='file.txt' ftp -n $HOST  

I would like to do this! Can you please expand on how I can do this? I need to do some things with ssh after uploading the file. Can this be done in one session?

While it was useful advice for the OP, it shouldn't be the accepted answer. It doesn't answer the original question (that is found via Google)

Some of the other posters have added binary mode (i.e. bin ) to the FTP commands. Since yours is the accepted answer, I recommend you adding this to your answer too.

Can also type hash to show progress of the ftp transmission in a way. Also, you will usually see

@SayanDasgupta as the answer says "The binary command will set it to binary mode which helps if you are transferring something other than a text file." If you are transferring images, pdfs, documents, zips, archives you want binary mode. If you know you are only transferring literal text files (e.g. source code, csvs) then skipping the binary might save you a tiny amount of time.

You can use a heredoc to do this, e.g.

so the ftp process is fed on standard input with everything up to End-Of-Session . It is a useful tip for spawning any process, not just ftp ! Note that this saves spawning a separate process (echo, cat, etc.). It is not a major resource saving, but it is worth bearing in mind.

This is totally confusing. How do I get out of this End-Of-Session thing? Why not input directly to the ftp prompt that comes without that?

@erikb85 - this for scripts, not (necessarily) for interactive use. The heredoc will automatically register an act upon your 'End-Of-Session' marker (you'd likely use EOF or similar)

The ftp command isn't designed for scripts, so controlling it is awkward, and getting its exit status is even more awkward.

Curl is made to be scriptable, and also has the merit that you can easily switch to other protocols later by just modifying the URL. If you put your FTP credentials in your .netrc, you can simply do:

# Download file curl --netrc --remote-name ftp://ftp.example.com/file.bin # Upload file curl --netrc --upload-file file.bin ftp://ftp.example.com/ 

If you must, you can specify username and password directly on the command line using --user username:password instead of --netrc .

Источник

Transfer Files From Computer to Cloud Linux Server

There are multiple methods you can use to transfer files between your machine and Linux server, some of which we’ll discuss in this article.

Using SCP (SSH)

SCP is a utility used to move files and directories securely via SSH. With the SCP command, you can transfer files from your computer to your Linux server and vice versa. As this utility uses SSH to move files, you’ll need the SSH credential of your server to transfer files.

SSH comes pre-installed on most Linux servers, but if not, you can install and enable it using the following steps.

Open the Ubuntu terminal and type.

Upload files via SCP

Scp command follows this pattern

To transfer a file from your computer to a linux server, write these commands

In the above command, first, you have to give the path of the file you want to copy from your computer to the Linux server, then the username and IP address of the Linux server, and the path where you want to copy the file on the Linux server fallowing this pattern (username@remote-server-IP: path/of/remote/file.ext).

After running this command, it will require the password of the Linux server user account

After entering the password, the file will be uploaded.

Download files via SCP

To download files from the Linux server to your computer, you need to provide SCP with the local path of the file or directory and the path on the Linux Server where you’d want your file to be uploaded.

After running this command, it will require the authentication password of the linux server. Once you have entered the password, then the file will be copied safely to your computer.

SCP Command-Line Options

You can use different flags(known as command-line options) in the SCP command.

-p flag is used to change the port. By default, ssh uses the 22 port, but with the -p flag, we can change port 22 to something else, like 2222.

-r flag is used to copy the folder and all of its content.

-i flag is used to authenticating the connection using a cryptographic key pair stored in a file instead of a username and password.

-c flag is used to compress the data that you want to transfer.

-q flag is used to suppress the non-error message and progress meter.

Transfer Files Using Netcat

Netcat is a Linux utility used for raw tcp/ip communication, transferring files, port scanning, and network troubleshooting, etc. It comes pre-installed in many Linux-based systems, and it is mainly used by Network Administrators.

If not already installed, you can install Netcat by typing the following command

To transfer files using Netcat, you have to type these commands. Turn the Netcat server on listening mode on any port, e.g.(port 4747), and type the path of the file you want to send.

On the receiving host, run the following command.

Note: The server sending file will use less than sign in the command ‘’ in the netcat command.

You can also transfer directories. Set the receiving host to listen on a port, e.g. (4747).

Send it to the receiving host listing on the port.

The directory will be transferred. To close the connection, press CTRL+C

Transfer Files Using FTP

FTP (file transfer protocol) is used to transfer files between computers or clients and servers. It is faster than HTTP and other protocols in terms of file transfer because it is specifically designed for this purpose. It allows you to transfer multiple files and directories, and if there is any interruption in the connection during the transfer, the file will not be lost. Instead, it will resume transferring where it got dropped.

You can install an FTP server like vsftpd using apt by running this command.

After the package has been installed, you have to start the service by typing.

Then you can connect to the FTP server by typing the command FTP and the IP address.

It will ask you the username and password of the FTP server. After you have entered the username and password, you will be connected to your FTP server.

You can list out all the contents of the server by executing this command.

Download via FTP

If you want to download any file from the FTP server, then you can get it by typing the command.

The file will be downloaded. You can also use different wildcards to download multiple files in a directory. For example ;

It will download all the files with the extension “.html” .

You can also set up a local directory for downloaded files from the FTP server by using the lcd command.

Upload files via FTP

To upload files on the FTP server, type the following command.

The file will be uploaded to the FTP server. To upload multiple files, type commands.

It will upload all the files with the extension “.html” .

Downloading files using Python

Python has a module called ‘http.server’, which is used to transfer files, but with it, you can only download files.

If you don’t have the python installed, then type the following command.

To turn on the python server, use the command.

Now the python server is listening on port 4747.

Go to your web browser and type the IP address and port no. on which the python server is listening.

A page will open containing all the files and directory on the python server. You can go into any directory and download the files.

You can go into any directory and download any file.

Conclusion

SCP, Netcat, FTP, and Python are commonly used methods to transfer files. All of the above methods of transferring files and directories are fast, reliable, and used in modern days. There are a lot of other techniques as well; you can adopt any method you prefer.

About the author

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14

Источник

How to upload local file to server through Linux terminal

I am trying to upload local files to server by using Putty or SSH but not getting upload there. Is there any direct method to upload file from local to server from Linux terminal without using FTP etc ?

want to transfer my local file to server by using Linux Terminal.Suppose we used to access server by putty or ssh but not able to transfer there file by on same terminal.

3 Answers 3

Sure. Use scp (secure copy) like this:

scp [source file] [username]@[destination server]:. 

Of course replace the bracketed [source file] , [username] and [destination server] to match your local settings. So if the file was cool_stuff.txt and your username on the remote sever is sanjeev and the destination sever is example.com , the command would be:

scp cool_stuff.txt sanjeev@example.com:. 

And the source could also be remote so you could do this to do the opposite of the above example:

scp sanjeev@example.com:cool_stuff.txt . 

That command would copy the remote file cool_stuff.txt to whatever local directory you are in. And if you are doing this with multiple files, just use a wildcard ( * ) like you would for a normal cp command.

Also, the . just indicates the immediate directory path; such as the one you are in right at the moment you run the command or the immediate path that the remote user on the destination server has. But you could also specify a path like /this/path/right/here in the local to remote example:

scp cool_stuff.txt sanjeev@example.com:/this/path/right/here 

Or the remote to local example right here:

scp sanjeev@example.com:cool_stuff.txt /this/path/right/here 

Now if the remote server does not allow SSH and only SFTP, then SFTP is the way to go. But scp is very useful when you want to just toss a file and not do the whole SFTP process manually from the command line.

Источник

Читайте также:  Установка kali linux виртуал бокс
Оцените статью
Adblock
detector