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 (12.34.56.78). 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.
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:
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:
mget test*
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:
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)
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:
mdelete test* mdelete testfile.tar? y 250 DELE command successful
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.
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.
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:
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.
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
No worries. 🙂
Looking through the list at the link I posted I realised it is far from complete and it includes some commands that are not part of a standard commandline FTP program. So I edited it pretty extensively and uploaded the result to my website at:
http://miriam-english.org/files/FTP_command_list.html
I hope it is of some use to you.