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 describe how a file behaves in Linux. It also describes if a file is compressed, deletable or if a file can be changed or modified.
The chattr command is included in Ubuntu by default and is available based on the file systems used. One common use of the chattr command is to set the immutable flag on a file which prevents a file from being deleted or renamed.
About chattr command:
The chattr command is used to change a file attribute in Ubuntu Linux. It is commonly used to make changes to files that can’t be deleted or renamed on a file system.
Syntax:
The 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 by the commands.
Below are some options for 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 time record is not changed. Atime is the last time a file was opened or accessed. |
-i | When the -I attribute is used, the file is immutable, meaning 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 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 a 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
For 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.