Raspberry Pi - Linux Shell

The Shell, called Bash in Raspberry Pi, is the text-based way of issuing instructions to your Pi board. In this chapter, let us learn about the Linux shell in Raspberry Pi. First, we will understand how to open a shell window.
You can open a shell window by using one of the two following ways −
There is a Terminal icon, having a >_ prompt, on the top of the screen. Click on it and you will get a shell window.
There is a Terminal icon, having a >_ prompt, on the top of the screen. Click on it and you will get a shell window.
Another way is to use the Accessories section of the Application menu. You can find the Terminal there.
Another way is to use the Accessories section of the Application menu. You can find the Terminal there.
Both of the above approaches will open a shell window on the desktop.
The prompt looks like as follows −
pi@raspberrypi ~ $

It contains lots of information. Let us see the various bits −
pi
It represents the name of that user who logged in.
raspberrypi
It represents the hostname of the machine i.e.; the name other computers use to identify when connecting to it.
The tilde symbol (~)
The tilde symbol tells the user which directory they are looking at. The presence of this horizontal wiggly line is known as home directory and the presence of this symbol shows that we are working in that directory.
The dollar sign ($)
It represents the presence of the ordinary user and not all-powerful superuser. A # symbol means a superuser.
When you start the shell window, you start in your home directory.
To see the folders and files in your home directory, you need to issue a command which is as follows −
pi@raspberrypi ~ $ ls

The output is as follows −
Desktop Downloads Pictures python_games Videos
Documents Music Public Templates

You can see the files and folders after issuing the ls command.
As we know that Linux is case sensitive hence the commands LS, Ls, ls and lS are all different.
You can see the above output, it’s all blue which means these are all directories. We can go to these directories and check which files they contain. The command to change the directory is cd. You need to use the cd command along with the name of the directory which you want to see.
The example for changing the directory in Raspberry Pi is given below −
pi@raspberrypi ~ $ cd Pictures

The command to find the information about a particular file is file. You need to put the name of the file after the command to check the information about that file.
Check the below example for finding information about the files in Raspberry Pi−
pi@raspberrypi ~ /Pictures $ file leekha.png aarav.png
leekha.png: PNG image data, 50 x 85, 8-bit/color RGBA, noninterlaced
aarav.png: PNG image data, 100 x 150, 8-bit/color RGBA, noninterlaced

We can also use the file command on directories. It will give us some information about the directories as well −
pi@raspberrypi ~ $ file Pictures Desktop
Pictures: directory
Desktop: directory

Earlier, we have used the cd command to change into a directory that is inside the current working directory. But sometimes, we need to go to the parent directory i.e. into the directory which is above the current working directory.
The command for this is cd..(cd with two dots), as given below −
pi@raspberrypi ~ /Pictures $ cd..
pi@raspberrypi ~ $

The tilde symbol represents your home directory.
The following diagram shows the part of the directory tree on your Raspberry Pi computer −
The directories and their uses are as follows −
bin
Bin, short for binaries, contains some small programs that behave like commands in the shell. For example, ls and mkdir.
boot
This directory contains the heart of the OS i.e. the Linux kernel. It also contains the configuration files containing the technical settings for Raspberry Pi computer.
dev
This directory contains a list of devices. For example, the devices like disks and network connections.
etc
This directory is used for various configuration files. These configuration files apply to all the users on the computer.
home
This is the directory where a user can store or write files by default.
lib
The directory contains various libraries that are used by different OS programs.
lost+found
This directory is used, if the file system gets corrupted and recovers partially.
media
You connect a removable storage device such as a USB key and it is automatically recognized. All the details will be stored in the media directory.
mnt
mnt stands for mount and will store all the details of the removable storage devices that we mount ourselves.
root
It is reserved for the use of the root users and we don’t have the permission to change into this directory as an ordinary user.
The shell enables the Raspberry Pi users to go straight to the location by specifying a path.
We have the following two types of paths −
It is a bit like giving the directions to the directory from where the user is now.
On the other hand, an absolute path is like a street address. This path is exactly the same wherever the user is. These paths are measured from the root. Hence, they start with a slash (/).
For example, we know the absolute path to the pi directory is /home/pi.
Now, go straight forward to this directory by using the following command −
cd /home/pi

If you want to go to the root, you can use the following command −
cd /

