Monday 24 March 2014

Remotely modify a text file on your Raspberry Pi

My last blog post explained how to transfer files to and from your Raspberry Pi using FileZilla. It made use of SFTP (SSH File Transfer Protocol).

If I am programming and would like to remotely modify a Python file which is on my Raspberry Pi, I can do this through the command line. I would SSH into the Raspberry Pi and probably using nano (I do keep meaning to learn vim!) I can modify the file. It works really well.

However sometimes I find it a little clunky. I think it would be nice if I could use one of my favourite text editors on my computer to do this. As it happens SFTP is integrated into various other programs to make it easier to remotely modify a file already on your Raspberry Pi.

Now I know text editors, and which is the best one to use, has been debated to death for years. vi vs. emacs being the longest running battle probably ever. This battle even has its own wiki page http://en.wikipedia.org/wiki/Editor_war. It can be a very emotive subject for many people.

I happen to like Notepad++ on Windows and TextWrangler on the mac. Both these can remotely log into your Raspberry Pi to modify text files.

As with FileZilla in my previous blog post this is very easy to set up.

First TextWrangler.

Click on File and then Open from FTP/SFTP Server... in the drop down menu.



A new window should open, click on Connect...

This will open up yet another window as below.




Fill in the following details:
  • The IP Address of your Raspberry Pi. 
  • Check the SFTP Box.
  • Type in your Raspberry Pi User Name. 
  • Type in your Raspberry Pi Password. 
If you are unsure of your IP address of your Raspberry Pi this blog post will help you out. 

Your screen should look like this (but with your Raspberry Pi IP address.) 



You can now click on Connect.



You can now navigate through the file structure to the file you want to open. Once you have found it you can click on Open. 

The file will now be open in TextWrangler, and you can modify it as normal. 

It is a very similar process using Notepad++.

You should click on Plugins, and then on NppFTP, and check the Show NppFTP Window. This will open up a window on the right hand side of the text editor.




Now you should click on the Settings icon which is at the Right hand side of this additional window. It looks like a little cog wheel. When an option of General Settings or Profile Settings appears, you should select Profile Settings.



In this new window you should do the following.

  • Click Add New. 
  • When asked to enter a profile name type Raspberry Pi and click OK.
  • Type your Raspberry Pi's IP address.
  • Change connection type to SFTP.
  • Type in your Raspberry Pi Username.
  • Type in your Raspberry Pi Password.

Once you have completed your settings as below, with your own details, you can click Close.


Now to connect or disconnect to your Raspberry Pi click the little blue Dis(Connect) icon in the NppFTP window.



And then select the Raspberry Pi option from the drop down menu.

You will now be connected to your Raspberry Pi and can open any files you want to by selecting them from the folder structure in the window below.



If you prefer a different text editor to the ones I have mentioned above, why not see if it has the functionality to connect via FTP?

While I am make no claim that this is the best way to do this, or the only way, it is something I have found useful, and I am sure many of you will too!

Tuesday 11 March 2014

Remotely copy files to and from your Raspberry Pi.

Most of the time I remotely log into my Raspberry Pi. Depending on what I am doing I will either SSH into it or VNC into it, which allows me to do almost whatever I would like to.

I say almost as there is a caveat.

What happens when I want to transfer a file to or from the Raspberry Pi?

I have to find a memory stick, plug it into my computer, copy the files onto it, unplug it, plug it into my Raspberry Pi, go back to my computer, copy the files over...

Then if I happen to modify a file and want to transfer it back I go through the whole process again.

What an absolute pain!

There must be a better way... Well you will be pleased to know there is!

The secret is using SSH File Transfer Protocol or SFTP. (SSH stands for Secure Shell.)

Once you know how to use this, and it really is simple, it opens up a whole world of really cool options.

Lets start simple, and work out how to transfer files to and from your Raspberry Pi.

There are a number of programs you can download to help you out with this, but if you are new to this, I recommend FileZilla.



You will want the client and not the server version. You can download it from this link.


There is a version for Windows, Linux and Mac OSX.

Once installed, load the program. You should see a screen like this.



Now click on File and then Site Manager.



Now you should click on New Site and make the following changes:
  • In the entry box next to Host type in the IP address of your Raspberry Pi. If you are unsure of this, you can follow an earlier blog post of mine which helps you to determine this. 
  • Protocol should be changed to SFTP - SSH File Transfer Prototcol.
  • Logon Type should be Normal
  • Type in your User name in the box below this
  • Your login Password goes into the box below. 
  • Using the Rename button I have renamed my connection to Raspberry Pi. You dont have to do this, but it helps you identify your connection.
Your screen should look something like this.



You can now click on connect.

You may get a message warning you that the server's host key is unknown. You can just click OK if this box appears.

Your FileZilla window will now look as follows.


On the left hand side you can see under the Local Site there is a window which shows you the folder structure of your system. Underneath that there is a window which shows information of the files and folders of any folder you chose in the folder structure.

This is repeated on the right hand side but for the file / folder structure on the Raspberry Pi.

To transfer files to you Raspberry Pi

  • Navigate to the folder you would like to place the files into using the right hand windows. 
  • Navigate to the files or folders you would like to copy over in the left hand windows. 
  • Right click on the files or folders and chose "Upload"
The file will start to copy over to the selected folder. 

A similar process is used to transfer files from your Raspberry Pi.

  • Navigate to the folder you would like to place the files into using the left hand windows. 
  • Navigate to the files or folders you would like to copy over in the right hand windows. 
  • Right click on the files or folders and chose "Download"

This will copy files from your Raspberry Pi.

In the future your information should be saved into FileZilla, so you can just click on File then Site Manager, select the Raspberry Pi connection you have set up and then click connect. Simple!

I am sure a lot of you will find the use of SFTP very simple but effective, and you will no longer be searching around for a memory stick to copy files to your Raspberry Pi.