Overview: SFTP Command Line
Although GUI SFTP file transfer clients are more intuitive to use, some people still prefer to use SFTP through the command line. That's understandable. If you're already familiar with all the commands you need, a command line interface can be so much faster and powerful than any GUI-based counterpart. In this post, we're going to introduce you to the SFTP command line and show you how it can be quite a handy tool as long as you're willing to roll up your sleeves and get dirty.
Advantages of using the SFTP command line
I'm sure some of you are skeptical about SFTP command line having advantages over the GUI, so it might be best to deal with your doubts before we proceed. Here are some of them:
1. Supports wildcards
Wildcards make it possible for you to create filters in order to display or select only the files you need. For example, let's say the remote directory you're currently on contains hundreds of files but you only want to see those that contain the word "text" in their filename. I don't know of any GUI SFTP client that will allow you to do that. But in the command line, you can simply type:
Here's a sample output where we've added the -l option to display additional information
You can even use wildcards to download those same files by typing:
Here's a sample of that command in action:
Wildcards are extremely powerful. With them, you can select files having the same extension, beginning with the same letter, ending with the same 3 characters, and so on. In fact, just the ability to use wildcards can already be compelling enough to use the command line.
2. Can run SFTP commands automatically
When you use SFTP on the terminal, it's possible to shift into what is known as "batch mode". In this mode, you can call a batchfile which will then enable your system to run a sequence of SFTP commands. The batchfile would typically be written beforehand. Once it's called, there won't be a need for human input. In other words, with SFTP on batch mode, you'll be able to carry out automated file transfers.
To call a batchfile, you would run sftp plus the -b option like this:
sftp -b batchfile username@host
We're planning on putting together a separate post on the SFTP batch mode, so stay tuned for that.
3. Readily accessible
Many operating systems, like UNIX, OS X, and virtually all distributions of Linux, already have a terminal-based client for SFTP, built-in. Thus, you won't have to install anything on the client side in order to use it. You load up the terminal and type "sftp". If it shows you anything like this, then you're all set.
Well, you're probably all eager to enter your first SFTP command so let's head out to the terminal and start learning some command line SFTP!
Note:Of course, you'll need to connect to an SFTP server. So if you don't have one running yet, follow the instructions on the articleSetting Up An SFTP Serverand get back here when you're done.
My, that was quick. Ready already? Let's begin.
Logging in to SFTP
Let's start off by showing you how to connect to an SFTP server. You'll need the username of an account that already exists on your SFTP server and the host/IP address of the server itself. Now, you put that all together like this:
Here's an example.
Note that, if it's the first time you've ever connected to that server from your client machine, you'll get a warning like the one shown above. It simply indicates that the client doesn't recognize the host yet. Once you've typed in 'yes' and clicked Enter, your client machine will register that host's identity and all succeeding login sessions will be free from this notification.
After typing 'yes', you'll then be asked to enter that username's corresponding password. Enter that and then again hit the Enter/Return button.
Assuming you've entered the right password, you'll be authenticated and brought into the SFTP environment. You'll know you've successfully logged in if you see thesftp>prompt.
As mentioned earlier, all succeeding logins will be devoid of the warning shown earlier. So, the next time we logged in to our SFTP server, our login looked like this.
While playing around with your SFTP client, server, and keys, it's possible you could encounter a warning that the remote host identification has changed
Don't panic. It's highly unlikely someone's actually trying to hack your system at this point. It's more probable that you just changed your host key and so your client machine isn't able to recognize your host anymore. To remedy this, just go into the file /root/.ssh/known_hosts and delete the entire line that contains the key. Chances are, you'll only find a single line in there (it may wrap around if it can't fit the length of your terminal screen), so this should be fairly easy.
After deleting, just login back again and you should be prompted with the same warning normally shown during first time logins.
Displaying directory contents in SFTP
After logging in, you might want to see the contents of the directory you're on in your SFTP server. To do that, just issue the 'ls' command. 'ls' will list all the files found in that remote directory.
If you want to display more information about each file, add the-loption. So, the command will now be:
Normally, if your intention is to upload/download files to/from a SFTP server, it wouldn't be enough to know the contents of the current remote directory. You'll also want to know the contents of your local directory. To display the files in your local directory, just add l (for local) before ls:
Navigating remote directories in SFTP
Now that you know how to display the files in both your local and remote directories, the next thing you might want to do is navigate through whatever directories you have there. First, you must be able to identify which of those directory contents are actually subdirectories.
If you run the 'ls-l' command, you can find directories by looking at the first character. If it's a 'd', then that item should be a directory. So in our example below, the item named 'pdfs' is actually a directory.
To move into that subdirectory, run the command:
To verify whether you're in fact inside the pdfs subdirectory, just run the command:
'pwd' will display your current location. Note that this is a relative path. The '/' refers to your remote home or working directory and is notthe root directory. Thus, in our example, pdfs is right under our working directory.
If you want to go back up one level (i.e., to your current directory's parent directory), run the command:
Here, we've issued the pwdcommand again to show the current directory. Sure enough, we've moved up one level higher.
Navigating local directories in SFTP
If you want to move around your local directory as well, just add 'l' to any of the commands we tackled earlier. The local analogues of practically all remote-based commands are the same commands with an 'l' in front. So cd becomes lcd and ls becomes lls.
Again, to display your current location at your client machine, just issue the command:
Here's how you move up to your current directory's parent directory in your local machine.
Downloading files in SFTP
Time to up the ante a bit. Let's now do some SFTP file transfers. To download a file in SFTP, just issue the getcommand:
Or to download and then save the file in a different filename:
get oldfilename newfilename
Here are the files we just downloaded. The first one is the downloaded/renamed file, while the second one is the one we simply downloaded
Just to refresh, here are those wildcards again in a get command. This time, we're downloading multiple files.
You can even download to a specific directory. Here, we're using a relative path.
Uploading files in SFTP
To upload files, just use the same syntax as get but replace get with put. Here's an example where we upload a single file using:
And here's how we upload a file and save it in a new filename.
put oldfilename newfilename
And here's how we upload multiple files using a wildcard. This time, we're uploading all files starting with the letter 'd'.
Lastly, here's how we upload files to a specific directory:
Other SFTP commands you can use on the terminal
Here are a few more SFTP commands you should find useful:
To create a new remote directory:
To create a new local directory:
To delete a directory:
To delete a file:
And to get help:
We can probably stop here. That wasn't so bad was it?
Now that you are familiar using SFTP on command line, are you still more interested in using SFTP via a GUI-based client? If you are, then perhaps you'd like to read the article How To Use An SFTP Client, which focuses on the kind of client you're looking for.
That's it! To be up to date on tips like this. Follow us on Twitter! Follow @jscape
JSCAPE MFT Server supports SFTP, FTPS, FTP, and a host of other file transfer services. Download a free, fully-functional evaluation edition now.