We can use the listing command (ls) to look inside any directory outside the current working directory as follows −
pi@raspberrypi ~ $ ls /boot

There are several advanced options, which we can use with the ls command.
These options are given in the following table −
This option is 1 not l and it outputs the results in a single column instead of a row.
The ls command with this option will display all the files. All the files will also include hidden files.
This option will add a symbol besides a filename. It will do this to indicate the file type. If you use this option, you will notice a / after directories names and a * after executable files.
This option is short for human-readable. It expresses file sizes by using kilobytes, megabytes, and gigabytes.
This option will display the result in the long format. It shows the information about the permissions of files, their last modification date, their size.
This option will list the result as a list separated by commas.
This option is the recursive option. It will also list files and directories in the current working directories, open the subdirectories(if any) and list their results too.
This option will sort the result as per the date and time they were last modified.
This option will sort your result as per the file extension.
Furthermore, we will learn about the other important aspects related to Linux Shell in Raspberry Pi.
Long format is one of the most useful formats of the ls command, because it provides us the additional information on the file.
You can use the ls command with the long listing option as follows −
pi@raspberrypi ~ $ ls -l
total 65
-rw-r--r-- 1 pi pi 256 Feb 18 22:45 Leekha.txt
drwxr-xr-x 2 pi pi 4096 Jan 25 17:45 Desktop
drwxr-xr-x 5 pi pi 4096 Jan 25 17:50 Documents
drwxr-xr-x 2 pi pi 4096 Jan 25 17:52 Downloads
drwxr-xr-x 2 pi pi 4096 Jan 25 17:53 Music
drwxr-xr-x 2 pi pi 4096 Jan 25 17:45 Pictures
drwxr-xr-x 2 pi pi 4096 Jan 25 17:45 Public
drwxr-xr-x 2 pi pi 4096 Jan 25 17:54 Templates
drwxr-xr-x 2 pi pi 4096 Jan 25 17:54 Videos

From the above output, it is very easy to understand that each line relates to one file or directory having its name on the right and the date and time, when it was last modified next to that.
The number 256, 4096 represents the size of the file. You can see some files and directories are having the same size.
The remaining part of this output shows the permissions i.e. who is allowed to use the file and what the user is allowed to do with that file or directory.
The permissions on a file are divided in the following three categories −
Owner
It is the person who created the file. This permission consists of the things the file owner can do.
Group
These are the people who belong to a group that has the permission to use the file. This permission consists of the things which the group owners can do.
World
These are known as the world permissions i.e. the things that everyone can do with that file or directory.
In Raspberry Pi, we have two main types of files. One is regular files which have a hyphen (-) and others are directories having a d.
Now let us understand the different types of permissions the owner, group and world have respectively −
Read permission − This permission gives the user ability to open and look at the contents of a file or to list a directory.
Read permission − This permission gives the user ability to open and look at the contents of a file or to list a directory.
Write permission − This permission gives the user the ability to change the content of a file. It allows the user to create or delete the files in a directory.
Write permission − This permission gives the user the ability to change the content of a file. It allows the user to create or delete the files in a directory.
Execute permission − This permission gives the user an ability to treat a file as a program and run it. It also gives permission to enter a directory using the cd command.
Execute permission − This permission gives the user an ability to treat a file as a program and run it. It also gives permission to enter a directory using the cd command.
The ls command deluges with the information that you cannot even notice sometimes, because it flies past our eyes faster than we understand or see it. To avoid this or solve this problem, we can use a command called less.
This command will take our listing and enable us to page through it and that is one screen at a time. To use this command, we need to use a | (pipe character) after the listing (ls) command.
The example of less command in Raspberry Pi is given below −
ls -RXF | less

The less command can also be used to view the content of a text file.
For this, we need to provide the filename as an argument, as given below −
less /boot/config.txt

