Hello, I’m Liang Xu.
Files are our most precious treasure in the computer, we often work for a long time, and then come out the result is just a file. Programmers in particular, we write code for hours, and the result is just code files.
But we’ve all been there — working for hours only to accidentally delete a file…
What’s your mood like at this point? The pain? Remorse? Helpless?
Today good xu to introduce a command, it will give your important documents add a lock, to prevent misdeletion or mismodification, for your baby file escort!
Introduction to the chattr command
One command we need to use here is chattr, which is available in most Linux distributions, so we won’t go into details about its installation. Its basic usage is as follows:
$chattr operator property file nameCopy the code
There are three types of operators:
+
: Adds attributes to the file-
: Removes file attributes=
: Sets the only properties of the file
Note that these properties are not the system properties of the file, but the properties that Chattr assigns to the file. The following two properties are covered in this article:
a
– Allows you to append content to filesi
– Protected mode (cannot be deleted or modified)
However, its properties can be set a lot, interested partners can go to see its MAN manual.
$ man chattrCopy the code
Prevent files from being deleted or modified by mistake
Let’s say we have a national treasure file called file.txt, and now we use chattr to protect it. Here, we add the + I attribute to the file:
$ sudo chattr +i file.txtCopy the code
We can then view its properties using the lsattr command. Similar output is as follows:
$ lsattr file.txt
----i---------e---- file.txtCopy the code
Now, let’s try to delete that file manually:
$ rm file.txt
rm: cannot remove 'file.txt': Operation not permittedCopy the code
Yi? Not allowed to delete? Don’t you have enough authority?
Well, I’ll sudo it!
$ sudo rm file.txt
rm: cannot remove 'file.txt': Operation not permittedCopy the code
I still can’t delete my X?
Let’s try it again, modify the contents of the file.
$ echo 'hello world! ' >> file.txt bash: file.txt: Operation not permittedCopy the code
As you can see, you still can’t modify the file.
Also, even if you manually delete the file from a GUI interface, it still won’t work.
So, as you can see, the file is now well protected from deletion (by any means) or modification.
So how do we remove this protection? It’s easy, just add the -i option.
$ sudo chattr -i file.txtCopy the code
Now, the files are back as they were, and we can modify them and delete them if we want.
$ echo 'Hello World! ' >> file.txt $ cat file.txt Hello World! $ rm file.txtCopy the code
Prevent folders from being deleted or modified by mistake
The above is to protect files, so how to protect folders?
It’s the same thing. Use the + I option. Suppose we now have a directory dir1 with a file.txt file in it. Let’s protect this folder.
$ sudo chattr -R +i dir1Copy the code
Here, we use the -r option to indicate that we can recursively apply to all files in the directory (including subdirectories).
Now, let’s also test if it can be deleted or modified.
$ rm -rf dir1 $ sudo rm -rf dir1 rm: cannot remove 'dir1/file.txt': Operation not permitted $ echo 'hello world! ' >> dir1/file.txt bash: file.txt: Operation not permittedCopy the code
So as with the files, we successfully secured the folder.
Prevents files/directories from being deleted, but allows content to be appended
Now we know how to prevent files/directories from being deleted or modified by mistake, but what if we don’t want the existing contents of the file to be modified, but allow others to append content to the end of the file?
This is where we need to use the +a option.
The file:
$ sudo chattr +a file.txtCopy the code
The directory:
$ sudo chattr -R +a dir1Copy the code
Now let’s verify that the file can be appended.
$ echo 'Hello World! ' >> file.txt $ echo 'Hello World! ' >> dir1/file.txtCopy the code
Let’s use the cat command again to check the contents:
$ cat file.txt
Hello World!
$ cat dir1/file.txt
Hello World!Copy the code
As you can see, files can be appended.
However, file. TXT and dir1/file. TXT still cannot be deleted.
If you want to remove apacheable attributes, use the -a option.
The file:
$ sudo chattr -R -a file.txtCopy the code
The directory:
$ sudo chattr -R -a dir1/Copy the code
Finally, recently, many friends asked me for Linux learning roadmap, so I stayed up for a month in my spare time according to my own experience, and sorted out an e-book. Whether you are interviewing or self-improvement, I believe will help you! The directory is as follows:
Free to everyone, just ask everyone to point to me!
Ebook | Linux development learning roadmap
Also hope to have a small partner can join me, do this e-book more perfect!
Have a harvest? Hope the old iron people come to a triple whammy, give more people to see this article
Recommended reading:
- Dry goods | programmers advanced architect necessary resources free of charge
- Artifact | support resource site search