In this tutorial we’ll be getting familiar with basic use of the Raspberry Pi terminal. We’ll cover navigating the file-system, making directories, writing and editing text files, and just touch on the use of wildcards.

Transcript

Gidday, my name is Michael from Core Electronics and today we’re going to be looking at how to use the Terminal with our Raspberry Pi. On the bench here I’ve got my Raspberry Pi 3 Model B with a mouse plugged in, a wireless keyboard, this is the video connection for our monitor and that’s our power. Over on the desktop this is what you will see when you boot up a fresh install of Rasbian Pixel. So this should be pretty similar to what you see when you first boot up and we can find the terminal either by accessing this icon here or we can access it by the applications menu - accessories and then terminal. This is what you’ll see when you first open the terminal, we have Pi, that’s the current user, Raspberry Pi is the machine we’re working on, the tilde (~) is telling us the directory that we’re in and the dollar sign ($) is the shells way of prompting us for our own input. So what we’re looking at is the terminal window, and inside that we’re looking at the shell.  The shell, called Bash, is what takes the users inputs and processes them. So we’re just going to start by printing the working directory with the command pwd. We can see the current working directory is /home/pi - that first slash is the very top level directory, it’s everything that exists on our Raspberry Pi sits underneath that directory, it’s called root. 

From there we have the home directory and that’s where all the user directories live, if we had other users they would be in there as well. Because we only have one user in there at the moment, pi, we are currently in the pi directory. This tilde ~  here is actually shorthand for home/pi. Lets take a look at what’s inside the pi directory, we can do this with the ls command, ls,  and you can see that there are a few other directories like desktop, downloads, pictures etc. When you get more and more files appearing with ls you’ll see that they will start to be color coded depending on what file they are.

For this tutorial lets make our own directory, just to have a play around and we do this with the  make directory command which is mkdir and i’m going to call it sandbox. You can see that we didn’t get any return, we didn’t get any feedback. The shell assumes that we know exactly what we’re doing so it just does what we tell it without asking if we’re sure or with out giving very much feedback at all. For something as simple as make directory you won’t see any feedback.  Then we can perform ls again and we can see that, there it is, the directory sandbox has appeared. Before jumping into sandbox we’re just going to make 3 more directories but we’re going to put them inside sandbox.  So we can just come down a bit and we can make a directory, we’ll call it test_1, excuse me, we’re going to clear that, we’re going to make director and we’re going to put it in sandbox and we’re going to call it test_1. We’re going to do the same thing again, make directory, but we’re going to specify it from root. So we can make a directory in root, in home/pi/sandbox/test_2. Now these 2 commands are going to make directories in sandbox. The first one was referenced to where we were already, to our working directory remember from the print working directory command. So its saying wherever I am right now there is already a folder or directory called sandbox and inside that I want to make a directory called test_1. The second command uses what is called an absolute reference. It’s saying from root I’m specifying the entire path all the way down to where I want to create test_2 directory.  This absolute way of specifying paths, this method means that no matter where you are in file system if you actually keep that command it will put test_2 in exactly that directory.  Again, lets make another directory, this time we’re going to specify it from tilde ~  so we’re going to say ~/sandbox/test_3 this is like another absolute reference except we’re referenced from the home directory pi. Let’s just do a quick ls and jump into our sandbox. Ignore that test_1 that was my first mistake. So we’re going to use the change directory command which is cd and we’re going to change into the sandbox directory and you can see our  prompt has updated to tell us of our new location so if that ~ was the pi directory we are now in pi/sandbox and we can ls from there and see that we have test 1, 2 and 3.

So now I suppose we better figure out how to make a text file, text files are going to be your primary way of writing scripts and automating tasks for your Raspberry Pi to do. Its definitely going to be a cornerstone of interacting with your Pi. For that we are going to use a program called nano, just by invoking the command nano and typing enter you can see that we’ve left the shell and we’re now in a text editing environment. Lets write something simple like hello world and we’re going to save this file. Down the bottom this is our menu essentially and this carrot symbol stands for the control key so we can see that the Ctrl+X corresponds to exiting so when we go to Ctrl+X we get a prompt. This is just saying do you want to save your file before quitting so we’re going to say yes! of course with a Y, we’re going to save and we’re going to save it in, let’s put it in the test_1 directory.  Because remember we called nano from sandbox so we can use a relative directory reference which is test_1 and let’s just call it textile and we’ve dropped back into the shell. So lets perform ls, nothing has changed in sandbox. If we change directories cd into test_1 and perform ls again we can see there is our text file. We can involve nano to have a look at it, so we can say nano textile and there is our file again with the same text we entered before. We can Ctrl+X to exit and because we made no changes to the file it just exits cleanly.  That didn’t last very long so how about we delete the text file with the remove command. So that is going to be rm textfile and again we get no feedback but if we type ls we can see nothing comes up, textfile has indeed been deleted.  This is just getting a little bit messy so I’m just going to use the clear command and thats going to clear our console. You can do that whenever things get a little bit overwhelming or a little bit too hard to read.  We’re still in sandbox test_1 and we can jump back up to sandbox, we can do that several ways. We could change directory using absolute references - we could say home/pi and leave it at that. Or we could use another relative reference which is the double dot (..) so what that is going to do. what that means is it’s just going to move up the directory file path 1 level. Because we’re in test_1 and sandbox is the next level up it’s going to move us into sandbox and if we hit enter we’re going to see the prompt that, yes, we are indeed in sandbox with our 3 test folders. Now let’s remove these directories, we can try rm test_1 and we get a return from rm saying we can’t remove test_1 - it’s a directory.  To remove directories and everything inside them we need to invoke rm with the -r argument, that r stands for recursive or a recursive remove. So we can rm r test_1 and ls and we can see that test_1 has been removed as well. To delete the other 2 test directories we’re going to do something  a little bit more clever. If you had hundreds of these and you wanted to remove all of them you wouldn’t want to do it manually the whole way along so we’re going to use something called wildcards. We’re going to use the same rm r command and because these two directories have the word ‘test’ in common we can type in test and follow it up with an * asterix which is a wildcard. This asterix is going to be a stand in for any number of characters. This will delete all files and directories that start with the word test and have anything coming after it.  So we can execute that, ls and we can see that both of those directories have been cleared. We can clear our console and we’ve cleaned up after ourselves and there is nothing in sandbox.

I encourage you to have a bit of a play in your sandbox directory, this could be a good environment to make some files, to make some text files and see how you can move them around. Perhaps explore copying, deleting, using those wildcards and generally just make sure you do everything within sandbox so you don’t remove or delete anything very important :-) 

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.