# NR 'write file' node \[storage\] Q / request

**URL:** <https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044>\
**Category:** General\
**Created:** [1 March 2024 02:00 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044 "2024-03-01T02:00:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [1 March 2024 02:00 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/1 "2024-03-01T02:00:01Z")

</div>

Is it possible to ask how, or if not currently included, ask to add, the ability to set this node with an option to write to top of file instead of always (only) appending to end? This would be very helpful with log files (that can get long).

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 March 2024 05:18 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/2 "2024-03-01T05:18:04Z")

</div>

Do you have examples of other logfiles to which data is prepended rather than appended?  
How do they handle multi-line log data?

I can't think of any examples, but maybe it's a thing in Windows or MAC?

On Linux I tackle large logfiles by using `tail -100 logfile` or `tail -f logfile` or even `vi logfile` followed by G to go to the last line.

---

<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:** [1 March 2024 06:52 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/3 "2024-03-01T06:52:40Z")

</div>

Appending will always be on the end as it is too expensive to insert it at the top (in terms of I/O, cpu).  
You can do a `tail` or use `tac` (reverse of `cat`).

eg,

```auto
# to get the the last 10 lines in the file
tac /somepath/file.txt | head -10 

# more efficient:
tail -10 /somepath/file.txt | tac

# or add linenumbers in front of each line and sort it in reverse
nl /somepath/file.txt | sort -nr

```

`tac` is installed by default on most linux distributions.

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [1 March 2024 17:23 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/4 "2024-03-01T17:23:34Z")

</div>

I guess I'm asking because I'd prefer to avoid creating a bunch of .sh files and running a bunch of bash exec nodes for all the logging (15 log files). Would rather use the NR node, since that's kinda the point of NR.

Besides, I didn't even ask the other favour ... to also include the ability to limit # of lines. lol

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 March 2024 17:45 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/5 "2024-03-01T17:45:21Z")

</div>

Well you could look to using this from a function node… [prepend-file - npm](https://www.npmjs.com/package/prepend-file) But yes you will def need to limit the number of lines.  
In which case if not too large you may be off better just holding an array of lines in memory. Unshifting to the front of that, then writing it all out , overwriting the existing file.

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [1 March 2024 18:09 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/6 "2024-03-01T18:09:00Z")

</div>

A worthy suggestion, I'll investigate.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 March 2024 19:06 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/7 "2024-03-01T19:06:01Z")

</div>

I don't understand where exec nodes come in, are you intending to view/analyse the log files within Node-red, on a dashboard maybem or is this to generate the log files?

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [1 March 2024 19:23 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/8 "2024-03-01T19:23:53Z")

</div>

No I'm not. The suggestion was to use Linux commands by having the processes run an exec node do the save each time (instead of a NR node) which infers needing to implement those.

---

<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:** [1 March 2024 21:18 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/9 "2024-03-01T21:18:21Z")

</div>

Well you don't have to create 'a bunch of bash files', it can be run directly in the exec node and you can create the command via node-red as well and list all 15 logfiles directly in one go.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 March 2024 22:10 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/10 "2024-03-01T22:10:46Z")

</div>

Unless I am missing something, @bakman2's suggestions and the commands I listed allow you to inspect the bottom of a logfile, not to build a logfile by adding lines at the top.

Can you explain what method you use to view logfiles that would be easier if the newest lines were at the top?

Here is a very inelegant work-around to maintain a second, reversed copy of each logfile:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58f9192d2a444a0cf813998596a555cbe71203c2.png)

```auto
[{"id":"b50d0e96ebb14c83","type":"inject","z":"faa13ab8c3e03edc","name":"Write something to log file","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":360,"wires":[["22ce53081d0ffdaf"]]},{"id":"22ce53081d0ffdaf","type":"file","z":"faa13ab8c3e03edc","name":"","filename":"/home/pi/logtwo.gol","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":370,"y":360,"wires":[[]]},{"id":"7fb54d6e4ee3a26a","type":"exec","z":"faa13ab8c3e03edc","command":" ","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"Do it","x":650,"y":420,"wires":[[],[],[]]},{"id":"f9db08676855add5","type":"complete","z":"faa13ab8c3e03edc","name":"","scope":["22ce53081d0ffdaf","9db35fa02d666815"],"uncaught":false,"x":90,"y":420,"wires":[["0223575f56b0f3c6"]]},{"id":"24079cc28b05f9bc","type":"inject","z":"faa13ab8c3e03edc","name":"Write something to log file","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":320,"wires":[["9db35fa02d666815"]]},{"id":"9db35fa02d666815","type":"file","z":"faa13ab8c3e03edc","name":"","filename":"/home/pi/logone.gol","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":370,"y":320,"wires":[[]]},{"id":"0223575f56b0f3c6","type":"template","z":"faa13ab8c3e03edc","name":"tac {{{filename}}} > $(basename {{{filename}}} .gol).log","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"tac {{{filename}}} > $(basename {{{filename}}} .gol).log","output":"str","x":380,"y":420,"wires":[["7fb54d6e4ee3a26a"]]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 May 2024 22:10 UTC](https://discourse.nodered.org/t/nr-write-file-node-storage-q-request/86044/11 "2024-05-30T22:10:55Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
