File transfer using FTP from command line

For the people who work on multiple machines (Windows, Unix or a combination), file transfer is a frequent need. There are many famous and widely used tools like filezilla, WinSCP but somehow I prefer using command line OS utility for its simplicity and robustness.

Here is how I use it:

Eg: Windows (Source), Target (Unix):

  • Open Windows command line. Change to the required directory.

cd c:\test

  • Login:ftp <IpAddress>

Hostname can also be given and IPAddress would get resolved.

  • Username-Password: You should see a prompt for Username and after entering it, another prompt comes up for Password. These are the same credentials of Unix user.

In case of incorrect credentials you would get the following error:

530 Login incorrect.

Login failed.

In such cases by being in ftp> prompt you can retry the username/password by giving the command ‘user’

ftp> user

The login would take you to the default location of the User. You can check the same using ‘pwd’ (present working directory) command.

ftp> pwd

You can change to the target directory, if required.

ftp> cd /apps/local/bin/test

  • Transfer mode:

The immediate thing that I do is to set the transfer mode. Default mode I’ve observed is ‘ASCII’.

Binary mode is the best bet as it transfers the file as-is. It’s a must for image, video, audio etc binary files. Ascii mode can be used for text files (.txt, .html..etc anything that can be viewed with normal text editors). Note that Ascii mode can modify the content for compatibility, especially for newlines : Windows uses \r\n ( Carriage Return, Line Feed) where as Unix uses \n (line feed only).

I prefer using Binary mode even for text files. If the transferred file contains ^M characters, they can be corrected using dos2unix utility.

ftp> bin

200 Type set to I.

  • Hash mark for Transfer progress: For transferring large files, understanding the progress is key, so Hash mark can be enabled. It would shows # symbols as the file transfer progresses.

ftp> hash

Hash mark printing On ftp: (2048 bytes/hash mark) .

  • Changing directory on Source machine:

Sometimes after we login, we might realize that the required source files were in a different directory. In such cases some users exit ftp prompt, change the directory and login once again to ftp. This hassle can be avoided by ‘lcd’ (Local change directory) command.

ftp> lcd c:\test\1

  • Transfer of files:

The actual transfer is triggered with commands: get, put, mget, mput

ftp>get 1.txt

Gets the file from Unix box to Windows machine.

ftp>put 2.doc

Puts the file from Windows to Unix.

mget and mput are even more handy and are used for multiple file transfers.

If there are two files export1.txt, export2.txt in source directory:

ftp> put export*

The above command picks the first file and directly transfers it.

ftp> mput export*

The above command prompts and based on confirmation transfers both the files.

The prompt is handy but sometimes could be annoying while we try to transfer numerous files. To avoid the prompt we need to login with ‘-i’ option.

c:\test\ftp -i <IPAddress>

  • Exit:

bye or quit commands can be used to exit from ftp console.

ftp> bye

or

ftp>quit

Note: The pre-requisite is ‘ftpserver’ should be running on both source and target machines and usually its one of the default software setup on machines. You can give ‘ftp’ command and quickly determine if its available on your machines.

Summary of commands for a sample transfer:

cd c:\test
c:\test> ftp <IpAddress>
ftp>cd /apps/local/bin/test
ftp>bin
200 Type set to I.
ftp>hash
Hash mark printing On ftp: (2048 bytes/hash mark)
ftp>lcd c:\test\1
ftp>get 1.txt
ftp>put 2.doc
ftp> bye

Leave a Comment