32 views (last 30 days)
Show older comments
Scott on 30 Jun 2015
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory
Commented: Scott on 1 Jul 2015
I have scripts that create a temporary directory to store intermediate files created during processing. At the end of the scripts I want to clean this temporary directory out and remove the directory. I can remove the files that I create, but when I run the rmdir() command, it will usually fail with the message "MATLAB:RMDIR:SomeDirectoriesNotRemoved". If I go into the file system and do a "ls -al" command, I will see that there is a Matlab lock file or something in the directory. It will go away if I shut down Matlab. Unfortunately it's not easy to recreate, because I've tried creating a directory from the command line, adding, modifying and removing a few files, and then removing the directory and it always seems to work fine. Something about running my scripts as a whole is causing Matlab to lock the temporary directory, and I don't know a way to unlock it.
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Scott on 30 Jun 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory#comment_295617
The actual command I'm using is rmdir(<directory>, 's') to remove everything in the temporary directory. I just did it again, and paused the program before it executed the rmdir command. I did "ls -al" and there were no hidden files, just some text files and subdirectories. I then stepped to execute the rmdir command, and it removed everything in the directory, but did not remove the temporary directory. I did "ls -al" again, and suddenly there was a .nfs* file in the directory.
Walter Roberson on 30 Jun 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory#comment_295634
⋮
<<If a file delete request is sent to a file on an NFS server while an NFS client still has the file open, NFS will create a file with the ".nfsX" extension, where "X" can be any number of characters. This allows the NFS server to maintain the file until all dependent processes on the file have been terminated.>>
This suggests that there is an open file in the directory. Perhaps something you did not fclose() ?
I think the inode of the .nfs* file should be the same as that of the file that is thought to be open, so if you record the ls -i before the rmdir and then look at the ls -i of the .nfs* file, I suspect you should find a correspondence.
Scott on 1 Jul 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory#comment_295872
That was it. I found a function call that didn't fclose its file ID, and the inode helped me to quickly figure out which one it was. Thank you!
Sign in to comment.
Sign in to answer this question.
Answers (2)
Guillaume on 1 Jul 2015
Open in MATLAB Online
Are all handles to files in the temp directory closed?
Does
fclose('all')
help?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Walter Roberson on 30 Jun 2015
Make sure that you cd out of the directory before removing it.
4 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Scott on 30 Jun 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory#comment_295616
I do cd to the directory that contains the temp directory I want to remove, so that's not the problem.
Walter Roberson on 30 Jun 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory#comment_295618
Edited: Walter Roberson on 30 Jun 2015
Does the lock go away if you "clear all" ? What is the lock file name?
Image Analyst on 30 Jun 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory#comment_295621
"I do cd to the directory" - that's exactly what he said not to do. That probably IS the problem. For one thing, there's no need to cd to any directory - I almost never do. You can use fullfile() and save things to the folder using the full file name without actually having the folder be the current directory. So, if you did cd into it, then cd out of it before you call rmdir().
If that still does not work, you can find out what has a hold on it with the shareware program called "unlocker" (Google it). It will pry off whatever program's grubby hands are on it.
Walter Roberson on 30 Jun 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/225959-how-to-get-matlab-to-unlock-or-release-a-directory#comment_295632
Edited: Walter Roberson on 30 Jun 2015
Scott cd'd to the containing directory, not the directory itself.
unlocker appears to be an MS Windows program, but "ls -al" is a Unix command so unlocker would not be applicable.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABProgrammingFiles and FoldersSearch Path
Find more on Search Path in Help Center and File Exchange
Tags
- lock file
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office