You can use 'lsof' to do it and probably that's the same scheme which is used by Puppy.
But you have to know that it is still not 100% safe. Because a file system allows concurrent access, at one moment your check says the file is not opened, but as soon as you start the copying and deleting the copy on the ramdisk, another process starts opening it for modification or deletion. You can imagine it will result in various confusion to your file system.
Intel Core i7-7700 3.60GHz Quad-Core CPU
$41.89
Intel - Core i7-14700K 14th Gen 20-Core 28-Thread - 4.3GHz (5.6GHz Turbo) Soc...
$399.99
Intel Core i7-3770 - Quad Core 3.40GHz CPU Processor
$27.45
Intel - Core i5-14600K 14th Gen 14-Core 20-Thread - 4.0GHz (5.3GHz Turbo) Soc...
$306.99
Intel Xeon E5-2680 v4 SR2N7 14 Core 2.4 GHz Server Processor
$15.99
** AMD Ryzen 3 3100 CPU Processor - USED **
$38.99
AMD Ryzen 7 7700 Processor (5.3 GHz, 8 Cores, Socket AM5) Boxed -...
$180.00
AMD Ryzen 5 4500 6-Core 3.6GHz Socket AM4 65W CPU Desktop Processor
$79.00
Intel Core i9-9900K Processor LGA1151 CPU
$259.00
Intel Xeon E5-2697 v2 2.7GHz 30M 12-Core LGA2011 CPU Processor SR19H
$34.99