3

Pretty much as the title suggests, my colleague asserts that using a text editor instead of a parser (such as less) locks the file and in the case of logfiles makes it such that the logfile cannot be written to.

I have never heard of this before and just some initial testing finds this to not be the case. Additionally, I cannot find anything about this on a Google search to suggest where he might have got this idea from.

1
  • The described behavior is very common in Windows (you normally can't write to files opened for read --- try to recompile a LaTeX document while watching the output in Acrobat...). Linux/Unix normally do not do mandatory locking. Now, if something manages to write to a file from two programs simultaneously... anything can happen.
    – Rmano
    Sep 22, 2014 at 14:43

3 Answers 3

5

vim doesn't do this, but I'm not sure about the others. However, there's a very simple way to check this:

Open up two terminal windows (konsole, gnome-terminal, etc), and run the following commands in the first console:

touch ~/test.txt
vim ~/test.txt

Now, in your second console, type the following:

echo "This is a test" >> ~/test.txt

If the file is locked by vim, your command in the second console will fail, as the file cannot be written to. It won't fail. Repeat with whatever editor you want as step #2 in the first console.

Obviously, if you try and save any changes in your first console, you're wandering into uncharted territory, but as far as appending to log files go, this is safe.

Some editors, such as vim, gedit and others, create a "shadow" file when opening a file to indicate that the file is open in the editor. This is not locking, and does nothing to other applications on your system - it merely serves as a way for the editor to know that the file is open.

1
  • That's basically what I already did, I just wanted to run it past others to see if this perhaps holds any weight in history or such but not that I can find. Thanks!
    – kemra102
    Sep 22, 2014 at 9:48
1

vim and vi do (an example of the effect). They don't prevent files from being written to, but throw a warning if you use another instance of vi/vim to edit the same file. nano and emacs (v24.3.1) doesn't (at least by default). I'll have to look up other editors. In fact, most decent editors are actually capable of watching for external changes to the file, suggesting strongly that they can't prevent external changes.

This maybe true if the application uses some locking function (either using O_EXLOCK on systems which support it, or flock, or something else on Windows).

6
  • emacs does, nano is just way too simple.
    – Braiam
    Sep 22, 2014 at 15:32
  • @Braiam didn't happen to me. Does it need to be configured?
    – muru
    Sep 22, 2014 at 15:33
  • 1
    I just open a file, twice, and it ask if I want to recover the previous session and/or someone else is editing the file
    – Braiam
    Sep 22, 2014 at 15:34
  • @Braiam I just did the same thing at it opened quietly without any questions. Since I never use it I have never customized it.
    – muru
    Sep 22, 2014 at 15:40
  • @muru, Emacs (dating back to version 17) will attempt to lock files (for other Emacs users, at least), unless the create-lockfiles user option has been disabled. See also File Locks.
    – phils
    Jun 20, 2018 at 6:13
0

I think I understand your Issue. I was trying to work around same issue myself

If you create a document using the command:

sudo nano nanoCreated.py 

The document is created however the document is locked. It can only be edited by nano editor. This will be an issue when you try to edit it with other editors.

However I don't think other editors such as Vim have the same issue.

To work around the issue of locking files:

  1. Create a file using the command: touch nameOfFile.py or with any other extension. You are also welcome to use any other commands such as : cat>nameOfFile.py

  2. You can then access the document using: sudo nano nameOfFile.py

  3. You can then edit the document from there and after you save it, it won't be locked and can be edited used by other text editors
    If you look at the image I've uploaded. I created files nameOfFile and nanoCreated.py files using sudo nano nameOfFile.py and both files were locked as you can see the lock icon. Those two files can only be edited by Nano. However I created the other files using the steps I provided and none of them were locked

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .