Useful Raspberry Pi Commands

superior_hosting_service

raspberry pi
raspberry-pi

Top 50 Useful Raspberry Pi Commands


Raspberry Pi Commands are useful for operating Raspberry Pi, which can be referred to as the most popular single-board computer around the world. It enables us, especially the students and developers, to work on a wide range of applications. From building a prototype to developing an existing software, Raspberry Pi can provide the support. But when one starts using a black or green screen rather than a colorful GUI in windows or mac, it becomes terrifying for most people. But there are many raspberry pi commands available that can be used from the terminal window to navigate and run applications on your pi device. Although it is not a convenient system, it can give you the power to have more control over your system and the Linux environment, as well.

Useful Raspberry Pi Commands

Raspberry Pi can be connected to many electronic devices to conduct physical computing. If you want to explore the IoT, you will need to take the help of general-purpose input and output pins that come with Raspberry. Many applications alongside this GPIO pins will not go into count if you do not know the useful commands as there is no other way to run those applications. So, we have listed all the essential, and most used raspberry pi commands.

System Upgrade


These are the raspberry pi commands one will need to use before starting a project on Raspberry Pi. They will help you to get the updated package list and upgrade the software packs as well.

1. Installing the Latest Package List and Upgrading the Software


If you are looking to start using Raspberry, the first thing you should do is updating the current package list to the latest version. This command actually does not install any latest software, instead, it helps you to get the new package list from the repositories to your SBC.

$ apt-get update

It is also used to resynchronize the package index from their source which is specified in /etc/apt/sources.list You should always perform an update operation before a dist-upgrade. After updating the package list to the latest version, your system can determine the available updates of the packages. This command is used to upgrade the version of the software.

$ apt-get upgrade

If you already have the latest version of any packages, they are retrieved and upgraded. But it leaves the packages from upgrading that depend on the current install status of other packages.

2. Updating the OS and Firmware


This command will install the latest version of the Linux distro you are using. Like apt-upgrade, this command may remove some packages while installing the newest package where required.

$ apt-get dist-upgrade

You can install the removed files from /etc/apt/sources.list. It also handles the dependencies pretty well and the smart resolution system detects the most important packages to upgrade. This command is intended to install the latest version of the firmware on your Pi device. But you can avoid it as it may install unstable files that come with the experimental firmware.

$ rpi-update

If you want to do all these, one way to save time is combining all the commands and the single line command will look like –

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo rpi-update

Packages Management


Package Management plays an important role while you are operating a raspberry pi device. It comes with a wide range of packages that allow you to install software or running different kinds of applications as well. We have included all the package controlling commands under this section which will be described below.

3. Installing a Package


You may require to install new packages while working on a project. If you want to install any other package that does not come with by default use this command.

$ apt-get install <package>

So if you want to install phpmyadmin package, you can use this command like this:

$ apt-get install phpmyadmin

4. Removing a Package


Similarly, if you want to remove an installed package you can do it also by using the remove command.

$ apt-get remove <package>

Similarly to remove phymyadmin package just use this name in place of package

$ apt-get remove phpmyadmin

These commands can help you to have full control over your raspberry environment.

5. Manage the Services


Raspbian is the official OS for Raspberry Pi. Raspbian is a great OS that comes with many commands dedicated to performing variational activities. The below-described raspberry pi commands allow you to manage the services or use them to control the execution of specific applications.

$ update-rc.d ssh enable

This command lets you start a service on the system boot. If you want to stop the service just use.

$ update-rc.d -f ssh remove

These commands are only useful for services. Here -f is used to delete the symbolic link. If you want to start any other command or script, edit the /etc/rc.local file using.

$ nano /etc/rc.local

It will open the rc.local file in the text editor Nano and you can edit the file accordingly.

6. Get the list of all Running Process


Sometimes it becomes so important to know which processes are running in the background. It helps developers to know which application is taking most of the memory or limiting the resources.

