# Help with how to get info on USB stick

**URL:** <https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773>\
**Category:** General\
**Created:** [28 July 2019 10:48 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773 "2019-07-28T10:48:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 10:48 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/1 "2019-07-28T10:48:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 July 2019 11:15 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/2 "2019-07-28T11:15:45Z")

</div>

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:

```auto
/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

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

```

which would give you the `number` in `Gi`gabytes available.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 11:25 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/3 "2019-07-28T11:25:15Z")

</div>

Thanks very much.

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [28 July 2019 11:26 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/4 "2019-07-28T11:26:42Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3837bc8d537df02aa8f4892d8959fc7d13934ae1.png)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 11:37 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/5 "2019-07-28T11:37:04Z")

</div>

> [@bakman2](#):
>
> df -h /dev/sda1 | grep / | tr -s ' ' | cut -d " " -f4

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  
`1007M`

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.  
🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 11:40 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/6 "2019-07-28T11:40:08Z")

</div>

Strange.

When I run the flow I get this back:

`/media/pi/9020-9C27`

Not the size available.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [28 July 2019 11:51 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/7 "2019-07-28T11:51:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 11:54 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/8 "2019-07-28T11:54:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [28 July 2019 11:56 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/9 "2019-07-28T11:56:30Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 11:57 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/10 "2019-07-28T11:57:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 12:11 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/11 "2019-07-28T12:11:05Z")

</div>

I decided to use your way.

Like this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 July 2019 12:15 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/12 "2019-07-28T12:15:17Z")

</div>

> [@Trying\_to\_learn](#):
>
> Ok, so that is:  
> looking for the `/` (Not sure why)

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 12:20 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/13 "2019-07-28T12:20:04Z")

</div>

Thanks. Really.

`awk`. I'll look at that.

Though I have got it working as @cymplecy said.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 July 2019 12:23 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/14 "2019-07-28T12:23:03Z")

</div>

> [@Trying\_to\_learn](#):
>
> But it had to be `-2` not `-1` . But no worries.

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`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 12:25 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/15 "2019-07-28T12:25:16Z")

</div>

All good.

Again: Thanks.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [28 July 2019 12:33 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/16 "2019-07-28T12:33:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 12:39 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/17 "2019-07-28T12:39:44Z")

</div>

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

```auto
pi@TelePi:/media/pi/9020-9C27 $ df -h /dev/sda1 | grep / | tr -s ' ' | cut -d " " -f4
1007M
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:

```auto
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 $ 

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 July 2019 12:44 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/18 "2019-07-28T12:44:32Z")

</div>

```auto
466Gi 295Gi 170Gi

```

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 13:03 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/19 "2019-07-28T13:03:17Z")

</div>

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

That would be `-1` yeah?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 July 2019 13:09 UTC](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773/20 "2019-07-28T13:09:17Z")

</div>

> [@bakman2](#):
>
> See the difference (-1) character or (-2)

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.

[Next page](https://discourse.nodered.org/t/help-with-how-to-get-info-on-usb-stick/13773.md?page=2)
