You 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:
ftp example.com Connected to example (18.104.22.168). 220 FTP-Example Name:
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:
230 User tester logged in Remote system type is UNIX. Using binary mode to transfer files. ftp>
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:
Commands may be abbreviated. Commands are: ! debug mdir sendport site $ dir mget put size account disconnect mkdir pwd status append exit mls quit struct ascii form mode quote system bell get modtime recv sunique binary glob mput reget tenex bye hash newer rstatus tick case help nmap rhelp trace cd idle nlist rename type cdup image ntrans reset user chmod lcd open restart umask close ls prompt rmdir verbose cr macdef passive runique ? delete mdelete proxy send
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:
mkdir test 257 "/test" - Directory successfully created
Likewise, rmdir does a good job at deleting (empty) directories:
rmdir test 250 RMD command successful
To delete a directory that contains files you must first remove all files (see below under Deleting Files) and then use this command.
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:
get testfile.tar local: testfile.tar remote: testfile.tar 227 Entering Passive Mode (81,169,163,229,179,131) 150 Opening BINARY mode data connection for testfile.tar (86365356 bytes) 226 Transfer complete 86365356 bytes received in 11 secs (7865.17 Kbytes/sec)
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!
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:
mput test* mput testfile.tar? y 227 Entering Passive Mode (81,169,163,229,218,225) 150 Opening BINARY mode data connection for testfile.tar 226 Transfer complete 236716 bytes sent in 0.0141 secs (16825.36 Kbytes/sec)
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:
mdelete test* mdelete testfile.tar? y 250 DELE command successful
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.