Learning how to use Terminal and enter command line commands on a Raspberry Pi is an essential skill to master. With Linux commands you can take full control of Raspberry Pi OS. And Terminal is an essential tool for most maker projects.
The power of the Terminal is not only for the Raspberry Pi, but this is the perfect place to start learning to home such an important skill.
Underneath all the pretty pictures your Desktop offers you; there is a whole world of power. Applications only help you unlock a portion of the capabilities at a time. So imagine what you could do if you did not need an application to tell your system what you need it to do. This is where the power of Linux lies… By putting the power back in your hands.
Terminal often intimidates new users. However, once you get to know it, you gradually start liking it. Well, that happens with most Linux users. The terminal you see is just one of the various terminal applications available. After all terminal is just a GUI tool that gives you access to a shell where you can run the commands.
Different terminal applications (properly called terminal emulators) look different, have slightly different functions and features (like different keyboard shortcuts, color combination, fonts etc).
Open the terminal with keyboard shortcut
You can open the terminal in Linux by looking for it in the system menu. However, my favorite way is to use the Ctrl+Alt+T keyboard shortcut in Ubuntu.
Terminal is the graphical application that runs a shell by default. Shell is difficult to visualize separately from the terminal. The terminal is running a shell, usually Bash shell by default in Ubuntu. Like terminals, there are various shells as well. Bash is the most popular of them all and default shell on most Linux distributions.
There’s really nothing wrong with GUIs, and Raspbian comes with a rather fine one. But beneath the icons sits a whole other world: the command line. This is where your real computer is. With the command line, you’re not locked into doing just what desktop applications enable you to do. You can do just about anything to your computer, and you can do it much faster.
Think of it like driving a car. If you’ve only ever used a GUI then you’re driving an automatic. The command line is like switching to manual. It’s a lot trickier, but you get far more control and feel like a proper driver. The command line can be daunting for newcomers, but it really needn’t be. With just a few commands, you can master the command line.
Find your location in the command line
The first thing you need to learn is how to find out where you are. You are in your home folder by default. Enter the following command and press RETURN:
This command is short for ‘print working directory’ and it tells you where you are. The command line will return /home/pi if you are using a Raspberry Pi. The home folder is the same one that appears by default when you open the File Manager app. You view the files and directories inside the working directory using the list (ls) command:
Here you’ll see the same directories (or folders) that are in the File Manager app: Desktop, Downloads, Documents, and so on.
Relative paths are ‘relative’ to your working directory, which is /home/pi/ when you start. Entering ls alone shows the contents of the current directory. You can view the contents of a directory inside your working directory using ls and its name:
You can also view the contents of the directory above you using two dots (..):
This displays files relative to where you currently are in the file system. If you moved into the Downloads folder and entered ls Documents, it’d cause an error, because there is no Documents directory inside the Downloads folder.
An absolute path, on the other hand, always starts with a slash ‘/’, which is the root directory (the base of your hard drive).
…to view the root directory. Here you’ll see all the directories and files that make up Linux. You’ll see directories like bin (for binaries), boot (files used to start up the system), and home, which contains your user folder.
…and you’ll view the contents of your home folder, just as if you had entered ls from within it. You can use absolute paths no matter what your working directory might be, because they always start from the root.
Changing directories in Terminal
Up until now we’ve stayed in the home folder and looked around using ls. You move from one directory and another using the cd (change directory) command:
…and you’ll see a different working path:
/home/pi/Documents. To move back up a directory (known as the ‘parent’ directory), you use two dots.
Enter pwd again and you’re back in the home folder. Now try it using an absolute path. Enter:
…and you’ll be in the root directory. Enter ls to view the folders at the base of your hard drive. Enter:
…to move back to your home folder. There’s a shortcut for this:
The tilde (~) character is a shortcut for your home folder. You can use it at the start of an absolute directory too. For instance, entering:
…moves to your Downloads folder no matter where you are in the system.
Make a directory and file in Terminal
One of the first commands you need to learn is mkdir. This stands for ‘make directory’. Move to the home folder and create a new directory called test:
To create files, you use a rather odd command called touch. Officially, touch is used to update the modification time of files (reach out and ‘touch’ them). If you touch a file, it updates the time next to it to the current time.
Few people use touch for that. A happy by-product of the command is that if you touch a file that doesn’t exist, it creates an empty file. Enter:
You’ll create a blank file called test.txt. Enter ls -l and you’ll see the new file along with all its details. Notice that the file size is 0. This is because the file is completely empty.
vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX. Wikipedia
GNU nano editor
GNU nano is a text editor for Unix-like computing systems or operating environments using a command line interface. It emulates the Pico text editor, part of the Pine email client, and also provides additional functionality. Unlike Pico, nano is licensed under the GNU General Public License. Wikipedia
For me vi has a much steeper learning curve, though more powerful too. Nano is my favourite, so this is what I like to use.
We can edit the contents of the file using nano:
You can enter and edit text in nano, but the Save and Exit commands predate the traditional CTRL+S, CTRL+W standards. Enter a single line, ‘Hello World’, and press CTRL+O followed by ENTER to save the file. Now press CTRL+X to exit.
Enter ls -l again; you’ll notice that the file size has changed from 0 to 12. This is one for each letter (including space) and a newline marker at the end (you can see this character using od -c test.txt if you’re curious).
Delete a directory and file in Terminal on a Raspberry Pi
Let’s try deleting files. This command removes the file:
Now move up to its parent directory and use another command, rmdir, to remove the empty test directory.
Unfortunately, you’ll rarely use rmdir to remove directories, because they typically have files inside them. You can see how this fails using these commands:
It will say rmdir: failed to remove ‘test’: Directory not empty. The solution is to use rm with the -R option. This option stands for ‘recursive’ and means it goes inside each directory (and sub-directory) and removes every file and directory. Be careful when using rm -R, as it’s permanent and deletes everything inside. Enter:
rm -R test
The test directory and everything inside will disappear. Unlike the desktop environment, there is no Wastebasket in the command line. When you remove files, they’re instantly gone for good.
. and ..
You may often come across . and .. notation while using the Linux terminal.
Single dot (.) means the current directory.
Double dots (..) mean the parent directory (one directory above the current location).
You’ll often use the double dot (..) in relative path or for changing directory. Single dot (.) is also used in relative path but more importantly, you can use it in the commands for specifying the current locations.
Understand the command structure
A typical Linux command consists of a command name followed by options and arguments.
command [options] argument
Option, as the name suggests, are optional. When used, they might change the output based on their properties. For example, the cat command is used for viewing files. You can add the option -n and it will display line numbers as well. Options are not standardized. Usually, they are used as single letter with single dash (-). They may also have two dashes (–) and a word. Same options may have different meaning in a different command. If you use -n with head command, you specify the number of lines you want to see, not the lines with numbers.
Command Structure Example
Same option -n has different use in cat and head commands. In command documentations, if you see something between brackets (), it indicates that the contents of the bracket are optional. Similarly, arguments are also not standardized. Some commands expect filenames as argument and some may expect directory name or a regular expression.
As you start using commands, you may remember some of the options of frequently used commands but it is simply not possible for you to remember all the options of any command. Why? Because a single command may have more than ten or twenty options. So, what do you do when you cannot recall all the options? You take help. And with help, I do not mean asking a question in It’s FOSS Linux forum. I ask to use the help option of the command. Every standard Linux command has a quick help page that can be accessed with -h or –help or both.
It gives you a quick glimpse of the command syntax, common options with their meaning and in some cases, command examples.
cat Command help page
The help page of the cat command; If you need more help, you can refer to the manpage i.e. manual of a command:
It goes in all of details and could be overwhelming to read and understand. Alternatively, you can always search on the internet for ‘examples of xyz commands in Linux’.
Linux is case-sensitive
Everything you type in the terminal is case-sensitive. If you do not take that into account, you’ll often run into bash: command not found or file not found errors. In the home directory, you have all the folders name starting with the upper case. If you have to switch to the Documents directory, you have to keep the first letter as D and not d. Otherwise, the terminal will complain.
You can have two separate files named file.txt and File.txt because for Linux, file and File are not the same.
Running shell scripts
You can run a shell script by specifying the shell:
Or you can execute the shell script like this:
The second one will only work when the file has execute permission.
Use tab completion instead of typing it all
The Ubuntu terminal is pre-configured with tab completion. This means that if you start writing something in the terminal and then hit tab, it tries to automatically complete it or provide options if there are more than one possible matches. It works for both commands as well arguments and filenames. This saves a great ton of time because you don’t have to write everything completely.
Ctrl+C and Ctrl+V is not for copy pasting in terminal
Ctrl+C, Ctrl+V might be the ‘universal’ keyboard shortcuts for copy paste but it doesn’t work in the Linux terminal. Linux inherits a lot of stuff from UNIX and in UNIX, Ctrl+C was used for stopping a running process. Since the Ctrl+C was already taken for stopping a command or process, it cannot be used for copy-paste anymore.
You can surely copy paste in the terminal
Don’t worry. You can still copy paste in the terminal. Again, there is no fixed rule for the copy-paste keyboard shortcuts because it depends on the terminal application you are using or the configuration you have in place.
In Ubuntu terminal, the default keyboard shortcut for copy is Ctrl+Shift+C and for paste, it is Ctrl+Shift+V.
You can use Ctrl+C to copy text and commands from outside the terminal (like a web browser) and paste it using Ctrl+Shift+V. Similarly, you can highlight the text and use Ctrl+Shift+C to copy the text from the terminal and paste it to an editor or other applications using Ctrl+V.
Avoid using Ctrl+S in the terminal
Another common mistake beginners do is to use the ‘universal’ Ctrl+S keyboard shortcut for save. If you use Ctrl+S in the terminal, your terminal ‘freezes’. This is coming from the legacy computing where there was no scope of scrolling back up. Hence, if there were lots of output lines, Ctrl+S was used for stopping the screen so that text on the screen could be read.
You can unfreeze your terminal with Ctrl+Q. But again, avoid using Ctrl+S in the terminal.
Pay attention to $ and <> in command examples
If you are referring to some online tutorial or documentation, you’ll see some command examples with text inside <>. This indicates that you need to replace the content along with < and > with a suitable value. For example, if you see a command like this:
You should replace the and with the respective actual values. It is and indication that the command is just an example and you have to complete it with actual values. Another thing to note here is that some tutorials show command examples that start with $ like this:
Dollar symbol at the beginning of command
This is a way for them to indicate that it is command (not command output). But many new Linux users copy the preceding $ along with the actual command and when they paste it in the terminal, it throws error obviously.
So, when you are copying some command, don’t copy the $ if it is there at the beginning. You should also avoid copying random commands for random websites specially when you do not understand what it does.
Since you are reading about copying commands, when you see commands in multiple lines together, you should copy one line at a time and run them on by one:
Avoid copying multiple commands together
The next section tells you how to run multiple commands in one go. You can run multiple commands at once without user intervention. You might have seen it already as an Ubuntu user in the form of this command:
sudo apt update && sudo apt upgrade
There are three different ways to combine commands in the terminal: ; Command 1 ; Command 2 Run command 1 first and then command 2
&& Command 1 && Command 2
Run command 2 only if command 1 ends successfully
|| Command 1 || Command 2
Run command 2 only if command 1 fails
Stop a running Linux command
If a Linux command is running in the foreground, i.e., it is showing output or you cannot enter any other command, you can stop it using the Ctrl+C keys. I discussed it previously. It comes from the legacy computing days of UNIX. So, the next time you see a command like top or ping running continuously and you want the terminal control back, just use these two keys:
Stop a running program in Linux with Ctrl+C
Clear the terminal
When I find that my screen is too cluttered with different kind of output, I clear the terminal screen before starting some other work. It just a habit but I find it helpful.
To clear the terminal, use the command
You may also use Ctrl+L terminal shortcut.
Exiting the terminal
In a few cases, I have seen people closing the terminal application to exit the session. You could do that but the proper way to exit a terminal is to use the exit command:
You may also use the keyboard shortcut Ctrl+D for Ubuntu terminal.
Above we touched on one more funny command: sudo
The sudo command allows you to run programs with the security privileges of another user (by default, as the superuser). It prompts you for your personal password and confirms your request to execute a command by checking a file, called sudoers , which the system administrator configures.
This is where another very big privilege of Linux comes in. Avoiding permission issues, by simply running a command as sudo. Running applications can also be done in this fashion. For example if you want to run the Arduino IDE, and if you have ever used this application in Linux; you will know just how often it gives permission errors. To bypass this you can simply type the following command in your terminal:
This will run the application as super-user, which has all permissions on everything. No more permission errors. You can run any application from terminal.
Honestly speaking, there is way too much to talk about. It is difficult to determine what should be considered as absolute basics and what should be left out. For example, I wanted to avoid including the information about paths because it needs detailed explanation but going too much in detail on a single could be overwhelming.
I have passed the stage where small things used to baffle me in the terminal. Getting to know the basics really did help me. After that… just Google it. I promise you no question is too dumb, I’m sure at least a few more people have asked the exact same question at least one.
Read more here