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.
Seagate ST8000NM0055 8TB 7200RPM 256MB SATA 6.0 Gb/s 3.5" Enterprise Hard Drive
$44.53
WD 16TB Elements Desktop, Certified Refurbished Hard Drive - RWDBWLG0160HBK-NESN
$209.99
HDD 3.5" SATA Hard Drive with Windows 7/Win 10 Installed Legacy
$35.99
Seagate ST12000NM0127 12TB SATA 6Gb/s 256MB 7200RPM 3.5" Enterprise Hard Drive
$99.99
HGST Ultrastar DC HC520 12TB SATA 6Gb 256MB 3.5" Enterprise HDD- HUH721212ALE601
$89.99
1TB HDD/SSD 2.5" SATA Hard Drive for Laptop with Win 10/Win 11 Pro Pre-installed
$18.99
Seagate Exos X14 12TB SATA6Gb/s 7200RPM 3.5" Enterprise Hard Drive ST12000NM0558
$99.95
SEAGATE ST500LM021 500GB 7200RPM 2.5" SATA 6Gb/s 32MB Laptop Thin Hard Drive
$7.99
WD 4TB Certified Refurbished Elements, External Hard Drive - RWDBU6Y0040BBK-WESN
$87.49
Western Digital Gold Enterprise 4TB,7200 RPM Internal Hard Disk Drive WD4002FYYZ
$39.95