Help with how to get info on USB stick

I am using a USB stick to store data because I have been warned not to do this with the SD card, as this kind of writing to the card is not good for them.

But either way, I am stuck getting how much memory is left on the USB stick. (Or SD card, if I use it)

I know there is the command df -h, but that is more human readable, and it isn't for the specific device.

So say it is a 1 G SD card. How can I get a command that shows me the remaining memory so I can be notified when it gets too low.

They both use the same technology: NAND.
The potential problem with both is writing/reading when the device writing/reading crashes, or gets unplugged. And although the risk should be low, it can still happen that you end up with an unusable storage stick/card.

To get the filesystem status for a particular device, first determine its mount point. With df -h you should see something like:

/dev/disk1s1                                  466Gi  295Gi  170Gi    64%     3567807 9223372036851208000    0%   /

Together with a bunch of others, one of which is your SD card/USB stick.

The last part/column in the output shows you where it is 'mounted' on.
Enter df -h /< the mount found in the list >

For use in node-red, you could use the exec node with something like

df -h /< mount found in the list >  | grep / | tr -s ' ' | cut -d " " -f4

which would give you the number in Gigabytes available.

1 Like

Thanks very much.

I shall have to "tweak" the output so it is a number, then I can do comparisons of value.

Just split and select the data required out of the df command

[{"id":"e9ee1b34.cfb5f8","type":"exec","z":"8376d5c1.273ed8","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":670,"y":700,"wires":[["5c3effd9.e932a"],[],[]]},{"id":"c65210d3.f1ccf","type":"inject","z":"8376d5c1.273ed8","name":"","topic":"","payload":"df -h /dev/sda1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":700,"wires":[["e9ee1b34.cfb5f8"]]},{"id":"601069e4.f909b8","type":"debug","z":"8376d5c1.273ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1170,"y":680,"wires":[]},{"id":"5c3effd9.e932a","type":"split","z":"8376d5c1.273ed8","name":"","splt":" ","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":830,"y":680,"wires":[["86f87ffc.c62c6"]]},{"id":"86f87ffc.c62c6","type":"switch","z":"8376d5c1.273ed8","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"25","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":970,"y":680,"wires":[["601069e4.f909b8"]]}]
1 Like

Ok, so that is:
looking for the / (Not sure why)
trimming repeat characters (set to a space) So really removing excess spaces.
cutting (not sure about this either) setting delimiter as a space (" ")
returning the 4'th entry.

Ok, so I get back

So I need to strip the trailing M from there.

I'll mess about with it but if you could help me get through the workings of the grep / and how the last part works: cut -d " " -f4

Ok. What I understand.
cut -d " " -f4
Use a space (" ") as a delimiter and return the 4'th section.

Now all I need to do is "strip" the message.... Or maybe better: Delete the last letter of the message. (M)

Sorry, but what is the command for doing that?
I'll look, but if may be quicker with a bit of help.


When I run the flow I get this back:


Not the size available.

Might have to alter the index number in the split node for your system
Also might have to alter inject string so that it reads your usb drive - it might not be /dev/sda1 on your system

It is /dev/sda1

I'm kind of bitten by bakman2's method. I just don't know all the commands to do the tricks to manipulate the string/s.

use @bakmans2 method and then add this in a function node to get rid of the letter

msg.payload = msg.payload.substring(0,msg.payload.length-1);
return msg;
1 Like

I didn't think of that.

I was thinking of doing it all in the original command.

Oh, bakman2.....

I worked out why you need the grep /. To get the second line which has the information I want.

Moving on.

I decided to use your way.

Like this:

msg.payload = msg.payload.substring(0,msg.payload.length-2);
msg.payload = parseInt(msg.payload);
return msg;

That way it is a number and stripped the trailing M.

But it had to be -2 not -1. But no worries.

Now on to the next part of the journey.

Because df -h /path outputs 2 lines, the first line shows the columns, we don't need that line so we grep the / which only occurs in the next line.

In one go, to end up with a number:

df -h /dev/sda1 | grep / | tr -s ' ' | cut -d " " -f4 | sed s'/[A-Za-z].$//'

The sed command (stream editor) can manipulate per line. And in this case it is looking for 1 or more uppercase or lowercase characters and replaces it with nothing. (regular expressions are a completely different world in which you can get lost, but extremely powerful).

Use a space (" ") as a delimiter and return the 4'th section.

Read it as "column".

There are many ways to Rome, this could also be done (perhaps simpler) with the awk command. Or just with a flow with split and change nodes like @cymplecy shows.

1 Like

Thanks. Really.

awk. I'll look at that.

Though I have got it working as @cymplecy said.

That is caused by the operating system, the flow will not be compatible crossplatform, some OS-es output 200MB, while other say 200M, same applies to G vs Gi

All good.

Again: Thanks.

Just for my curiosity to see the difference - what does df -h output on your system?

(Actually I was about to post on that.....)

pi@TelePi:/media/pi/9020-9C27 $ df -h /dev/sda1 | grep / | tr -s ' ' | cut -d " " -f4
pi@TelePi:/media/pi/9020-9C27 $ 

I get 1007M. So it is interesting that I need to specify the -2 rather than the -1

But to answer your question fully:

pi@TelePi:/media/pi/9020-9C27 $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        30G  4.2G   24G  15% /
devtmpfs        181M     0  181M   0% /dev
tmpfs           186M     0  186M   0% /dev/shm
tmpfs           186M   21M  165M  12% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           186M     0  186M   0% /sys/fs/cgroup
/dev/mmcblk0p1   42M   22M   20M  54% /boot
tmpfs            37M     0   37M   0% /run/user/1000
/dev/sda1      1007M  272K 1007M   1% /media/pi/9020-9C27
pi@TelePi:/media/pi/9020-9C27 $ 
466Gi  295Gi  170Gi

See the difference (-1) character or (-2)

Yeah, but if I am getting 1007M why is it I am needing -2 to delete the M?

That would be -1 yeah?

Actually (and honestly): I'm missing something I think.
Other than the first has 4 digits and the second and third have only 3 digits.