Introduction to chattr command
In Linux, the file attributes are the settings that determine the file's behaviour. An attribute can determine whether a file can be modified, deleted, renamed, compressed, etc. chattr changes the attributes of files or directories in a Linux system. The attributes can be changed to make files secured so other users except superusers cannot modify the data in the file. You can view the file attributes using lsattr command.
Some Common Files Attributes
Following are some of the attributes that can be set to the file.
- A: The time record is not modified when you access the file.
- a: The file can only be open in append mode for writing.
- c: The file is automatically compressed on the disk by the kernel.
- D: The changes are written synchronously on the disk.
- d: A file is not backed up when the dump program is used.
- E: It indicates a compressed file has a compression error.
- e: The file is using extents for mapping the blocks on disk.
- I: It is used by the htree code to indicate that a directory is being indexed using hashed trees.
- i: A file cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file.
- j: A file having the 'j' attribute has all of its data written to the ext3 journal before being written to the file itself.
- s: When a file with the 's' attribute set is deleted, its blocks are zeroed and written back to the disk.
- S: When a file is modified, the changes are written synchronously on the disk.
Syntax of chattr and lsattr command
The syntax of chattr command is:
$ sudo chattr [option] [mode] files
The format of a symbolic mode is
+-=[acdeijstuADST]. '+' is used to add the selected attributes to the existing attributes and '-' is used to remove the selected attributes. Whereas '=' causes the selected attributes to be the only attribute left. chattr allows you to add or remove multiple attributes at once.
Some options available in
- -R: Change attributes of directories and their contents recursively
- -V: Display the verbose output and program version
- -f: Suppress the error message
The syntax for
lsattr command is:
$ lsattr [option] file
These are some options available in
- -R: List directories and their contents recursively
- -V: Display the program version
- -a: List all files in directories
Different examples to use chattr and lsattr command
1. lsattr command to list the attributes of file or directory
lsattr command followed by a file or directory name to view the attributes.
$ lsattr file
ubuntu@golinux:~$ lsattr test.txt
When no file or directory is specified,
lsattr command lists the attributes of all files and directories present in the working directory.
2. chattr command to allow file to be opened in append mode only
The attribute 'a' should be added to make the file to be opened in only append mode.
$ sudo chattr +a file
3. chattr command to make any file immutable
You can add the 'i' attribute to make a file immutable. So you can use +i attribute to make sure a file cannot be deleted or modified or renamed.
$ chattr +i file
4. chattr command to remove any attribute
You can remove the attribute using '-' followed by the attribute symbol.
$ sudo chattr -attribute file
To remove the 'i' attribute, you can use the below command.
As you can see, a file can be modified after removing the 'i' attribute.
5. chattr command to set only selected attributes
The '=' symbol is used to set only the selected attributes to the file. Other attributes will be removed from the file.
$ sudo chattr =attr file
6. lsattr command to list the attributes of directories recursively
-R option lists the attributes of directories and their files and sub-directories.
$ lsattr -R dir
7. chattr command to change attributes of directories recursively
You can also use
-R option with chattr to change the attributes of directories and their contents recursively.
$ sudo chattr +a -R dir
8. List all files in directories with lsattr command
-a option can be used to list all files directories, including files that start with '.'.
$ lsattr -a [dir]
9. List directory like other files with lsattr command
Normally, lsattr lists all files in the directory. But with
-d option, lsattr only lists the directory rather than listing its contents.
$ lsattr -d dir
ubuntu@golinux:~$ lsattr -d Record
10. Suppress error messages of chattr command
You can suppress or hide the error messages in the output displayed by the chattr command. It does not hide all error messages.
$ chattr -f +a file
11. chattr command to display the verbose output
chattr command does not display any output. You can use
-V option to view the verbose output of chattr command. It also prints the chattr version.
$ sudo chattr -V +a file
ubuntu@golinux:~$ sudo chattr -V +a system.txt
chattr 1.45.5 (07-Jan-2020)
Flags of system.txt set as -----a--------e-----
12. lsattr command to list the file's version/generation number
You can use
-v option with lsattr to view the file's version/generation number.
$ lsattr -v file
ubuntu@golinux:~$ lsattr -v system.txt 851053182 -----a--------e----- system.txt
This tutorial teaches you to use
lsattr command in Linux. chattr helps to change attributes and lsattr helps to list attributes of files and directories in the Linux system. If you still have any confusion, please let us know in the comment section.