How to use Chattr Command in Linux Terminal

How to use Chattr Command in Linux Terminal

In this article we are going to show you how to use chattr Command in Linux operating system. If you want to learn about chattr command in Linux then this post is ideal for you.

In the Linux, the chattr command is a file system command that is used to change the attributes of a file in a directory. File attributes are meta-data properties that describes the behavior of file’s in the Linux.

The common use of the chattr command is to make a file immutable, undeletable and many more. The chattr command also indicate if a file is compressed, delete-able or if a file can be changed or modified.


How to Use chattr command in Linux Terminal

In the Linux, the basic syntax of the chattr command is show as below:

chattr [OPTIONS] [OPERATOR][ATTRIBUTES] FILE...

Here [OPERATOR] are:

  • + – It is used to add specified attributes to the existing ones.
  • - – Minus operator used to remove specified attributes from the existing ones.
  • = – It is used to set specified attributes as the only attributes.

Below is a list of some common attributes of the chattr command:

  • a – When this attribute is used, the file can only be opened in append mode for writing.
  • A – When this attribute is used, its atime record is not changed.
  • i – This attribute describes that the file is immutable, which means it cannot be deleted or renamed.

To check a complete list of all available file attributes and flags, Run man chattr  command in your terminal as show below.

man chattr
       chattr changes the file attributes on a Linux file system.

       The format of a symbolic mode is +-=[aAcCdDeijsStTu].

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

       The  letters  'aAcCdDeijsStTu' select the new attributes for the files:
       append only (a), no atime updates (A), compressed (c), no copy on write
       (C), no dump (d), synchronous directory updates (D), extent format (e),
       immutable (i), data journalling (j), secure deletion  (s),  synchronous
       updates  (S),  no tail-merging (t), top of directory hierarchy (T), and
       undeletable (u).

       The following attributes are read-only, and may be listed by  lsattr(1)
       but  not  modified  by  chattr:  compression  error (E), huge file (h),
       indexed directory (I), inline data (N), compression raw access (X), and
       compressed dirty file (Z).

       Not  all  flags  are supported or utilized by all filesystems; refer to
       filesystem-specific man pages such as btrfs(5), ext4(5), and xfs(5) for
       more filesystem-specific details.

Examples of chattr command

When you run the chattr command without any option then it will display only the help information.

chattr

To make a file immutable, use the i flag with the + operator to the existing attributes. Simply run the command below:

sudo chattr +i filename.txt

To Confirm that the attribute is added or not, run the command below:

sudo lsattr filename.txt
Output
----i---------e----- filename.txt

Run the command below with the - operator, if you want to revert the change and remove the immutable attribute:

sudo chattr -i filename.txt

The chattr command also allows you to use multiple attributes at once in single command. For example, if you wish to make a file immutable and also tell the kernel not to track the last accessed time. To do that run the commands below:

sudo chattr +iA filename.txt

That’s all.

If you face any error and issue in above steps , please use comment box below to report.

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top