Bash on a Raspberry Pi

Bryan Palmer
Level Up Coding
Published in
8 min readJan 14, 2020

--

An image of raspberry pies
Photo by Joydeep Pal on Unsplash

So you have splashed out on the Raspberry Pi Zero, downloaded Buster Lite to a micro-SD card, and now you are getting up to speed with the Bourne Again Shell, commonly known as Bash. Let me help you with that.

In this piece, we will walk through the key Linux, Bash, and Raspbian commands you need to know. Treat this as a memory jogger. It is not a comprehensive list of every Linux/Raspbian command, and it does not go through all of the possible options: just some of the important ones.

You can use this piece to look up the online manual. For example, if you want to know more about what the cat command does, you can look it up by typing man cat into the terminal.

We will begin with user commands and some Bash keyboard tricks before moving to the super user commands that are necessary to administer your new system. With the following list, key options are enclosed in [square brackets]. Meta descriptions are enclosed in <angle brackets>.

File system commands

  • pwd print the working directory (AKA the current directory)
  • mkdir <directory> make a new directory
  • cd <directory> change the working directory — Note: cd ~ will take you back to your home directory. cd .. will take you to the parent directory from your current directory. And cd / will take you to the root directory.
  • rmdir <directory> remove an empty directory
  • ls [-la] [directory] list [all] the files [with access permissions and timestamps] in the [specified] directory
  • touch <file> update the timestamp for a file; create an empty file if it does not exist
  • nano [file] edit the contents of a file using the nano text editor (which is easier to use/learn than the vim text editor)
  • vim [file] edit the contents of a file using vim (which is powerful but harder to learn than nano)
  • cat <file> print the contents of a file
  • head [-n 5] <file> print the first 10 [or a specified number of] lines from a file
  • tail [-n 5] <file> print the last 10 [or a specified number of] lines from a file
  • rm [-rf] <file> remove a file [optionally recursively descending directories and forcing removal] — Note: be very careful with the -rf option: it cannot be undone.
  • mv <source> <destination> move (rename) a file or directory
  • cp <source> <destination> copy a file
  • chmod <permission-code> <name-list> set file/directory access permissions (see man chmod for all the permission codes)
  • grep <search> <file-list> search for text in a file — Note: grep can be piped to without an argument (eg. cat /etc/passwd | grep pi)
  • find <starting-directory> -name “file.name” search for a file in the file system
  • <command> < <input-file> > <output-file> use < and > to redirect input to and output from a command
  • <command> | <command> pipe the output from one command as input to a second command
  • which <command> show the full directory and file address for the command that would run
  • file <file> show information about the type of file it happens to be
An image of a large raspberry pie
Photo by Amy Simpson on Unsplash

Process management commands

  • ps [aux] report on the processes that are running on this system
  • kill <pid> kill a specific process using the process identifier found in the ps command
  • top a dynamic real-time view of the top processes in a running system
  • htop another dynamic real-time view of the top processes in a running system
  • <command> & use an ampersand at the end of a line to run a command in the background
  • nohup <command> & run a background command that will continue after the terminal closes
  • ctrl-c this key sequence cancels the command running in the foreground and returns control to screen
  • ctrl-z this key sequence suspends the command running in the foreground and returns control to screen
  • fg resume execution of a suspended process; or move a background process to the foreground
  • bg resume execution of a suspended process in the background
  • jobs list suspended and background processes [job numbers can be used with fg, bg, kill, disown]
  • disown –h %1 disown first job from the jobs list [like nohap: protects a process from hang-ups]
An image of raspberries, chocolate and cinnamon sticks
Photo by Massimo Adami on Unsplash

Get information about your system

  • man <command> get the online manual to find out more about a command (eg. man rmdir)
  • date get the current date, time and time zone information
  • uname -a print key information about the Linux operating system
  • who who is currently using this system?
  • w what is my username and which terminal am I using?
  • id see my real and effective user and group identities
  • du [optional-directory-name] — list the disk usage in the current or specified directory
  • df report filesystem space usage and available disk free space
  • mount report on the filesystems that are available (or mounted) on the system
  • free report the free physical and swap memory available to the system (ie. free memory)
  • lsusb list the USB devices within and connected to the system
  • lsmod list the modules that are loaded in the Linux kernel
  • lsof list all of the open files on the system
  • cat /proc/cpuinfo show CPU information
  • cat /proc/meminfo show memory use information
  • cat /proc/version show which version of Linux you are running
  • dmesg show the message log from the boot process — useful for debugging
  • uptime show how long the system has been up and what load it has been under
An image of berries in punnets
Photo by Timo Volz on Unsplash