Here we will be learning few tricks to speed up the use of the shell −
If you want to retype a command, then you can save retyping it because, shell keeps the record of history i.e. the commands you entered previously.
If you want to retype a command, then you can save retyping it because, shell keeps the record of history i.e. the commands you entered previously.
In case if you want to reuse your last command, you just need to use two exclamation marks and press enter.
In case if you want to reuse your last command, you just need to use two exclamation marks and press enter.
You can also bring back the previous commands in order by tapping the up arrow.
You can also bring back the previous commands in order by tapping the up arrow.
Similarly, you can also move through your history of commands in another direction by tapping the down arrow.
Similarly, you can also move through your history of commands in another direction by tapping the down arrow.
The shell also guesses what the user wants to type and it also automatically completes it for us.
The shell also guesses what the user wants to type and it also automatically completes it for us.
Redirecting files means, you can send the results from a command to a file instead of sending the results to the screen. For this, we need to use a > (greater-than) sign along with the file name, which we would like to send the output to.
The example of creating file by using redirection in Raspberry Pi is given below −
ls > ~/gaurav.txt

There are other commands too and with their help, we can display the content online. These commands are explained below −
The echo command, as the name implies, will display on screen whatever we write after it. The best use of this command is to solve mathematical problems. You need to put the expression between two pairs of brackets and put a dollar sign in front.
The example of echo command is given below −
echo $((5*5))

The date command, as the name implies, will display on screen the current date and time.
The cal command (cal stands for calculator) will display the current month’s calendar with today highlighted. With the help of option -y, you can see the whole year calendar.
Here, we will understand how to create and remove directories in Raspberry Pi. Let us begin by learning about creating directories.
The command to create a directory under your home directory is mkdir.
In the below example, we will be creating a directory named AI_Python −
mkdir AI_Python

You can also use one command to create several directories as follows −
pi@raspberrypi ~ $ mkdir AI_Python Machine_Learning Tutorialspoint
pi@raspberrypi ~ $ ls
Downloads AI_Python Machine_Learning Tutorialspoint Desktop Pictures Documents Public

If you want to remove an empty directory, you can use the command rmdir as follows −
pi@raspberrypi ~ $ rmdir AI_Python

On the other hand, if you want to remove non-empty directories, you need to use the command rm -R as follows −
pi@raspberrypi ~ $ rm -R Machine_Learning

We can use the rm command to delete a file.
The syntax for deleting a file would be as follows −
rm options filename

In an example given below, we will be deleting a text file named leekha.txt −
pi@raspberrypi ~ $ rm leekha.txt

Like mkdir, the rm command will not tell us what it is doing.
To know its function, we need to use the verbose(-v) option as follows −
pi@raspberrypi ~ $ rm -v leekha.txt
removed 'leekha.txt'

We can also delete more than one file at a time as follows −
pi@raspberrypi ~ $ rm -v leekha.txt gaurav.txt aarav.txt
removed 'leekha.txt'
removed 'gaurav.txt'
removed 'aarav.txt'

A directory contains a lot of files with the similar filenames and if you want to delete a group of such files, you don’t need to repeat the command by typing out each filename. In shell, wildcards will do this job for us.
Following table provides us a quick reference to the wildcards, which we can use in Raspberry Pi −
The below given example will remove all the files starting with letters lee,
rm –vi lee*

Copying files is one of the fundamentals things we would like to do.
The command for this is cp, which can be used as follows −
cp [options] copy_from copy_to

Here, we need to replace copy_from with the file you want to copy and copy_to for where you want to copy it.
Let us see an example of using the command to copy the respective file.
Suppose, if you want to copy the file leekha.txt from the /desktop directory to the home directory, you can use the cp command as follows −
cp /Desktop/leekha.txt ~

We can also specify a path to an existing folder to send the file to as follows −
cp /Desktop/leekha.txt ~/doc/

Rather than making a copy of the file, if you want to move it from one place to another then, you can use the mv command as follows −
mv ~/Desktop/leekha.txt ~/Documents

The above command will move the file named leekha.txt from Desktop directory to the Documents directory. Both of these directories are in the home directory.
With the help of following command, we can reboot our Raspberry Pi without disconnecting and reconnecting the power −
sudo reboot

With the help of following command, we can safely turn off our Raspberry Pi −
sudo halt


    48 Lectures 
     2 hours 

 Comfiny

    27 Lectures 
     2 hours 

 Axel Mammitzsch

    25 Lectures 
     1 hours 

 Axel Mammitzsch

    96 Lectures 
     9.5 hours 

 Edouard Renard

    10 Lectures 
     34 mins

 Ashraf Said

    12 Lectures 
     45 mins

 Ashraf Said
 Print
 Add Notes
 Bookmark this page