Bash on a Raspberry Pi
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 directorycd <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. Andcd /
will take you to the root directory.rmdir <directory>
remove an empty directoryls [-la] [directory]
list [all] the files [with access permissions and timestamps] in the [specified] directorytouch <file>
update the timestamp for a file; create an empty file if it does not existnano [file]
edit the contents of a file using the nano text editor (which is easier to use/learn than thevim
text editor)vim [file]
edit the contents of a file using vim (which is powerful but harder to learn thannano
)cat <file>
print the contents of a filehead [-n 5] <file>
print the first 10 [or a specified number of] lines from a filetail [-n 5] <file>
print the last 10 [or a specified number of] lines from a filerm [-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 directorycp <source> <destination>
copy a filechmod <permission-code> <name-list>
set file/directory access permissions (seeman 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 commandwhich <command>
show the full directory and file address for the command that would runfile <file>
show information about the type of file it happens to be
Process management commands
ps [aux]
report on the processes that are running on this systemkill <pid>
kill a specific process using the process identifier found in the ps commandtop
a dynamic real-time view of the top processes in a running systemhtop
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 backgroundnohup <command> &
run a background command that will continue after the terminal closesctrl-c
this key sequence cancels the command running in the foreground and returns control to screenctrl-z
this key sequence suspends the command running in the foreground and returns control to screenfg
resume execution of a suspended process; or move a background process to the foregroundbg
resume execution of a suspended process in the backgroundjobs
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]
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 informationuname -a
print key information about the Linux operating systemwho
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 identitiesdu [optional-directory-name]
— list the disk usage in the current or specified directorydf
report filesystem space usage and available disk free spacemount
report on the filesystems that are available (or mounted) on the systemfree
report the free physical and swap memory available to the system (ie. free memory)lsusb
list the USB devices within and connected to the systemlsmod
list the modules that are loaded in the Linux kernellsof
list all of the open files on the systemcat /proc/cpuinfo
show CPU informationcat /proc/meminfo
show memory use informationcat /proc/version
show which version of Linux you are runningdmesg
show the message log from the boot process — useful for debugginguptime
show how long the system has been up and what load it has been under
Bash variables, history and other Bash tricks
<command2> $(<command1>)
use the output of one command as the arguments for anotherenv
show all of the environment variablesexport 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 variablehistory
show the history of commands entered into the shell- The
up
anddown
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 historybind –p
get the complete list of Bash keyboard shortcuts
Raspberry Pi specific commands
vcgencmd get_mem arm
show the CPU memory allocationvcgencmd get_mem gpu
show the GPU memory allocationvcgencmd measure_temp
show the CPU temperatureecho “$(expr $(</sys/class/thermal/thermal_zone0/temp) / 1000)’C”
show the GPU temperaturevcgencmd measure_clock arm
CPU speed in kilohertz — other clocks can be reportedcat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
also shows the CPU speedvcgencmd measure_volts core
core voltage; volts at other locations can also be reportedvcgencmd get_config int
show the system’s configuration settingsvcgencmd version
show the firmware version information
Other useful commands
passwd
if you have not changed your password, do it now. The default pi password leaves your system vulnerable to hackersclear
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 environmentsudo mount -t <type> <device> <directory>
mount a device into the filesystemsudo umount <device>|<directory>
unmount a device from the filesystemsudo chown <owner> <file>
change the owner of a particular filesudo chgrp <group> <file>
change the group for a particular filesudo raspi-config
configure your raspberry pi
Account management commands
sudo useradd -c “John Smith” -m john
create a new user account for Johnsudo passwd john
set a password for Johnsudo usermod -aG sudo john
add John to the group of people who can use sudocat /etc/passwd | grep john
look at John’s entry in the password filesudo su — john
test that John’s new account workssudo userdel john
and delete John’s account
Software maintenance commands
sudo apt-get update
always do this firstsudo apt-get full-upgrade
then this — for a full upgrade including dependenciessudo apt-get install <package>
install a particular software packagesudo apt-get remove <package>
uninstall software but keep configuration filessudo apt-get purge <package>
completely uninstall softwareapt-cache show <package>
get information about an installed packagesudo apt-get install -f
find broken dependencies and install themdpkg --get-selections
list all of the installed packages
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 eth0sudo ifdown <iface>
disable a network interface — interface is typically wlan0 or eth0iwconfig
report on the wireless network interfaces available to the systemiw dev
another way to report on the wireless network interfaces available to the systemiwlist 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 pointsping <domain-name or internet-address>
check the connection to a specified addresswget <URL-for-file>
download a file from the internetssh <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 — seeman netstat
for the many optionsip 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
)
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.