Showing posts with label NcFTP. Show all posts
Showing posts with label NcFTP. Show all posts

2012/09/14

Linux: Using FTP in bash

Default FTP client in Linux is simply called "ftp", it covers mostly all the basic needs and its easy to use.

Using FTP in command line

Lets pretend that i have file called data.dat that i need to upload to ftpserver.net. First we start FTP by simply typing in "ftp".

 m@srv:~$ ftp  
 ftp> o ftpserver.net  
 Connected to ftpserver.net.  
 220 ProFTPD 1.3.4b Server ready.  
 Name (ftpserver.net:m): myusername  
 331 Password required for myusername  
 Password:  
 230 User myusername logged in  
 Remote system type is UNIX.  
 Using binary mode to transfer files.  
 ftp>  

So first of all we connect with command "o", followed by the server address, if connection is successful   server will prompt you for username and password.

After server lets you in, you can list directories by doing "dir" or "ls"

 ftp> ls  
 200 PORT command successful  
 150 Opening ASCII mode data connection for file list  
 drwx--x---  9 ftp   ftp     4096 Sep 14 11:45 .  
 drwx--x---  9 ftp   ftp     4096 Sep 14 11:45 ..  
 drwx--x--x  4 ftp   ftp     4096 Sep 3 20:00 .appdata  
 -rw-r--r--  1 ftp   ftp      220 May 12 2008 .bash_logout  
 -rw-r--r--  1 ftp   ftp     3116 May 12 2008 .bashrc  
 -rw-------  1 ftp   ftp      89 Sep 13 22:12 .clipboard.txt  
 -rw-r--r--  1 ftp   ftp      675 May 12 2008 .profile  
 -rw-r-----  1 ftp   ftp      34 Jun 5 2009 .shadow  
 drwxrwx--x  2 ftp   ftp     4096 Sep 14 07:21 .spamassassin  
 drwx------  2 ftp   ftp     4096 Sep 3 20:01 application_backups  
 drwxr-xr-x  2 ftp   ftp     4096 Apr 21 2011 backups  
 drwx--x--x  3 ftp   ftp     4096 Jun 5 2009 domains  
 drwxrwx---  3 ftp   ftp     4096 Jun 5 2009 imap  
 drwxr-xr-x  2 ftp   ftp     4096 Sep 14 11:45 upload  
 226 Transfer complete  
 ftp>  

Now we can proceed and upload file, there is a folder called upload so I'm going to use that one. You can create directories with "mkdir".

 ftp> cd upload  
 250 CWD command successful  
 ftp> put /home/m/data.dat data.dat  
 local: /home/m/data.dat remote: data.dat  
 200 PORT command successful  
 150 Opening BINARY mode data connection for data.dat  
 226 Transfer complete  
 ftp> ls  
 200 PORT command successful  
 150 Opening ASCII mode data connection for file list  
 drwxr-xr-x  2 ftp   ftp     4096 Sep 14 11:50 .  
 drwx--x---  9 ftp   ftp     4096 Sep 14 11:45 ..  
 -rw-r--r--  1 ftp   ftp       0 Sep 14 11:50 data.dat  
 226 Transfer complete  
 ftp>  

With "put" command we first specify local location of the file,  following that only the file name, since we are already in a correct folder. We could also use absolute paths here, like this: "/upload/data.dat". We verified that our file is indeed in upload folder with "ls".

Note: You can delete files with command "del" and remove directories with "rm".

Now we can exit

 ftp> exit  
 221 Goodbye.  

Polite FTP server will tell you Goodbye!

Upload file using NcFTP

I personally wanted to have a ftp program that can upload a file in a single command, i couldn't do this with ftp so i needed to look for alternatives.

Client called NcFTP can do this. This doesn't come as a default so you need to install it.

 m@srv:~$ sudo apt-get install ncftp  

With ncftp, you get a program called ncftpput. If i would want to upload same file as above with single line command it would look something like this:

 m@srv:~$ ncftpput -u myusername -p mypassword ftpserver.net /upload /home/m/data.dat  
 /home/m/data.dat:                 183.41 kB 208.00 kB/s  
 m@srv:~$  

Here you first specify remote folder and after that local file location, exact opposite to ftp's put.

Very handy for scripting!