How to Lock a File for Renaming/Deleting in Linux

If you’ve ever worked with sensitive files on Linux, you might have wanted to prevent others (or even yourself) from accidentally renaming or deleting them. Thankfully, Linux provides a few methods to “lock” a file, making sure it stays safe from unwanted changes.

In this guide, we’ll show you how to lock a file to prevent renaming or deleting it using simple commands and tools available in Linux. We’ll also walk through an example to demonstrate each method.

Let’s assume we have a file called important.txt located in the /home/user/ directory, and we want to protect this file from being renamed or deleted.

Method 1: Using chattr to Make a File Immutable

One of the simplest and most effective ways to protect a file from renaming or deletion is to use the chattr command, which changes file attributes in Linux.

First, let’s check the attributes of important.txt using the lsattr command, which will list the attributes of files and directories:

lsattr /home/user/important.txt

If the file is not locked, you should see nothing or just - in the output.

Check File Attributes
Check File Attributes

To make important.txt immutable (unable to be renamed or deleted), run the following command:

sudo chattr +i /home/user/important.txt
lsattr /home/user/important.txt

Now, you should see an i in the output next to the file name, indicating it’s locked.

Lock File in Linux
Lock File in Linux

Attempting to rename or delete the file will now fail.

mv /home/user/important.txt /home/user/important_backup.txt

Similarly, if you try to delete the file.

rm /home/user/important.txt

You will get an error saying “Operation not permitted“.

Delete File in Linux
Delete File in Linux

To remove the immutability and allow changes to the file, use:

sudo chattr -i /home/user/important.txt

Now, you can rename or delete the file as usual.

Method 2: Using File Permissions to Restrict Deleting

Another way to prevent file deletion is by changing the file’s permissions by using the chmod command, which sets permissions that make a file unreadable or uneditable by other users.

To prevent everyone (including yourself) from deleting or modifying the file, use:

chmod a-w /home/user/important.txt

You can check the file’s permissions with:

ls -l /home/user/important.txt

You should see something like this, where the w (write) permission has been removed, which indicates that no one can modify or delete the file.

File Deletion Control with Permissions
File Deletion Control with Permissions

To allow yourself to delete or modify the file again:

chmod +w /home/user/important.txt

Method 3: Using chown to Change Ownership

If you’re the only person who should be able to modify or delete the file, you can change the ownership of the file.

sudo chown yourusername:yourgroup /home/user/important.txt

Replace yourusername and yourgroup with your actual username and group.

Now, you can check the file’s owner and group with:

ls -l /home/user/important.txt

You should see something like:

-r--r--r-- 1 yourusername yourgroup 0 Feb 3 10:00 /home/user/important.txt

Now, only you can modify or delete the file.

Conclusion

Locking a file in Linux can help prevent accidental changes like renaming or deletion, which is especially useful when dealing with important files.

The methods we’ve discussed – using chattr to make a file immutable, adjusting file permissions, and changing file ownership are easy to use and can provide the security you need.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

2 Comments

Leave a Reply
  1. “chmod a-w file” will NOT prevent it from being removed! The file can be removed if the user has write permission on the directory. You will/might see a warning asking if you wish to remove a write-protected file

    touch A
    chmod a-w A
    ls -l A
    
    -r--r--r-- 1 psamuel psamuel 0 Feb 12 15:09 A
    
    rm A
    rm: remove write-protected regular empty file 'A'? y   # user types 'y' followed by NEWLINE
    ls -l A
    /bin/ls: cannot access 'A': No such file or directory
    
    Reply
    • @Peter,

      You’re absolutely right! Removing write permissions from a file (chmod a-w file) does not prevent it from being deleted if the user has write permission on the parent directory.

      To truly prevent deletion, the best approach is:

      1. Make the directory immutable, which will prevents any modifications, including file deletions inside the directory.

      chattr +i directory/
      

      2. Modify directory permissions.

      chmod a-w directory/
      

      This removes write access, preventing users from deleting files.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.