Tuesday, March 19th, 2024
Latest updates

50 Raspberry Pi Linux Bash commands you’ll actually use

So, what is Bash?

The terminal (or ‘command-line’) on a computer allows a user a great deal of control over their system. All of these tools allow a user to directly manipulate their system through the use of commands. These commands can be chained together and/or combined together into complex scripts (see the linux usage page on scripting) that can potentially complete tasks more efficiently than much larger traditional software packages.

Bash is a Unix shell and command language written by Brian Fox for the GNU Project that powers the text interface of your Raspberry Pi. Every time you type a command into the terminal, you’re speaking Bash. First released in 1989, it has been distributed widely as the default login shell for Linux distributions and Apple Mac OSX. A version is also available for Windows 10. Before computers had graphical interfaces it was the only way you could interact with them. It’s necessarily very powerful and flexible. 

On the Raspberry Pi (running Raspbian), the default terminal application is LXTerminal.

A shell is a command processor which lets you manipulate your computer by typing in commands. Commands are typed after the “prompt” which is a brief snippet of text informing you who and where you are on the system:

Here is my list of useful Rasbian bash terminal commands:

General commands
apt-get update
Synchronizes the list of packages on your system to the list in the repositories. Use it before installing new packages to make sure you are installing the latest version.
apt-get upgrade
Upgrades all of the software packages you have installed.
chmod +x filename
Change the rights to a file so that it can run as a program.
./myProgram.bin
To run a program you must put a ‘./’ before the filename if you are in the same directory as the program file:
Otherwise just type the path to the file:
$ /home/pi/myProgram.bin
clear
Clears previously run commands and text from the terminal screen.
find / -name example.txt
Searches the complete system for the file example.txt and outputs a list of all directories that contain the file.
nano example.txt
Opens the file example.txt in the Linux text editor Nano.
poweroff
Shuts down the Raspberry immediately.
raspi-config
Opens the configuration settings menu.
reboot
Reboots the Raspberry Pi immediately.
shutdown -h now
To shut down immediately.
shutdown -h 14:30
To shut down at time 14:30
startx
Opens the GUI (Graphical User Interface).
File and directory
cat example.txt
Displays the contents of the file “example.txt”.
cd /abc/xyz
Changes the current directory to the /abc/xyz directory.
cp XXX
Copies the file or directory XXX and pastes it to a specified location; i.e. cp examplefile.txt /home/pi/office/ copies examplefile.txt in the current directory and pastes it into the /home/pi/ directory. If the file is not in the current directory, add the path of the file’s location (i.e. cp /home/pi/documents/examplefile.txt /home/pi/office/ copies the file from the documents directory to the office directory).
ls -l
Lists files in the current directory, along with file size, date modified, and permissions.
mkdir example_directory
Creates a new directory named example_directory inside the current directory.
mv XXX
Moves the file or directory named XXX to a specified location. For example, mv examplefile.txt /home/pi/office/ moves examplefile.txt in the current directory to the /home/pi/office directory. If the file is not in the current directory, add the path of the file’s location (i.e. cp /home/pi/documents/examplefile.txt /home/pi/office/ moves the file from the documents directory to the office directory). This command can also be used to rename files (but only within the same directory). For example, mv examplefile.txt newfile.txt renames examplefile.txt to newfile.txt, and keeps it in the same directory.
rm example.txt
Deletes the file example.txt.
pwd
Print Working Directory FULL path. It is used for :
=> Find the full path to the current directory.
=> Store the full path to the current directory in the shell variable.
=> Verify the absolute path.
=> Verify the physical path i.e exclude symbolic links.
rmdir example_directory
Deletes the directory example_directory (only if it is empty).
scp user@10.0.0.32:/some/path/file.txt
Copies a file over SSH. Can be used to download a file from a PC to the Raspberry Pi. user@10.0.0.32 is the username and local IP address of the PC, and /some/path/file.txt is the path and file name of the file on the PC.
touch example.txt
Creates a new, empty file named example.txt in the current directory.
Network and web commands
hostname -I
Shows the IP address of your Raspberry Pi.
ifconfig
To check the status of the wireless or wired connection you are using  (to see if wlan0 has acquired an IP address).
iwconfig
To check which network the wireless adapter is using.
iwlist wlan0 scan
Prints a list of the currently available wireless networks.
iwlist wlan0 scan | grep ESSID
Use grep along with the name of a field to list only the fields you need (for example to just list the ESSIDs).
nmap
Scans your network and lists connected devices, port number, protocol, state (open or closed) operating system, MAC addresses, and other information.
ping
Tests connectivity between two devices connected on a network. For example, ping 10.0.0.32 will send a packet to the device at IP 10.0.0.32 and wait for a response. It also works with website addresses.
wget http://www.somewebsite.com/example.txt
Downloads the file example.txt from the web and saves it to the current directory.
System info
cat /proc/meminfo
Shows details about your memory.
cat /proc/partitions
Shows the size and number of partitions on your SD card or hard drive.
cat /proc/version
Shows you which version of the Raspberry Pi you are using.
cat /proc/cpuinfo shows your cpu version
ctrl-c
Terminate current program
df -h
Shows information about the available disk space.
df /
Shows how much free disk space is available.
dpkg ––get–selections | grep XXX
Shows all of the installed packages that are related to XXX.
dpkg ––get–selections
Shows all of your installed packages. May produce a lot of output; Pipe it through “less” or “more”; Like this: dpkg --get-selection | less
free
Shows how much free memory is available.
kill
To force quit (kill) an application type:
$ sudo killall applicationName.bin
Or with PID number:
$ sudo kill -9 XXXXX
XXXXX is the PID for the process you want to kill. Can be found using the ‘top’ command
lsusb
Lists USB hardware connected to your Raspberry Pi.
top
top rovides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime.
UP key
Pressing the UP key will print the last command entered into the command prompt. This is a quick way to repeat previous commands or make corrections to commands.
vcgencmd measure_temp
Shows the temperature of the CPU.
vcgencmd get_mem arm && vcgencmd get_mem gpu
Shows the memory split between the CPU and GPU.

