How to use Chattr command in Ubuntu Linux with examples

This brief tutorial shows students and new users how to use the chattr command on Ubuntu Linux with examples.

The chattr command is used to change a file attribute in Ubuntu Linux. File attributes are meta-data properties that describes how a file behaves in Linux. It also describes if a file is compressed, delete-able or if a file can be changed or modified.

The chattr command is included in Ubuntu by default and available based on the file systems being used. One common uses of the chattr command is to set the immutable flag on a file which prevent a file from being deleted or renamed.

If you’re a student or new user looking for a Linux system to begin with, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners.

Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices.

About chattr command:

The chattr command is used to change a file attribute in Ubuntu Linux. It is commonly used to make changes to file that that they are can’t be deleted or renamed on a file system.

Syntax:

Syntax is the rule and format of how the chattr command can be used. These syntax options can be reordered, but a straight format must be followed.

Below is an example syntax of how to use the chattr command.

chattr [-pRVf] [-+=aAcCdDeijPsStTuF] [-v version] files…

Another way to use the chattr command

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

The [OPERATOR] section:

  • + The operator ‘+’ causes the selected attributes to be added to the existing attributes.
  • – The operator ‘-‘ causes the selected attributes to be removed from existing attributes.

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. They modify the behavior of the commands, and are separated by spaces, followed after the commands.

Below are some options of the chattr command:

   files. Replace files.. with the name of the file you want to change its attributes
-a When the -a attribute is used, the file can only be opened in append mode for
writing.
-A When the -A attribute is used, its atime record is not changed. Atime is the last time a file was opened or access.
-i When the -i attribute is used, the file is immutable which means it can’t be deleted or renamed.

Examples:

Below are some examples of how to run and use the chattr on Ubuntu Linux.

Simply run the chattr command to invoke it.

If you run the chattr command but don’t specify an option, it will simply display help information.

chattr

For example, if you want to make a file immutable, run the commands below:

sudo chattr +i documents.txt

To view attributes of files, run the commands below:

sudo lsattr documents.txt

It should display similar line as shown below:

----i---------e----- documents.txt

To revert the change and remove the immutable attribute, run the commands below:

sudo chattr -i documents.txt

You can also combine multiple attributes with the chattr command. For example, if you want to make a file immutable and also instruct the kernel not to track the last accessed time, run the commands below:

sudo chattr +iA documents.txt

More more details about the chattr command, run man chattr:

man chattr

It should display the help page for the chattr command.

NAME
       chattr - change file attributes on a Linux file system

SYNOPSIS
       chattr [ -RVf ] [ -v version ] [ -p project ] [ mode ] files.

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

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

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

Conclusion:

This post showed you how to use the chattr command in Ubuntu Linux. If you find any error above please use the form below to comment.