0

I'm using a Raspberry Pi to collect data (from USB-serial converter) and I'd like the recording files to be numbered in ascending order, to make it easy to find out which one is the latest when browsing the filesystem with SFTP.

Normally, I'd accomplish that by using the date and time in the filename. But Raspberry Pi has no non-volatile calendar. And using the fake-hwclock date would confuse people.

What's a simple way to create filenames in sequentially increasing order, across multiple boots?

EDIT: In case I was unclear, the network isn't present during data collection, only during data download. And there are expected to be multiple reboots before the network is available again. (Plus my university blocks NTP!, so getting date is non-trivial even when connected. But that's a completely separate problem.)

I haven't completed the filesaving script, but it's going to be along the lines of

stty -F /dev/mydevice 9600
cat /dev/mydevice > /data/recorded-N.txt

Does raspbian use systemd? I got quite annoyed on another linux system trying to start a data collection script on hotplug using udev, because udev killed the script, even when using nohup, etc. The workaround was to make udev start a systemd service.

  • What is wrong with dates? I've had my Raspberry taking daily backups of 3 servers for months and it has never failed. All the filenames are prepended by a date in the YYYYMMDDhhmmss format – foibs Nov 22 '13 at 21:59
  • @foibs: Using dates would be fine, if there was any way to know the date. – Ben Voigt Nov 22 '13 at 22:20
  • I can think of several solutions; e.g. saving in an environment variable. It depends how you are getting the data into the file. – Milliways Nov 22 '13 at 22:28
  • @BenVoigt: saving a counter into a file could probably do your job then. If you provide a sample of a file saving script (only the part that creates the filename), we'll make it use an incremental counter in the filename – foibs Nov 22 '13 at 22:32
  • @foibs: I wonder if the best approach would be to use bash globbing to detect the existing names, pull out the numeric part somehow, add 1, and use that. – Ben Voigt Nov 22 '13 at 22:38
1

As you suggested, there are more than one ways to do that.

The file way, where the counter is stored in a file:

#!/bin/bash
N=1
if [ -f ~/counter ]; then
N=$((`cat ~/counter`+1))
else
touch ~/counter
fi
echo $N > ~/counter

stty -F /dev/mydevice 9600
cat /dev/mydevice > /data/recorded-$N.txt

And the glob way, where the counter is read from the already existing files

#!/bin/bash
N=(`ls /data/recorded-*.txt | grep -o '[0-9]*' | sort -nr`)
N=$(($N+1))

stty -F /dev/mydevice 9600
cat /dev/mydevice > /data/recorded-$N.txt

The file way is faster (it doesn't need to read the whole directory), plus you can edit the file anytime. The glob way is easier. Choose whichever you find more suitable. You can find a dozen more ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.