The chattr
command is used to change file attributes on a Linux file system. This command allows users to set special attributes on files and directories to prevent accidental deletion or modification, prevent files from being overwritten, or to make files immutable.
Overview
The chattr
command is used to change file attributes on a Linux file system. The general syntax for the chattr
command is as follows:
chattr [options] [files or directories]
To change file attributes, the user must have write permission on the file or directory. Here are some examples of how to use the chattr
command:
chattr +i file.txt
This command makes the file file.txt
immutable, meaning it cannot be modified, deleted, or renamed. To remove the immutable attribute, use the -i
option:
chattr -i file.txt
The chattr
command can also be used to set other file attributes, such as making a file append-only or setting a file to be automatically compressed. Here are some examples:
chattr +a file.txt
This command makes the file file.txt
append-only, meaning new data can only be added to the end of the file. To remove the append-only attribute, use the -a
option:
chattr -a file.txt
chattr +c file.txt
This command sets the file file.txt
to be automatically compressed by the kernel. To remove the compression attribute, use the -c
option:
chattr -c file.txt
Options
The following table lists the available options for the chattr
command:
Option | Description |
---|---|
+a |
Sets the file to be append-only |
+c |
Sets the file to be automatically compressed |
+d |
Sets the file to be excluded from backup when using dump |
+i |
Sets the file to be immutable |
+j |
Sets the file to use data journaling |
+s |
Sets the file to be secure-delete enabled |
+t |
Sets the file to have a ‘top of directory hierarchy’ attribute |
-a |
Removes the append-only attribute |
-c |
Removes the compression attribute |
-d |
Removes the no-dump attribute |
-i |
Removes the immutable attribute |
-j |
Removes the data journaling attribute |
-s |
Removes the secure-delete attribute |
-t |
Removes the top of directory hierarchy attribute |
-R |
Recursively changes file attributes for directories and their contents |
Troubleshooting Tips
If you are unable to change the file attributes using the chattr
command, make sure that you have write permission on the file or directory. Additionally, some file systems may not support certain attributes, so check the documentation for your file system to see which attributes are supported.
Notes
- The
chattr
command can only be used by the root user or a user with sudo privileges. - The
chattr
command can be used to set attributes on directories as well as files. - The
chattr
command can be used in conjunction with thelsattr
command to view file attributes.