File management is a complicated process in Linux as it is a multi-user operating system. The administrators can change the attributes of a file using the “chattr” command so it cannot be accessed and changed by anyone except the superuser. This saves the important files from accidental deletion. In this write-up, we will focus on how to modify the attributes of a file by using the “chattr” command. We will also learn about different flags that can be used along with the “chattr” command. But first, let’s discuss the syntax of the “chattr” command:

Flags

Here is a list of the most common flags and attributes:

‘a’ With this attribute a file can only be opened in append mode. ‘i’ To make a file immutable ‘S’ Files with this attribute are synchronously updated on the disk ‘u’ To save contents of a file when it is deleted ’t’ To restrict tail merging ‘j’ The data of files with this attribute is updated to ext3 journal before the file itself

Operators

‘+’ This operator is used to add additional attributes. ‘-’ This operator is used to remove attributes of a file. ‘=’ This operator is used to make the specified attributes, the only attributes of the file.

How to use ‘i’ attribute to make a file immutable

The “chattr” command is often used to make files immutable. Immutable means that the file cannot be moved, renamed, or deleted. Here we will give the ‘i’ flag to a file named “test-file.txt” as an example:

You can use the “lsattr” to check the file’s attributes. As you can see in the screenshot above the ‘i’ attribute has been set and the file has become immutable. The ‘i’ attribute can also be used to make directories immutable.

How to remove the ‘i’ attribute from the file

Once the ‘i’ attribute has been set the file can only be changed or deleted once the attribute is removed by the root user. Use the ‘-’ operator with the option to remove the attribute:

How to use the ‘a’ attribute to open file in append mode

We can use the ‘a’ attribute to open the file in the append mode. In append mode, users can only append Data on a file without changing the data that is already present in the file.

Now, as you can see in the screenshot below when I try to add more data into the text file by using the echo command the terminal gives me an error:

But we can append data into the file by using “»” instead of “>” operator:

How to add ‘j’ attribute to update data of the file to ext3 journal

By using the ‘j’ attribute, the data of the files attribute will be updated to the ext3 journal before the file itself:

Conclusion

The “chattr” command is a very useful tool for administrators. It enables them to modify file permissions which helps in the protection of important files and prevents them from being altered. In this write-up, we discussed what the ‘chattr’ command is and how to use it. Moreover, we also discussed some important flags that are used along with the ‘chattr’ command.

Chattr Command in Linux with Examples   TecAdmin - 49Chattr Command in Linux with Examples   TecAdmin - 22Chattr Command in Linux with Examples   TecAdmin - 71Chattr Command in Linux with Examples   TecAdmin - 66Chattr Command in Linux with Examples   TecAdmin - 92Chattr Command in Linux with Examples   TecAdmin - 81