$ ps aux

This basic command will display all the running command on your raspberry. If you want to see a process that was run by a specific user simple use

$ ps -u pi

The output of this command will contain the process IDs which can be used later for other purposes. You will get a screen like this.

ps raspberry pi commands
ps-raspberry-pi-commands

7. Killing the Running Program


If you want to terminate a program that is running on your raspberry there are raspberry pi commands available out there. But for this, you will need process id which you can get using the previous command.

$ kill <pid>
$ kill 12345

Here, 12345 is the process id. It is also possible to stop all the occurrences of a program. Suppose if you want to stop all PHP scripts the following command might do the work.

$ killall php

These commands will terminate the program immediately. So make sure that the script is not engaged in any major activity as it might cause data corruption, system failure or unwanted situation. It is advised that you should use these commands at the last step or during the noncritical process.

File and Directory Commands


It is important to store, manage and access files on any OS. If there is a UI available then the task becomes much easier. But in Raspberry, you will need to use some commands for file management and navigating through directories.

8. Displaying the Contents


If you want to display all the contents of a particular file, Raspberry has dedicated command for this purpose. You can use this command to complete this task.

$ cat abc.txt

If you have a file called abc.txt and want to display the contents you can use this content. So basically you will need to use type cat followed by [filename].[filetype]

9. Navigate to the Directories


Raspberry Pi offers commands that allow the user to access files of any directories. By default, you start in the home folder. Suppose, if your user name is pi, the home directory will be located at /home/pi. Now if you want to visit a specific folder you will need this command.

$ cd/folderA/folderB

Here, folderB is the name of the folder you are looking for. If you want to go back to your home directory use this command.

$ cd /home/pi

10. Copying files


If you are planning to use Raspberry Pi, then this is the command you will be using frequently. This command will help you to copy files from one directory to the desired location.

$ cp /home/pi/documents/file.txt /home/pi/landing/

Here file.txt is the file you want to copy from the documents directory to perform the paste operation in the landing directory.

11. Get the Contents


If you want to see the contents that a directory is currently containing you can use the command mentioned below.

$ ls -l

You will need to first navigate to your desired directory and then use this command to know the list of files, alongside file size, date modified, and permissions as well.

12. Creating a New Directory


By now, you should be able to navigate through the existing directories, copy files from one directory to another, see the list of all files of a directory and so on. Now we are going to focus on creating a new directory using this command.

$ mkdir new_directory

If you want to call your new directory as new_directory then just use mkdir followed by the filename.

13. Moving and Renaming Files


If you are wondering about how to perform cut and paste operation just like Windows in your Raspberry Pi then you will be happy to know that there are dedicated raspberry pi commands for completing this operation.

$ mv /home/pi/documents/file.txt /home/pi/landing/

This mv command will move your file.txt file from the current directory which is documents to another existing directory called landing. You can also use file name and the desired location where the file is going to be moved like mv file.txt /home/pi/landing/
This command can also be used to rename file names within the same directories.

$ mv oldfile.txt newfile.txt

It will change the name from the old file to the new file of the .txt file

14. Deleting Files and Directories


If you want to delete a file or directory you can simply follow a straightforward approach which is using rm or rmdir followed by the file or directory name.

$ rm samplefile.txt
$ rmdir sample_directory

You should keep in mind that the directory will be deleted only if it is empty or does not contain any files or folders.

15. Copying Files over SSH Protocol


SSH is a security protocol that provides cryptographic security for an unsecured network. It is widely used for transferring data from one place to another. If you want to copy any file from a desktop computer to your raspberry remotely using the SSH encryption you use this command.

$ scp user@10.1.1.12:/home/landing/file.txt

Here, the user name and the ip address of the source PC is considered as user@10.1.1.12 while the name and the location of the file can be pointed using /home/landing/file.txt

16. Create an Empty File


It is one of the most used raspberry pi commands which allows you to open an empty file instantly. The type of the file can be of any kind.