Services

First, you can determine which system is currently running by means of the command:
sudo service --status-all

In a normal boot, thus you start at 2, and progress up to 5. To find the services started in this progression, issue the command:
# grep -nrI Default-Start /etc/init.d

SYSTEMD:
The command to list all services is
systemctl list-unit-files
which will spit out much info. To select all the services enabled, i.e, those which are started at boot, just grep the above command as follows:
systemctl list-unit-files | grep enabled
I prefer this command to systemctl list-units: the difference is that this one will only display services your OS has in memory = the OS thinks it needs), while systemctl list-unit-files will display all installed services, including those which are masked (i.e. those which are supposed never to be started, not even manually), and those which it will not activate, which are called static. In other words, the command I suggested is more comprehensive than the one listing only active services.

RCCONF
For simple startup service management on Raspberry Pi I recommend tool rcconf. It allows you to easily turn on/off services in /etc/init.d/. You can also see if they are enabled and will run at startup.
-select service:arrows
-space: toggle
-tab: select OK or CANCEL
To install the tool:
sudo apt-get install rcconf
Start the tool:
sudo rcconf

Remove programs

To uninstall a program, use the “apt-get” command, which is the general command for installing programs and manipulating installed programs. For example, the following command uninstalls grafana and deletes all the configuration files, using the “–purge” (there are two dashes before “purge”) command.

sudo apt-get --purge remove grafana
If you don’t want to remove the configuration files, simply leave out the “–purge” command, as shown in the following command.
sudo apt-get remove grafana

As discussed in this article, programs installed in Linux depend on other packages to function. When you uninstall a program, there may be packages that the uninstalled program depended upon that are no longer used. To remove any unused packages, use the “autoremove” command, as shown in the following command.
sudo apt-get autoremove

You can combine the two commands for removing a program and removing dependencies that are no longer being used into one, as shown below (again, two dashes before “auto-remove”).
sudo apt-get purge --auto-remove gimp

Python version control:
a proper way to do it is to use Debain’s update-alternatives

UPD: people are too lazy to just Read The Fine Manual :

update-alternatives –remove-all python
update-alternatives –install /usr/bin/python python /usr/python2/bin/python2 10
update-alternatives –install /usr/bin/python python /usr/python3/bin/python3 20
That’s the way it should be. After that to switch the things up :

update-alternatives –set python “/usr/python2/bin/python2”
to select the one you need. It won’t break PEP-394, because python2 and python3 binaries will be where they must be in multi-versioned install : in the installations’ prefixes(usr/python2 and /usr/python3 respectively)

pip list list installed py modules

Installing pip if necessary:
Ubuntu and Debian Linux users may
need to execute
sudo apt−get install python3−pip

3. Installing extra modules
You cannot install modules on the School machines. On your own computer, you can install thousands.
Mac and Linux users install (in particular) the modules matplotlib, beautifulsoup4, openpyxl and jupyter by executing
pip3 install matplotlib
pip3 install beautifulsoup4
pip3 install openpyxl
pip3 install jupyter
You can get a listing of the modules you have installed by executing
pip3 list
To check whether some of the modules you have installed are not up to date, execute
pip3 list −−ou tda ted
If a module some_outdated_module is listed as outdated, you can update it by executing
pip3 install −U some_outdated_module

After installation, example code can be found under /usr/src/sense-hat/examples.
These can be copied to the user’s home directory by running cp /usr/src/sense-hat/examples ~/ -a.

About Terje

Check Also

How to setup UART on the Raspberry Pi 3 GPIO

How to setup the UART on Raspberry Pi 3

Tips and tricks about configuring the UART on the Raspberry Pi 3 and Pi Zero W. The introduced bluetooth module makes this a little bit different.....

Datalogger example using Sense Hat, InfluxDB and Grafana

In this article we will make a simple datalogger application running on the Raspberry Pi. It is compatible with Raspberry Pi Zero W, Pi 2 and Pi 3.

How to Setup CLAWS Mail

What is Claws mail? Claws Mail is an email client aiming at being fast, easy-to-use …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.