Catching a transitory file in the act
Trying to debug a printing problem on my Linux box, I found myself needing to read a file that was so temporary that it would be deleted automatically within a few milliseconds after its creation.
The file in question was created in /tmp, which being mounted tmpfs meant that file recovery was not an easy option. However the fil did have a predictable component to its name: it always started with “PrtDrv-”. This allowed me to just poll for it using BASH!
sudo echo Scanning... && while true; do if [ -e /tmp/PrtDrv-* ]; then sudo cat /tmp/PrtDrv-* | tee $(basename "/tmp/PrtDrv-*"); break; fi; done
Theoretically it might mean that I might miss the file as I’m setting up a race condition, but in practice it seems this loop is fast enough that I had to actually add the break command keep it from reading out the file multiple times.
I might break down the above command in the future, but for any decently advanced BASH scripter it should be fairly obvious.