$ touch example.txt

It will create an empty file of .txt type in the current directory. So you need to be in the desired directory where you want to have this new file before using this command.

Networking and Internet Commands


Raspberry Pi is used worldwide to build basic and advanced IoT applications. It is a popular device among the developers for its wide range of commands which help developers to work in this field. The most used networking raspberry pi commands are also described in this article.

17. Configuring Interface


Raspberry Pi comes with several interfaces. It is necessary for the current state of the configuration before developing a prototype. To know the configuration of the network interfaces you can use this command.

$ ifconfig
ifconfig raspberry pi commands
ifconfig-raspberry-pi-commands

Here, ifconfigmeans interface configuration. You can change the configuration of the network and get the list of all currently active interfaces.

18. Configuring Specific Interface


If you want to configure a particular interface you can do it too. Suppose, if you want to configure ethernet0/0, you can enter this command in the terminal.

$ ifconfig eth0

This command will only display the configuration of eth0/0 port.

19. Activating and Deactivation Interface


Users can manually activate and deactivate any interface anytime. This option lets you have full control over your raspberry and the associated devices as well. For activating an interface use this command:

$ ifconfig eth1 up

While if you want to disable the wireless network interface wlan1 use the below-mentioned raspberry pi command.

$ ifconfig wlan1 down

20. Setting up IP, Broadcast, and Masking


There are several raspberry pi commands under this section. You can use these commands separately and combine them to serve your desired purpose as well.

$ ifconfig wlan0 192.168.1.0

This command will set the mentioned IP address in the wlan0 wireless interface. To set up a broadcast address for a network you have to first specify an interface and then use the following command.

$ ifconfig wlan1 broadcast 192.168.1.253

In addition, if you want wlan0 to use the network mask of 255.255.255.0 the appropriate command will be:

$ ifconfig wlan0 netmask 255.255.255.0

However, you can also combine these commands and write in a single line to save your time.

$ ifconfig wlan0 172.168.1.0 netmask 255.255.255.0 broadcast 172.168.1.253

21. Dynamic IP Address using DHCP


You can set up any static IP address using the interface name and ifconfig. But when it comes to setting up a dynamic IP you will need to use DHCP and the required command is:

$ dhclient eth0

In this case, the eth0 interface will obtain an IP address dynamically from DHCP.

22. Checking Wireless Networks


This command is similar to ifconfig. It focuses on checking the connectivity of the wireless adapters while ifconfig is used for ethernet.

$ iwconfig

It checks the network that is being used by the wifi adapters. Besides you can also see all the available wireless networks using the following command

$ iwlist wlan1 scan

Again, if someone wants to get the electronic address of his single board computer he/she can use:

$ iwlist wlan0 scan | grep <filename>

Here you can use the name of your desired field following grep to get a list containing your desired field only.

23. Scanning your Network


One of the most important raspberry pi commands under the networking section. It works like a network scanner to identify the services and ports available on a computer by sending packets. It then analyzes the response and displays the result for the user.

$ nmap

This command will scan your computer to let you know the port numbers, MAC, protocol, open or closed state, OS, and so on. Alongside this, you will get other important information as well.

24. Test the Connectivity


If you have already worked in a Linux operating system or Networking sector, then this command is not supposed to be new to you. It judges the connectivity between two devices connected through the internet.

$ ping 10.0.0.1

So it will check the connectivity from your raspberry to the host using 10.0.0.1 ip. You can even use any web address instead of ip like this

$ ping www.google.com

25. Decrease the Interval Time


Ping command takes some time for receiving feedback from the destination address. If you want to decrease the ping interval time you can use it.

$ ping -i 0.2 www.google.com

You can also send the packets as quickly as possible to decrease the time also. This is an alternative way to the previous command.

$ ping -f google.com

26. Ping with Parameters


