-2

Can any one help me writing a shell script to Download files from Linux/UNIX system?

Regards

Muhammad Abid
  • 801
  • 4
  • 12
  • There are many ways to do this. Can you tell us in what scenario this script will be used? – Marijn van Vliet Feb 02 '16 at 07:08
  • Also, on what operating system will the script run. Are you downloading from a UNIX system to a UNIX system, or to a Windows system? – Marijn van Vliet Feb 02 '16 at 07:10
  • I have a remote Linux machine there is a directory with some text files.... I need those files on my local machine daily at certain time to read and make notes out of those files.... I have user name and password and IP address of remote system. I can remote login but that is headache. – Muhammad Abid Feb 02 '16 at 07:11
  • ok thanks, that's all I need. – Marijn van Vliet Feb 02 '16 at 07:13
  • Look for `ftp` option if you are able to. See this [link](http://www.techradar.com/how-to/software/operating-systems/how-to-use-ftp-through-the-command-line-in-mac-os-x-1305664) – Utsav Feb 02 '16 at 07:20

1 Answers1

0

On UNIX systems, such as Linux and OSX, you have access to a utility called rsync. It is installed by default and is the tool to use to download files from another UNIX system.

It is a drop-in replacement for the cp (copy) command, but it is much more powerful.

To copy a directory from a remote system to yours, using SSH, you would do this:

rsync username@hostname:path/to/dir .

(notice the dot at the end, this means 'place everything here please', you can also give the name of the local dir where the files should be placed.)

To download only some specific files, use this:

rsync 'username@hostname:path/to/dir/*.txt' .

(notice the quotes: if you omit them, your shell will try to expand the *.txt part locally, will fail and give you an error.)

Useful flags:

--progress: show a progress bar
--append: if a file has only partially downloaded, resume it where it left off

I find the rsync utility so useful, I've created an alias for it in my shell and use it as a 'super-copy':

alias cpa 'rsync -vae ssh --progress --append'

With that alias, copying files between machines is just as easy as copying files locally:

cpa user@host:file .

Making it even better

Since rsync is using SSH, it helps to setup a private/public key pair, so you don't have to type in your password every time: How do I setup Public-Key Authentication?

Futhermore, you can write down your username in your .ssh/config file and give the remote host a short name: read about it here.

For example, I have something like this:

Host panda
    Hostname panda.server.long.hostname.com
    User rodin

With this setup, my command to download files from the panda server is just:

cpa panda:path/to/my/files .

And there was much rejoicing.

Community
  • 1
  • 1
Marijn van Vliet
  • 5,239
  • 2
  • 33
  • 45