Linux

Chattr Command in Linux (File Attributes)

Chattr Command in Linux (File Attributes)

In Linux, file attributes are meta-data properties that describe the file's behavior. For example, an attribute can indicate whether a file is compressed or specify if the file can be deleted.

Some attributes like immutability can be set or cleared, while others like encryption are read-only and can only be viewed. The support for certain attributes depends on the filesystem being used.

This article explains how to use the chattr command to change file attributes on Linux file systems.

chattr Syntax #

The chattr command takes the following general form:

chattr [OPTIONS] [OPERATOR][ATTRIBUTES] FILE… 

The value of the [OPERATOR] part can be one of the following symbols:

The operator is followed by one or more [ATTRIBUTES] flags that you want to add or remove from the file attributes. Below is a list of a few common attributes and associated flags:

For a complete list of all file attributes and flags, type man chattr in your terminal.

By default, file attributes are not preserved when copying a file with commands like cp or rsync .

chattr Example #

One of the common uses of chattr is to set the immutable flag to a file or directory to prevent users from deleting or renaming the file.

You can view the file attributes with the lsattr command:

lsattr todo.txt

The output below shows that only the e flag is set:

--------------e----- todo.txt 

To make the file immutable, add the i flag with the + operator to the existing attributes:

sudo chattr +i todo.txt

We're using sudo because only root can change the immutable flag.

Confirm that the attribute is added:

lsattr todo.txt
----i---------e----- todo.txt 

To revert the changes and remove the immutable flag, use the - operator:

sudo chattr +i todo.txt

With chattr, you can add or remove multiple attributes at once. For example, to make the file immutable and tell the kernel not to track the last accessed time, you would use:

sudo chattr +iA todo.txt

The last operator that you can use is the = operator. For example, to set the e attribute as the only attribute, you would run:

sudo chattr "=e" todo.txt

Notice that the operator and flag are enclosed in quotes, to avoid shell interpretation of the + character.

Conclusion #

chattr is a command-line tool for changing the file attributes on a Linux file system.

If you have any questions or feedback, feel free to leave a comment.

How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...
Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...
Add Mouse gestures to Windows 10 using these free tools
In recent years computers and operating systems have greatly evolved. There was a time when users had to use commands to navigate through file manager...