Ping command can often generate misinterpreted output or result may vary due to congestion in the network, failure of the ARP request, security protocols like firewall, routing error, packet filtering, data loss, cable fault or switch and router problem. So it is always better to have full control over the ping command. You can do this using the following parameters:

$ ping -w 6 www.google.com

If you insert -W after the ping command, it will specify the duration when the host computer will be sending packets to the remote host. In this command, your computer will ping the website of google for 6 seconds and after that, it will automatically be terminated.

Again, if you want to see the summary of the ping report you can insert C as a parameter. The number of packets that will be transmitted can also be specified.

$ ping -c 5 -q www.google.com

So, the host computer will send 5 packets and display a summary of the statistics at the end of the operation.

27. Downloading Files from Web


You will be surprised to know that there are raspberry pie commands available even for downloading files from a website. Typically it is not an easy job when there is no graphical user interface. However, use this command to complete the downloading operation.

$ wget https://www.website.com/sample.png

Here sample.png is the file you will be downloading while website.com is the webserver where this file is hosted.

System Information Commands


Raspberry Pi is a sophisticated device and contains a lot of information that you can not even imagine. On the other hand, if you do not know the insights of the system then it is impossible to get the best out of it. These useful raspberry pi commands allow you to discover many hidden details like the temperature of the processor, condition of the memory distribution, and so on.

28. Memory Size and Memory Distribution


Raspberry Pi has dedicated commands that will let you know the details of your memory. The result will include the memory size, used, and available memory unit also.

$ cat /proc/meminfo

This command will also show the number of active and inactive files alongside with the cache memory. Again, we have seen several partitions on our desktop computer. In the same way you can set the number of partitions for the memory of your raspberry.

$ cat/proc/partitions

This command will display the number of partitions you currently have on your SDcard or harddisk. Besides, to know the version of your pi you can write this command.

$ cat /proc/version

29. Get the Temperature


It is important to know the raspberry temperature. Because excessive heat can lead you to unwanted situations. Even it can cause system malfunction or unexpected results which is commonly known as thermal throttling. However, to know the temperature of the raspberry pi use this command.

$ vcgencmd measure_temp

Those who have overclocked the processor of the raspberry must check the temperature frequently. Because it is advised that you should always try to restrict the pi temperature below 80 degrees Celsius.

30. Parsing the Temperature Data


You can also use this data in a bash shell script using the egrep command. The temperature data will be extracted and displayed.

$ vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*'

Then you can print this data on your shell script using print_temp.sh command or can open it in the nano text editor by applying the following command.

$ nano print_temp.sh

If you are developing a project you should always keep track of the temperature as the performance of your prototype or software is also associated with this.

31. Get Packages Information


We have already discussed the raspberry pi commands that can provide memory-related information. You can also extract useful information regarding the packages that are currently installed.

$ dpkg – –get–selections

This command will display all the packages you are using. If you want to get package information related to a specific type package us this command

$ dpkg – –get–selections | grep ABC

In this case, this command will produce a result that will contain all the packages related to ABC.

32. Get USB hardware Information


There are raspberry pi commands available that displays the list of hardware connected through USB. To see the list of USB hardware:

$ lsusb

Besides, you can also know the version of Raspberry you are using by entering the following command:

$ cat /proc/version

It will show the actual kernel version used to build the Linux distribution you are using. Besides, information concerning the GCC compiler will also be generated.

33. Start or Stop any Service


Raspberry Pi provides many important services and these enable you to implement many sophisticated projects or develop any prototype. But there are services that can not be used at the same time. In that case to start or stop a service use this command:

$ sudo service apache start
$ sudo service apache stop

In some cases, you may have to use other commands for performing these operations depending on the services such as

$ sudo service apache reload
$ sudo service apache restart

Raspbian Commands


So far, we have discussed some of the most useful raspberry pi commands. But Raspbian has some exclusive command that you can use for developing your project. We have tried to include some of the widely used Raspbian commands in this section.

