I am trying to delete a folder which consists of 8 folders which in turn consists of few .tiff images. Before I delete these images recursively using "rm -rf" command, I would like to know whether these files are being used by some other processes or typically perl and python programs specifically.
I have a simple solution in my mind to check whether these files are used by other process. I will try to rename this folder. If it allows, then these files are used by other process and hence I can delete these folders recursively. If it doesn't allow me change the name, then the .tiff files are being used by some other process and hence I can exit without deleting the folders.
Please let me know whether this approach is correct or any other graceful way of checking whether the folders are being used by other process.
Satish is correct. Linux is much less tenacious in file locking than Windows is.
However, Murphy's Law says that in the few microseconds between the time you do the check and the time you delete, someone will manage to come in on another process and grab the file, so a simple "lsof/rm" isn't reliable.
Depending on context, it may or may not be a problem if you remove a file from the filesystem while another app is using it, since the actual file contents aren't deleted, just the directory linkage - the contents don't formally disappear until the user has closed the file.
However, if that's a problem, you need to add some sort of monitoring construct that allows the files to be locked and unlocked and add it to both the apps that use it and to the app that deletes them. The monitor would maintain a semaphore to serialize access.
A variant approach in modern-day Linux might be to create a userspace filesystem that fronts the actual files in question and does all that work for you. That way you would require minimal-to-no coding changes to the apps.
Please don't ask me for examples. It's more work that I can afford to do for free - especially in today's economy. But it's doable with a moderate amount of work.
An IDE is no substitute for an Intelligent Developer.
If you can afford to re-code the scripts slightly, a separate file lock might help you? In Bash you'd be looking at something like:A similar approach in the Perl (or whatever) scripts will tell this external script when the directory is in use via the LOCK_FILE and cause it not to remove files. It doesn't however provide for concurrent access from multiple simultaneously executing Perl scripts, nor provide any indication of how many scripts are currently using the directory. For that you'd need some sort of counter, or possibly a directory containing lock files with randomly generated names (one for each Perl script execution).
It will almost always work, except if the processor allows execution of the same lines in your foreign script between the if and touch lines, which would be most unfortunate (and highly improbably).
That just came from the top of my head---it might not be the best solution to your problem. Does it help at all? [ September 29, 2008: Message edited by: Charles Lyons ]
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
Joined: Mar 27, 2003
And after I pressed submit, I realised you can use Linux native file locks, which are atomic operations so will be immune to multi-threading. You'll need to use flock---e.g. see here for shell command and here for Perl. That should save you some work and in theory be better too...
Joined: Jan 13, 2008
Thanks everyone for your help and will start my home work. Thanks.
Maybe you could use a simple linux command to see which files are in use by which process. for example if you put the parent folder name in the following command you will find which files inside it or its child are in use by some process.
I have a java program that looks for files in a folder on a linux machine. The files in the folder are being ftp’ed. I have a requirement of not processing these files till the time they are being written to that folder. I tried using FileChannel and locking but linux is much less tenacious as far as locks are concerned.
Is there a way of knowing that the file is still being written/copied to the location through Java on linux systems?