Bash variables, history and other Bash tricks

  • <command2> $(<command1>) use the output of one command as the arguments for another
  • env show all of the environment variables
  • export VARNAME=”some value” set a shell variable and make it available to processes-Note: there are no spaces on each side of the equals sign.
  • echo $VARNAM print the contents of a shell variable [eg. echo $HOME]
  • unset VARNAME remove a shell variable
  • history show the history of commands entered into the shell
  • The up and down arrow keys allow you to quickly step through your command history
  • ctrl-r type this key, then start typing and Bash will auto-complete using a reverse history search
  • !! run the previous command again [useful with sudo !!, when you forgot to sudo the first time]
  • !!:0 <arguments> run the previous command again with new arguments
  • <command> !$ run a command, reusing the arguments from the previous command
  • !-2 run the second last command
  • !10 run the tenth command as identified from history
  • bind –p get the complete list of Bash keyboard shortcuts
Looking through the handle of a mug at a lone raspberry on a wooden table
Photo by Alexandra Niculai on Unsplash

Raspberry Pi specific commands

  • vcgencmd get_mem arm show the CPU memory allocation
  • vcgencmd get_mem gpu show the GPU memory allocation
  • vcgencmd measure_temp show the CPU temperature
  • echo “$(expr $(</sys/class/thermal/thermal_zone0/temp) / 1000)’C” show the GPU temperature
  • vcgencmd measure_clock arm CPU speed in kilohertz — other clocks can be reported
  • cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq also shows the CPU speed
  • vcgencmd measure_volts core core voltage; volts at other locations can also be reported
  • vcgencmd get_config int show the system’s configuration settings
  • vcgencmd version show the firmware version information
A slice topped with raspberries
Photo by Jordane Mathieu on Unsplash

Other useful commands

  • passwd if you have not changed your password, do it now. The default pi password leaves your system vulnerable to hackers
  • clear to clear the screen

System administration commands

  • sudo <command> run a command as the super user (AKA system administrator or root user)
  • sudo sh fire up a shell with super user powers in the current directory (can be dangerous)
  • sudo su - start a login shell in root’s home directory with root’s environment
  • sudo mount -t <type> <device> <directory> mount a device into the filesystem
  • sudo umount <device>|<directory> unmount a device from the filesystem
  • sudo chown <owner> <file> change the owner of a particular file
  • sudo chgrp <group> <file> change the group for a particular file
  • sudo raspi-config configure your raspberry pi
A pile of raspberries
Photo by Liliya Lugovaya on Unsplash

Account management commands

  • sudo useradd -c “John Smith” -m john create a new user account for John
  • sudo passwd john set a password for John
  • sudo usermod -aG sudo john add John to the group of people who can use sudo
  • cat /etc/passwd | grep john look at John’s entry in the password file
  • sudo su — john test that John’s new account works
  • sudo userdel john and delete John’s account
A raspberry pie
Photo by Amy Simpson on Unsplash

Software maintenance commands

  • sudo apt-get update always do this first
  • sudo apt-get full-upgrade then this — for a full upgrade including dependencies
  • sudo apt-get install <package> install a particular software package
  • sudo apt-get remove <package> uninstall software but keep configuration files
  • sudo apt-get purge <package> completely uninstall software
  • apt-cache show <package> get information about an installed package
  • sudo apt-get install -f find broken dependencies and install them
  • dpkg --get-selections list all of the installed packages
Punnets of raspberries
Photo by Mona Eendra on Unsplash

Internet and WiFi commands

  • hostname [-f] report on the internet hostname for this system [in full]
  • ifconfig [-av] report on the network interfaces available to the system [in full]
  • sudo ifup <iface> enable a network interface — interface is typically wlan0 or eth0
  • sudo ifdown <iface> disable a network interface — interface is typically wlan0 or eth0
  • iwconfig report on the wireless network interfaces available to the system
  • iw dev another way to report on the wireless network interfaces available to the system
  • iwlist wlan0 scan scan for wireless access points (sometimes useful to pipe to grep “SSID”)
  • sudo iw dev wlan0 scan another way of scanning for wireless access points
  • ping <domain-name or internet-address> check the connection to a specified address
  • wget <URL-for-file> download a file from the internet
  • ssh <login@address> make a secure shell connection to another system (eg. ssh pi@10.1.1.5)
  • netstat -lp see what is happening on your network — see man netstat for the many options
  • ip address see active internet addresses on the system (for the many options see: man ip)
  • iptables -h like a firewall, iptables blocks and allows traffic (see also: man iptables)
A glass filled with raspberries
Photo by Rutger van Deelen on Unsplash

Shutdown/reboot commands

  • sudo shutdown -h now halt the computer now (syncing the filesystem first)
  • sudo reboot immediately restart your computer

Concluding remarks

Well that is your feast of commands for the Raspberry Pi. I hope you are feeling fully sated.

--

--

Bryan recently retired after a long career in government service. Bryan has first class honours degree in political science and a masters degree in econometrics