Home Command line utilities Prevent Files And Folders From Accidental Deletion Or Modification In Linux

Prevent Files And Folders From Accidental Deletion Or Modification In Linux

By sk
Published: Updated: 34.6K views

Some times, I don't think straight and accidentally "SHIFT+DELETE" my data. To make things even worse, I don't even backup the data before deleting them. If you're anything like me, here is a good news for you. I know an useful commandline utility called "chattr" that is used to prevent files and folders from accidental deletion or modification in Linux.

What is Chattr?

Chattr, short for Change Attribute, applies/removes certain attributes to a file or folder in your Linux system. So nobody can delete or modify the files and folders either accidentally or intentionally, even as root user. Sounds useful, isn't it? Indeed!

Prevent Files and Folders from Accidental Deletion or Modification in Linux Using Chattr

By default, Chattr comes pre-installed in many Linux operating systems. So let us not bother with installation.

The default syntax of chattr command is:

chattr [operator] [switch] [filename]

chattr has the following operators:

  • The operator '+' causes the selected attributes to be added to the existing attributes of the files.
  • The operator '-' causes them to be removed.
  • The operator '=' causes them to be the only attributes that the files have.

Chattr has different attributes namely - aAcCdDeijsStTu. Each letter applies a particular attribute to a file as listed below.

  • a - append only,
  • A - no atime updates,
  • c - compressed,
  • C - no copy on write,
  • d - no dump,
  • D - synchronous directory updates,
  • e - extent format,
  • i - immutable,
  • j - data journalling,
  • P - project hierarchy,
  • s - secure deletion,
  • S - synchronous updates,
  • t - no tail-merging,
  • T - top of directory hierarchy,
  • u - undeletable.

In this tutorial, we are going to discuss the usage of two attributes, namely a and i which are used to prevent the deletion of files and folders.

Prevent files from accidental deletion in Linux

Let me create a file called file.txt in my current directory.

$ touch file.txt

Or,

$ > file.txt

Now, I am going to apply "i" attribute which makes the file immutable. Meaning - you can't delete, modify the file, even if you're the file owner and the root user.

$ sudo chattr +i file.txt

You can check the file attributes using command:

$ lsattr file.txt

Sample output:

----i---------e---- file.txt
Make files immutable using chattr command in Linux
Make files immutable using chattr command in Linux

Now, try to remove the file either as a normal user or with sudo privileges.

$ rm file.txt

Sample output:

rm: cannot remove 'file.txt': Operation not permitted

Let me try with sudo command:

$ sudo rm file.txt

Sample output:

rm: cannot remove 'file.txt': Operation not permitted

Let us try to append some contents in the text file.

$ echo 'Hello World!' >> file.txt

Sample output:

bash: file.txt: Operation not permitted
Prevent files from accidental deletion in Linux Using Chattr
Prevent files from accidental deletion in Linux Using Chattr

Even if you try remove the file from your file manager in GUI mode, you can't delete it.

Prevent files from removal in Linux
Prevent files from removal in Linux

As you noticed in the above outputs, We can't delete or modify the file even as root user.

Revoke attributes

To revoke attributes, just use "-i" switch as shown below.

$ sudo chattr -i file.txt

Now, the immutable attribute has been removed. You can now modify or delete the file as you wish.

$ echo 'Hello World!' >> file.txt
$ cat file.txt
Hello World!
$ rm file.txt
Remove immutable file attributes using chattr
Remove immutable file attributes using chattr

Similarly, you can restrict the directories from accidental deletion or modification as described in the next section.

Prevent folders from accidental deletion and modification in Linux

Create a directory called dir1 and a file called file.txt inside this directory.

$ mkdir dir1 && touch dir1/file.txt

Now, make this directory and its contents (file.txt) immutable using command:

$ sudo chattr -R +i dir1

Where,

  • -R - will make the dir1 and its contents immutable recursively.
  • +i - makes the directory immutable.

Now, try to delete the directory either as normal user or using sudo user.

$ rm -fr dir1
$ sudo rm -fr dir1

You will get the following output:

