Comments on: How to Lock a File for Renaming/Deleting in Linux https://www.tecmint.com/prevent-file-deletion-linux/ Tecmint - Linux Howtos, Tutorials, Guides, News, Tips and Tricks. Thu, 13 Feb 2025 04:08:13 +0000 hourly 1 By: Ravi Saive https://www.tecmint.com/prevent-file-deletion-linux/comment-page-1/#comment-2270639 Thu, 13 Feb 2025 04:08:13 +0000 https://www.tecmint.com/?p=59543#comment-2270639 In reply to Peter Samuel.

@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.

]]>
By: Peter Samuel https://www.tecmint.com/prevent-file-deletion-linux/comment-page-1/#comment-2270540 Wed, 12 Feb 2025 20:17:21 +0000 https://www.tecmint.com/?p=59543#comment-2270540 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
]]>