- Raspberry Pi - Discussion
- Raspberry Pi - Useful Resources
- Raspberry Pi - Quick Guide
- Third-party Software Package
- Raspberry Pi - Add-on Boards
- Raspberry Pi - GPIO Connector
- Raspberry Pi - Managing Software
- Raspberry Pi - Linux Shell
- PIXEL Desktop Environment
- Raspberry Pi - Working with Linux
- Raspberry Pi - Configuration
- Connecting Raspberry Pi
- Raspberry Pi - Operating System
- Raspberry Pi - Getting Started
- Raspberry Pi - Introduction
- Raspberry Pi - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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.
Open 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. Cpck on it and you will get a shell window.
Another way is to use the Accessories section of the Apppcation menu. You can find the Terminal there.
Both of the above approaches will open a shell window on the desktop.
Understanding the Prompt
The prompt looks pke 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 pne 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.
List Files and Directories
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
Output
The output is as follows −
Desktop Downloads Pictures python_games Videos Documents Music Pubpc 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.
Change the directory
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
Find information about files
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
Parent Directory
Earper, 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.
Directory Tree
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 pke 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 pst of devices. For example, the devices pke 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.
pb
The directory contains various pbraries 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.
Relative and Absolute Paths
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 −
Relative path
It is a bit pke giving the directions to the directory from where the user is now.
Absolute path
On the other hand, an absolute path is pke 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 /
Advanced Listing Commands
We can use the psting 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 −
Option | Description |
---|---|
-1 | This option is 1 not l and it outputs the results in a single column instead of a row. |
-a | The ls command with this option will display all the files. All the files will also include hidden files. |
-F | 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. |
-h | This option is short for human-readable. It expresses file sizes by using kilobytes, megabytes, and gigabytes. |
-l | 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. |
-m | This option will pst the result as a pst separated by commas. |
-R | This option is the recursive option. It will also pst files and directories in the current working directories, open the subdirectories(if any) and pst their results too. |
-r | It is the reverse option and will display the result in reverse order. |
-S | This option will sort the result by their size. |
-t | This option will sort the result as per the date and time they were last modified. |
-X | 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 Listing Format
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 psting 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 Pubpc 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 pne 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.
Permissions
The permissions on a file are spanided 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.
Types of Permissions
Now let us understand the different types of permissions the owner, group and world have respectively −
Read permission − This permission gives the user abipty to open and look at the contents of a file or to pst a directory.
Write permission − This permission gives the user the abipty 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 abipty to treat a file as a program and run it. It also gives permission to enter a directory using the cd command.
Less Command
The ls command deluges with the information that you cannot even notice sometimes, because it fpes 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 psting 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 psting (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
Speed up the use of Shell
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.
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.
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.
Create File with Redirection
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 pke 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 onpne. These commands are explained below −
echo command
The echo command, as the name imppes, 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))
date command
The date command, as the name imppes, will display on screen the current date and time.
cal command
The cal command (cal stands for calculator) will display the current month’s calendar with today highpghted. With the help of option -y, you can see the whole year calendar.
Create and Remove Directories
Here, we will understand how to create and remove directories in Raspberry Pi. Let us begin by learning about creating directories.
Create 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 Pubpc
Remove directories
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
Delete Files
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
Raspberry Pi Wildcards
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 −
Wildcard | Meaning | Example | Description |
---|---|---|---|
? | It means any single character. | pic?.jpg | The example means that the files start with a pic and have exactly one character after it before extension starts. |
* | It means any number of characters. | *pic* | The example means that any files that have the word pic in their filename. |
[…] | This wildcard will match any one of the characters in brackets. | [gla]* | The example means that all the files that start with the letter g, l or a. |
[^…] | This wildcard will match any single character that is not between the brackets. | [^gla]* | The example means that any files that do not start with the letter g, l or a. |
[a-z] | This wildcard will match any single character in the range specified. | [x-z]*.png | The example means that any files that start with a letter x, y or z and end with the .png extension. |
[0-9] | This wildcard will match any single character in the range specified. | Pic[1-5]*.png | The example means that it will match pic1.png, pic2.png, pic3.png, pic4.png, and pic5.png. |
The below given example will remove all the files starting with letters lee,
rm –vi lee*
Copy Files
Copying files is one of the fundamentals things we would pke 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.
Example
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/
Move Files
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.
Reboot Raspberry Pi
With the help of following command, we can reboot our Raspberry Pi without disconnecting and reconnecting the power −
sudo reboot
Shutdown Raspberry Pi
With the help of following command, we can safely turn off our Raspberry Pi −
sudo haltAdvertisements