rm: cannot remove 'dir1/file.txt': Operation not permitted

Try to append some contents in the file using "echo" command. Did you make it? Of course, you couldn't!

Prevent folders from accidental deletion and modification in Linux
Prevent folders from accidental deletion and modification in Linux

Remove attributes

To revoke the attributes back, run:

$ sudo chattr -R -i dir1

Now, you can delete or modify the contents of this directory as usual.

Prevent files and folders from accidental deletion, but allow append operation in Linux

We know now how to prevent files and folders from accidental deletion and modification. Next, we are going to prevent files and folders from deletion, but allow the file for writing in append mode only. That means you can't edit, modify the existing data in the file, rename the file, and delete the file. You can only open the file for writing in append mode.

To set append mode attribution to a file/directory, we do the following:

For files:

$ sudo chattr +a file.txt

For directories: 

$ sudo chattr -R +a dir1

A file/folder with the 'a' attribute set can only be open in append mode for writing.

Add some contents to the file(s) to check whether it works or not.

$ echo 'Hello World!' >> file.txt
$ echo 'Hello World!' >> dir1/file.txt

Check the file contents using cat command:

$ cat file.txt
$ cat dir1/file.txt

Sample output:

Hello World!
Allow append operation in Linux using chattr command
Allow append operation in Linux using chattr command

As you can see, we can be able able to append the contents. It means we can modify the files and folders.

Let us try to delete the file or folder now.

$ rm file.txt

Output:

rm: cannot remove 'file.txt': Operation not permitted

Let us try to delete the folder:

$ rm -fr dir1/

Or try with sudo:

$ sudo rm -fr dir1/

Sample output:

rm: cannot remove 'dir1/file.txt': Operation not permitted
Prevent files and folders from accidental deletion, but allow append operation in Linux
Prevent files and folders from accidental deletion, but allow append operation in Linux

Remove attributes

To remove the attributes, run the following commands:

For files:

$ sudo chattr -R -a file.txt

For directories: 

$ sudo chattr -R -a dir1/

Now, you can delete or modify the files and folders as usual.

For more details, refer the man pages.

$ man chattr

Wrapping up

Data backup and protection is one of the main job of a Linux Sysadmin. There are numerous free and commercial data protection software are available on the market. Luckily, we've got this built-in tool which helps us to protect the data from accidental deletion or modification using Chattr. Chattr can be used as additional tool to protect the important system files and data in your Linux system.

You May Also Like

11 comments

David Okwii February 9, 2017 - 1:47 pm

Wow, I didn’t know about this feature. Only knew about setting file/directory permissions. Thanks for the tutorial.

Reply
Philip Rhoades May 17, 2022 - 11:03 pm

Ah . . this ALMOST does what I want – I need to be able to mv the dir or file eg:

mv dir1 dir2
mv dir/t1 dir/t2

the only way I can see to do these things is with a script that turns the chattr off, do the mv and then do another chattr . .

Thanks!

Reply
Bishwajit Samanta March 26, 2017 - 8:44 am

Wowwwwwww awesome feature

Reply
Lee Kierstead May 1, 2019 - 2:17 am

Thank you. I was looking for a way to prevent accidental deletion of precious photos and other files. This is useful info for Linux users, not just system admins. This works perfectly for my needs.

Reply
kakhaber tsitaishvili February 6, 2020 - 12:17 am

thank you it is very useful

Reply
CatLord March 7, 2020 - 5:04 am

Nice job, this appears to be really useful !

Reply
Ram Sambamurthy May 18, 2021 - 1:58 pm

Thanks. Didn’t know these existed.
Question: How can i prevent deletion of the file but allow the file to be moved to a different location? Is this possible?

Reply
sk May 18, 2021 - 4:12 pm

Did you try to move the file? I think we can move the file after applying the immutable attributes.

Reply
ali moradzade December 21, 2021 - 10:55 am

Very useful, Thank you 🙂

Reply
A January 1, 2023 - 4:19 pm

Can it be done for a specific user?

Reply
sk January 2, 2023 - 10:48 am

As far as I know, it is not possible by chattr utility.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More