raspberry pi tips – renaming a usb thumb drive

One of the easiest tasks to perform on Windows and macOS is renaming a USB thumb drive. Using the file explorer on either, you select the item, open its properties, and attempt to rename the thumb drive. Works without issue on Windows and macOS, but try that on Raspbian and you’ll get the following:

Oh dear. No obvious way to run this on the graphical desktop via sudo. It’s these kinds of it-should-be-the-same-on-Raspbian-but-it-isn’t moments that tends to kill some of the enthusiasm of newcomers to the Raspberry Pi and Raspbian itself. Rather than worry about that, let’s do it from the command line. It’s real easy, and you’ll learn a bit about the command line in the process.

First, let’s open an LXTerminal and do a directory listing of where the thumb drive is located (or mounted in Unixese).

pi@raspberrypi:~ $ ls -AlFh /media/pi/total 20Kdrwxr-xr-x 3 pi pi  16K Dec 31  1969 1D2D-5A2A/drwxr-xr-x 2 pi pi 1.0K Dec 31  1969 MICROBIT/pi@raspberrypi:~ $ 

You can tell I have the micro:bit inserted, but what’s 1D2D-5A2A? That, my friend, is the name given by the OS and the USB to the USB because it has no explicit device/volume name. I want to rename that to something more human-friendly. Before I can rename it, I need to find out what the device name is. Knowing it’s volume name won’t help. But I can use the volume name it currently has to find the device name. So let’s do that right now.

pi@raspberrypi:~ $ mount | grep 1D2D-5A2A/dev/sda1 on /media/pi/1D2D-5A2A type vfat ...pi@raspberrypi:~ $ 

When you run this command on your Raspberry Pi you’ll see it produces a lot more information than what I’ve show here, which is why I’ve added the ellipsis to the results. The information we are interested in is right up front, which is the device (/dev/sda1) and the type of file system it is, which in this case is vfat. We have our information, so let’s rename it. I’m going to call it ‘green32’ because it’s a green USB thumb drive with 32GB of storage.

pi@raspberrypi:~ $ sudo mlabel -i /dev/sda1 ::green32pi@raspberrypi:~ $ sudo umount /media/pi/1D2D-5A2Api@raspberrypi:~ 

Now unplug the USB thumb drive and plug it back in again. Raspbian will rediscover it and mount it with the new name you just gave it.

pi@raspberrypi:~ $ ls -AlFh /media/pi/total 20Kdrwxr-xr-x 3 pi pi  16K Dec 31  1969 GREEN32/drwxr-xr-x 2 pi pi 1.0K Dec 31  1969 MICROBIT/pi@raspberrypi:~ $ 

The keen-eyed among you will note that even though I used ‘green32’ it is seen by Raspian as ‘GREEN32’. Just accept it and move on.

From this point you can use it just like you would before you renamed it, except it’s name is more easily readable. Being able to pick a name for your volume makes building automated tools that look for a specific file path much easier to write, especially if your detachable storage has the same name, such as BACKUP for example, across multiple external devices.

raspberry pi utilities – rpinfo

rpinfo (or rpinfo.sh) is a Bash script that I’ve written over time to print out key statistics of my various Raspberry Pi systems. And when I say various, I mean various. Since 2013 I’ve slowly accumulated various versions of the Raspberry Pi, including several Raspberry Pi Zeroes, to the point where I now have over a dozen of the little critters sitting around and doing things.

The script will look for and if found, query certain applications I care about. Except for calling the external tools to get their versions, I’ve tried to keep everything within Bash itself, especially using Bash’s built-in string and array features. Of course it doesn’t always turn out that way. The use of cat (concatenate and print files), tr (translate characters), sed (stream editor) and grep (globally search a regular expression and print) is so deeply engrained in the way I write scripts due to decades of use.

Looking at the screen capture above, there are a few interesting items to call out. At the very top is the name of the board including revision. This is followed by a block of statistics that includes the CPU type and the OS Description. At the very bottom is the filesystem size. Yes, I have a 128GB Sandisk micro SDXC card installed. It’s an Extreme and I’ve been slowly picking them up from my local Costco because Costco has been selling them in a two pack for US $30. They’re high performance with lots of elbow room. With a 128GB card in the Raspberry Pi 4 I have a computer that exceeds my desk side Dell tower I was using in the early to mid 2000s.

The way I use this script is to have a copy on every Raspberry Pi I have in the house. It’s just a quick way to catalogue the critical features I need. It’s my hope you find this useful, either in whole or some of its parts.

#!/usr/bin/env bash## Copyright (c) 2019 William H. Beebe, Jr.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.## ------------------------------------------------------------------------------## Simple Bash script to display information about any Raspberry Pi board in# inventory.## Note that for the version string we have to use tr to substitute the ending# null byte for a newline, or else a warning about null is emitted by the# latest versions of Bash.#model=$(cat /proc/device-tree/model | tr '\0' '\n')echoecho " ${model}"echomemTotal=$(cat /proc/meminfo | grep MemTotal | sed 's/:/ :/' | sed 's/ */ /g')echo " ${memTotal}"# Look for the explicit processor/core type (i.e. 'Cortex-A53')#data=$(lscpu | grep 'Model name:')wordarray=(${data//:/ })echo " CPU Type : ${wordarray[2]}"# Look for the core count. Use wc (word count) to count lines (-l).#coreCount=$(cat /proc/cpuinfo | grep processor | wc -l)echo " Core Count : ${coreCount}"hardware=$(cat /proc/cpuinfo | grep Hardware | tr '\t' ' ')echo " ${hardware}"revision=$(cat /proc/cpuinfo | grep Revision | tr '\t' ' ')echo " ${revision}"echokernelRevision=$(uname -r)echo " Kernel Release: ${kernelRevision}"description=$(lsb_release --all 2>/dev/null | grep Description | tr '\t' ' ')echo " OS ${description}"echoecho " Tools"version=$(git --version)echo " Git: ${version}"echoecho " Languages Installed"golang='/usr/local/go/bin/go'if [ -e ${golang} ]thenversion=$(${golang} version)echo " Go: ${version}"elseecho " No Go found."firustlang="$HOME/.cargo/bin/rustc"if [ -e ${rustlang} ]thenversion=$(${rustlang} --version)echo " Rust: ${version}"elseecho " No Rust found."fi# Redirect stderr to stdout for Python 2 version, because# that's they way they did it, printing version string to# stderr...version=$(python -V 2>&1)echo " ${version}"version=$(python3 -V)echo " ${version}"version=$(pip3 -V)wordarray=(${version})echo " Pip ${wordarray[1]}"version=$(gcc --version)wordArray=(${version})echo " Gcc ${wordArray[3]}"echodf -kh .echo