DEV Community

林子篆
林子篆

Posted on • Originally published at dannypsnl.github.io on

5 tools for file transfer

We usually have to transfer files between two computers, such as config, log, image. There are a lot of tools that could do it, but if we only know one approach, we would have trouble quickly, so I’m going to show you five tools relate to the topic.

1. scp(secure copy)

scp is a command line utility that allows you to copy files and directories between two locations securely. It put at first order is because it’s the best choice if we could. With scp, we could:

  • copy from your local to a remote
  • copy from a remote to your local system
  • copy between two remotes from your local

When scp transferring data, both password and files are encrypted so anyone could see the packet can’t get any sensitive.

command line syntax

The format of the command is:scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

For example:

$ scp log.txt danny@54.133.2.8:~/
Enter fullscreen mode Exit fullscreen mode

Means copy the ./log.txt to the home directory of 54.133.2.8 this computer.

scp just like cp, as you thought, copy remote to local was:

$ scp danny@54.133.2.8:~/log.txt ./
Enter fullscreen mode Exit fullscreen mode

If you want to copy between two remote:

$ scp danny@54.133.2.8:~/log.txt danny@54.133.2.9:~/
Enter fullscreen mode Exit fullscreen mode

Usually, you need a key to use scp, to add it into command, use the option -i to do that:

$ scp -i /path/to/key $src $dst
Enter fullscreen mode Exit fullscreen mode

There are a lot of useful options, just quickly pick some at here:

  • -r: recursively copy the directories
  • -P: specific port
  • -C: force compress data

To get more information, take a look at scp man page

2. nc(netcat)

Although scp is powerful, sometimes the environment couldn’t use it.

nc is a network testing tool actually, but if you use:

$ nc -l 8888 | tar zvf -
Enter fullscreen mode Exit fullscreen mode

Nice, you listen on port 8888 now, next thing is transfer data to it.

$ tar cvf - ./file | nc localhost 8888
Enter fullscreen mode Exit fullscreen mode

For a lazy guy:

# listen
$ nc -l 8888 > file
$ nc localhost 8888 < file
Enter fullscreen mode Exit fullscreen mode

nc would close after receiving some data, so if we don’t want that, try:

$ while true; do nc -l 8888 | tar zvf -; done
Enter fullscreen mode Exit fullscreen mode

3. python

If you have no choice or want to provide a long time server, and no like to remember a lot of pipeline command, try python:

$ python3 -m http.server
Enter fullscreen mode Exit fullscreen mode

It would start a file server at your command location.

4. tar

At nc section, could see we already use this tool, tar is helpful while the file is quite big, reduce the size to improve our life.

Example:

# compress
$ tar cvf file
# extract
$ tar zvf file
Enter fullscreen mode Exit fullscreen mode

To get more information: tar man page

5. ngrok

At the previous section, we are assuming the target have public ip could reach easily. But life is hard. Sometimes we didn’t have public ip; for this situation, ngrok could help.

Site: ngrok

This tool has to download by yourself.

Let’s show how to use it:

# expose localhost:8080 by HTTP
$ ngrok http 8080
# expose localhost:8080 by TCP
$ ngrok tcp 8080
Enter fullscreen mode Exit fullscreen mode

The TCP is beneficial for nc command. And HTTP is useful while you create a file server.

Conculsion

In this tutorial, you learn how to transfer files by different tools and solve the limited networking environment issue to transfer data between different computers. Thanks for reading.

Top comments (0)