Celebrate Our 22nd Anniversary with Huge Savings! Up to 70% Off

What is the use of chattr command? Explain it with example

The chattr command is a Linux utility that is used to change the file attributes of a file system object. These attributes are used to control the way in which files are accessed and modified. The chattr command can be used to make files immutable, append-only, no-delete, no-rename, and more. The command can be used to modify the attributes of files, directories, and symbolic links.
The syntax of the chattr command is: chattr [options] [file]
Where options are the attributes that you want to set, and file is the name of the file that you want to modify.

Examples of chattr command:

1. Making a file immutable

One of the most common uses of the chattr command is to make a file immutable. An immutable file cannot be modified, deleted, or renamed. This is useful for files that need to be protected from accidental changes or malicious attacks. Here is an example of how to make a file immutable:

# chattr +i filename

This command sets the immutable attribute for the file called filename. Once the attribute is set, the file cannot be modified, deleted, or renamed.

2. Making a file append-only

Another useful attribute that can be set with the chattr command is the append-only attribute. When this attribute is set, new data can only be added to the end of the file. This is useful for log files that need to be kept for auditing purposes. Here is an example of how to set the append-only attribute:

# chattr +a filename

This command sets the append-only attribute for the file called filename. Once the attribute is set, new data can only be added to the end of the file.

3. Making a file no-delete

The no-delete attribute can be set with the chattr command to prevent a file from being deleted. This is useful for files that need to be kept for legal or compliance reasons. Here is an example of how to set the no-delete attribute:

# chattr +d filename

This command sets the no-delete attribute for the file called filename. Once the attribute is set, the file cannot be deleted.

4. Checking file attributes

To check the attributes of a file, you can use the lsattr command. This command displays the attributes of a file in a readable format. Here is an example of how to use the lsattr command:

# lsattr filename

This command displays the attributes of the file called filename.

Conclusion:

The chattr command is a powerful tool that can be used to modify the file attributes of files, directories, and symbolic links. It can be used to make files immutable, append-only, no-delete, and more.


Was this answer helpful?

« Back

chat