How to use FTP from the Linux Command Line

folder_downloadsYou can use the ftp command to talk to an FTP server from the Linux Command Line. Type ftp to see if the tool is installed. If you get a “command not found” message then go ahead and type yum install ftp to make it available on your system.

Using it is very straightforward – but I keep forgetting how because I only do it once in a blue moon. So here’s a handy cheat sheet:

Logging in to your FTP Server

Assuming our site is example.com, simply type this:

This will connect you, but the system wants to know the username and password at the prompt. Provide those and if your login was successful you’ll see something like this:

Note that you’re now at the FTP command line and no longer on the Linux command line (you can tell by the ftp> in front of the cursor). Therefore only FTP commands are now accepted, until you type “exit” or “bye” to go back to Linux.

To see a list of available commands type help and you’ll see a list much like this:

No need to panic: The good news is that we don’t really use a plethora of new commands, and some (like ls and mkdir) are working the same way, just the output may look a bit different.

Let’s go through a few common scenarios now: listing and creating directories, uploading, downloading, and deleting files. Classic CRUD – FTP Style.

If you ever need to come out of a running command, CMD-D (or CTRL-D) will do the trick.

Listing and Switching Directories

Your usual Linux favourites will work fine to list and switch directories:

ls (list directory, same as dir)
cd (change into directory, for example “cd mydir”)
cd .. (move one directory up in the tree)

Excellent: nothing new to learn here. Result!

Creating and Deleting Directories

Another nice thing is that mkdir is still working to create a directory. Here’s how we create a directory called test:

Likewise, rmdir does a good job at deleting (empty) directories:

To delete a directory that contains files you must first remove all files (see below under Deleting Files) and then use this command.

Downloading Files

To download a single file we can use the get command (or recv if you can remember it better). You must type out the entire file name for this to work, and you won’t get a progress report while your file downloads:

This will save testfile.tar in the Linux directory that you were before you initiated the FTP session.

To save files in a directory other than the current one, I’m afraid you’re going to have to log out, cd into the directory you want those files to go, then re-connect. I know, ultra lame – but if there is another way then it’s kept so secret that no Google search will ever unveil it.

Sadly wildcards are no working in this operation, so you’ll always have to type out the exact file name. Lucky for us you CAN use wildcards to download multiple files with mget, like this:

Now all files starting with “test” are downloaded and you’ll be prompted one by one. This will work for single files too and saves you having to type out cryptic long names. Human 1 – FTP 0. Ha!

Uploading Files

put and mput work just like get, but they upload local files to the current FTP directory. You can specify a local Linux path when doing this, but put and mput expect a local path to also exist on the FTP remote (and fail if they don’t). Read: messy. There probably is a way to deal with this, but life’s just too short.

Just like get, put also needs the whole file name and cannot deal with wildcards – but mput does:

Deleting Files

There’s also a delete and mdelete command which – you guessed it – removes unwanted files from the server. Same as before: no wildcards on delete, but they work fine on mdelete:

Alternatives

FTP transfers all files and passwords “in the clear” and does not work with encryption. Checkout the sftp command which will do all of this and more while using encryption on all transfers.

Note that there is a difference between SFTP and FTPS: the latter (FTPS) is the same as FTP but with encryption added to it. SFTP isn’t really FTP at all, it’s an SSH connection that works much like rsync and scp, and uses similar syntax.

Further Reading

  • http://www.computerhope.com/issues/ch001246.htm
  • http://www.cs.colostate.edu/helpdocs/ftp.html
  • http://www.manpagez.com/man/1/ftp/
  • http://www.manpagez.com/man/1/sftp/

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

6 thoughts on “How to use FTP from the Linux Command Line

  1. Hi,
    I installed RHEL 6.1 on virtual box and now when i am trying to run ftp command it says command not found
    tried to install it by using yum and get command,it says no such directory/file
    please advise

    Thank you.

    1. Hi Bhavani,

      try using ‘yum install ftp’ – if it’s already installed you’ll see a message like ‘nothing to do’. If the ftp command isn’t called, perhaps your path variable doesn’t include /usr/bin (that’s where the command lives). In which case, try calling it with its full path:

  2. Hi Jay, you can change the local directory without logging out and back in to the ftp session. Use the command “lcd” (for local current directory). It is a bit inconsistent with the other local commands which are accessed by prefacing with “!” such as “!ls” to list the current local directory’s contents, or “!pwd” to print the name of the current working directory. The exclamation mark bypasses the ftp program.

    A short description of any of the commands may be displayed from inside the ftp program by typing help followed by the command you want to know about, for instance “help newer”. I’ve been looking for a full list of them so I don’t have to go through typing all of them in turn. (That’s how I found your page.) I finally found one at:
    http://www.smartfile.com/blog/the-ultimate-ftp-commands-list/

    Incidentally, I hate having to worry about security, but I believe all the commands that work with ftp also work exactly the same with sftp, which has the advantage of using a secure connection so that username and password and other sensitive material can’t be (easily) snooped.

    By the way, I spent a few tries attempting to post a reply here until I realised I had to click on the link to “change” my login, which is counter-intuitive. WordPress already knows who I am, yet I need to nevertheless enter my details? This probably explains why you have few replies to an interesting article.

    1. Hi Miriam, thank you so much for sharing those tips, I really appreciate it! I don’t often use FTP (or SFTP) from the command line, so I always forget how to do it when I have to 😉

      Not sure what’s going on with the WordPress login, but I’m glad you persevered and could post your comment.

      All the best,

      JAY

Add your voice!