Raspberry Pi can’t open a file for writing lock file error

Many times, it happens while using raspberry pi that we’re not able to edit some particular files. For me it has happened with

  • Motion.conf file, which is used to configure the video streaming service called motion on raspberry pi
  • Rc.local, this file resides in /etc and is used for adding commands to run programs on boot

What happens sometimes, is due to wrong file closure, or opening/closing while the file is being used, the file gets locked in the memory of the operating system and we just need to remove the residual stuffs which is causing this file to remain locked.

How to identify the problem? Whenever you try to open this locked file, say for example using a text editor like nano, you’ll get below error

Now to resolve this issue, please follow below steps

Go to the exact location in your terminal, where your expected file is residing for opening, in my case, it was /etc/motion/ here I had motion.conf file which I needed editing but I was getting above nerror

Now use this command to delete the swap file that is created, as long as the swap is there, you can’t edit the original file, so the command is

sudo rm .Filename.extention.swp, please mind the initial dot (.) is compulsory, so in my case, the command was

sudo rm .motion.conf.swp and hit enter

now try opening that file using your usual editor like nano, and the file will open normally now

sudo nano motion.conf

5 Responses

  1. Hello. This post was extremely interesting, especially because I was looking for thoughts on this topic
    last Thursday.
    Best regards,
    Balle Raahauge

  2. Thanks alot that what exactly happened with me , the raspberry pi was turned off suddenly and the file didn’t open i did your method and worked .
    Thanks alot for sharing your experience

  3. Thank you.
    sudo rm .config.txt.swp

    Before that ,cannot open /boot/config.txt

    after sudo rm .filename.ext.swp, can edit.

Leave a Reply

Your email address will not be published. Required fields are marked *

Tutorials and Posts you may be interested in...

How to use bluetooth with 8051

This Project is Suitable for Everyone Including student and Professional Bluetooth-based relay control using an 8051-based microcontroller involves using Bluetooth communication to remotely control the switching of a relay. Here’s

Read More »

How to use Raspberry Pi Pico W with Optocoupler

Optocouplers, also known as opto-isolators, are electronic components that provide electrical isolation between two circuits by using light to transfer signals. Optocouplers are commonly used to protect sensitive electronic devices

Read More »