34. Using Raspberry Camera and Video


Raspberry Pi comes with a camera. Many projects need a camera module to function properly. Or if you are working with image processing you must plug in a camera with the module. You can capture images and videos by using the following commands:

$ raspistil

This command allows you to capture still pictures. To capture and save an image file using the next command.

$ raspistill -o image.jpg

Here, image.jpg is the name of the picture you just captured. Again, to capture a video with your raspberry pi the following command is useful.

$ raspivid -o video.h264 -t 10000

Here, t denotes the duration of the capture in milliseconds.

35. Control your GPIO Pins


One of the main reasons behind the huge popularity of raspberry pi is the availability of the General Purpose Input/Output pins. These pins enable raspberry to communicate with the outside world. Besides they allow you to connect several devices like led, buzzer, or motors. After connecting devices, you will need simple programming to control them for achieving your desired outcome. Some of these commands are:

$ raspi-gpio get
$ raspi-gpio get 20
$ raspi-gpio set 20 a5
$ raspi-gpio set 20 op pn dh

You can also get or set value for a specific pin using these commands. Focus on these commands as without applying these you can not connect any external hardware or control them according to your demand.

Other Useful Commands


We could not place these commands under any of the above disciplines. But these are also useful to complete some operations. So we have tried to keep this under one umbrella so that you do not miss any.

36. Check the Command history


As Raspberry Pi can only be operated using the command-line interface, you need to use a lot of commands. So it becomes difficult for the user to keep track of the commands they have used or implemented. To check the history of the command you can try this command.

$ history
history rapberry pi commands
history-rapberry-pi-commands

You can limit the number of commands to display also using the following command.

$ history 5

It will show the last 5 commands that you have applied.

37. Clearing the History


Using the previous command you can get the whole history of the commands used already. If you want to clear any of those applied commands you can do it using the next command.

$ history -d <command id>

To clear the whole history use $ history -c

38. Scheduling Tasks


Raspberry Pi allows the user to schedule tasks. The commands described below will help you to schedule any scripts to execute periodically at any given interval. These tasks may include backing up your data hourly to login to your CPU at a specific time. The tool Cron enables you to schedule tasks and Crontab is the file where you can write the instructions for each task.

$ crontab -l
$ crontab -e

Here l and e flag are used to display and edit lines respectively. Besides you can also use sudo apt install gnome-schedule to install a graphical interface for the crontab.

39. Control the Running Apps in Background


The below mentioned raspberry pi commands are used to run something in the background even if the session is terminated or completed. The commands are as follows.

$ screen -s <name>
$ screen -r <name>

So you can see that we have used two flags which are -s and -r. Here S denotes the starting a new screen with the given name while r indicates resuming a running screen with the following name. You can also use the id in place of a name but if you have forgotten the id use  $ screen -r

General Command List for your Raspberry


There is no graphical interface available for you in Raspberry. As a result, it becomes pretty hard even performing general tasks like checking date and time, shutting down, or rebooting the system. The below mentioned will help you to complete all these basic kinds of stuff.

40. PowerOff or Shutdown


If you want to shut down, you can use any of the following commands. But you must remember, do not use them unless it is necessary. Because all the processes will be terminated, and the Raspberry will shut down immediately.

$ poweroff
$ shutdown
$ shutdown -h now

41. Scheduled Shutdown and Reboot


Even if you want that your Raspberry will turn off at a particular time or set up a timer you can use this command:

$ shutdown -h 03.44

In case if you are wondering how to reboot your device, then let us tell you that you can also reboot your Raspberry Pi by entering this command

$ reboot

It will instantly reboot your system, so make sure there is no application running in the background as you might lose it.

42. Set Date and Time


Raspberry Pi can not track the time. It can display time only when it connects to the internet; otherwise, you will need to set up the current time every time you reboot the system. But it can display a date anytime you want.

$ date -s "Thu Jan 29 11:51:14 UTC 2021"

You can add a real-time clock to your raspberry via the I2C interface if real-time is required for developing your project.

43. GUI and Configuration


Raspberry PI can provide a working graphical user interface to some extent. After opening the configuration window, you can use the arrows to explore the screen.

$ startx

But you should keep in mind that before using the above command, make sure that your user owns all the configuration files. You can also see the configuration of your Raspberry by entering the following command.

$ sudo raspi-config

44. Finding a File


You may find it difficult to get the location of a file if you forget where you have stored it as the GUI is not present. But if you know the following command, it will become easy for you.

$ find/ -name rxample.txt

You will get the list of all directories that contain example.txt after the execution of the command. Similarly, you can use this command for other file types also. Like if you are looking for an image file you can modify the command like:

$ find/ -name photo.png

45. Opening a Text File


If you want to open a particular text file then you can use this command:

$ nano file1

This command will force the system to open file1 in the official text editor of the Linux distribution system, which is called Nano.

Powerful Commands


To increase your knowledge and skills, we have included some of the most powerful commands of the raspberry pi. If you are using Linux for a long time, then you might be familiar with these commands already. But to mastery your expertise in raspberry, these commands will contribute a lot.

46. Search and Display Modified String


It is an important command and works as a programming language. It helps to find any string and enables you to display the string as like as you want. With this single command, you can search, modify and display a string which will definitely save a lot of time and make you more efficient

awk [-F] [-v var=value] 'program' filename

AWK is the main command, while F is used as a field separator string. V denotes the variable which can be used later for different purposes. The program denoted the expression that will be used to process the desired file, which is a filename in this case. You can look at this example:

awk -F":" '{print $1}' /etc/passwd

This command means /etc/passed is the file going to be parsed. “:” is the field separator while ‘{print $1}’ displays only the first column.

47. Transforming Text


It is another sophisticated way to transform a text or a string. It works like awk, although simpler and less complicated to use. It allows you to extract a specific part of a text or file, as well. The basic syntax of this command is as follows.

$ cut <options> <file>
$ echo <string> | cut <options>

The first command is used to extract from a file while the second command cuts a specific portion of a string and displays it for the user. Loot at the following example to get a clear concept.

$ echo "abcdefghi" | cut -c 2-4

This command will display bcd where -c denotes the character data type of the string.

48. Monitor what Happens


When you are waiting for a file or a directory, you can utilize the time by using the watch command to see what actually happens. These commands are intended to execute the same instruction every two seconds.

$ watch date
$ watch ls -latr
$ watch cat output.txt

Also, you can set the interval time according to your demand. Use -n option for setting the interval time.

$ watch -n10 date

This command will display a date after every ten seconds.

49. Monitor the Network


Using the previous command, you can only monitor the internal environment. But the command that we will discuss now will focus on monitoring the network. Raspberry Pi allows you to detect which ports are open currently and monitor the flow of traffic as well. The commands you need to perform this operation are as follows.

$ netstat -l
$ netstat -lp
$ netstat -lpc

Here p and c option is added with the original command where p is used to include the process id, and c is used to refresh data simultaneously.

50. Troubleshooting Raspberry Pi


Sometimes troubleshooting is really important to identify if there is any driver or service that is restricting you to get the expected result. Also, all the error reports will be displayed alongside with the explanation of the event.

$ dmesg

You will get a normal message if everything is working fine. You can see what happens in the start sequence while booting, and the time elapsed. The output will be as the picture attached below.

dmesg raspberry pi commands
dmesg-raspberry-pi-commands

Using the command line for operating a computer is the most challenging part for many people. These commands can help newcomers in this field. To get started with the terminal panel, these raspberry pi commands will also help you to build any applications or start working on any project. Besides, if you have never used Linux, these commands will boost your confidence to start your journey. Raspberry Pi is an efficient and powerful single board computer. But you will never get the best out of it if you cannot get yourself used to these useful commands.