Linux chattr Command Tutorial for Beginners (5 Examples)

Linux chattr Command Tutorial for Beginners (5 Examples)

In the world of Linux, there are a plethora of commands that are available to users to manage files and directories. One such command is chattr, which stands for “change file attributes.” This command is used to change the file attributes of a file or directory in Linux. In this tutorial, we will go over the basics of using the chattr command and provide five examples to help beginners understand its usage.

1. Viewing file attributes

Before we can change the attributes of a file or directory, it is important to know what attributes are currently set. To view the attributes of a file, use the following command:

chattr +i filename

This will display the current attributes of the file. In this example, the “+i” option is used to view the attributes of the file named “filename.”

2. Making a file immutable

One of the most common uses of the chattr command is to make a file immutable, meaning it cannot be modified, deleted, or renamed. To make a file immutable, use the following command:

chattr +i filename

This will set the immutable attribute on the file named “filename.” Once this attribute is set, the file cannot be changed unless the attribute is removed.

3. Removing the immutable attribute

To remove the immutable attribute from a file, use the following command:

chattr -i filename

This will remove the immutable attribute from the file named “filename,” allowing it to be modified, deleted, or renamed once again.

4. Setting append-only attribute

Another useful attribute that can be set using the chattr command is the append-only attribute. This attribute prevents users from overwriting the contents of a file, allowing only new data to be added to the end of the file. To set the append-only attribute on a file, use the following command:

chattr +a filename

This will set the append-only attribute on the file named “filename.”

5. Viewing attributes of all files in a directory

To view the attributes of all files in a directory, use the following command:

lsattr

This will list the attributes of all files in the current directory. If you want to view the attributes of a specific directory, use the following command:

lsattr /path/to/directory

This will display the attributes of all files in the specified directory.

In conclusion, the chattr command in Linux is a useful tool for managing file attributes. By using the examples provided in this tutorial, beginners can gain a better understanding of how to use the chattr command to protect their files from unwanted modifications.