Linuxstar
Published on

chattr command-change file attributes

Jonas

Jonas

2 min read
The chattr command can change the file attributes of the linux file system. The general format is: +-=[aAcCdDeijsStTu] "aAcCdDeijsStTu" represents the new attributes that can be given to the file. These attributes have the following modes.
  1. a: Let the file or directory be for additional purposes only
  2. A: When a file with "A" attribute is accessed, its a time record will not be modified
  3. c: Save the file or directory after compression
  4. d: Exclude files or directories from dumping operations
  5. i: Do not change files or directories arbitrarily
  6. j: If the file system is installed with the "data=order" or "data=writeback" option, the file with the "j" attribute will write all its data into the ext 3 log before writing to the file itself
  7. s: When deleting a file with "s" attribute set, its block will be zeroed and written back to disk
  8. S: When modifying a file with the "S" attribute set, the change will be written to the disk synchronously. This is equivalent to the "synchronous" mount option applied to a subset of files
  9. u: When deleting a file with the 'u' attribute set, its content will be saved.
Syntax:
chattr [parameter] [file]

Command parameters:

-R Recursively process all files in the directory
-v Set file or directory version
-V Show instruction execution process
+ Open the attribute of the file or directory
- Turn off the attribute of the file or directory
= Specifies the attribute of the file or directory

Example

Use the chattr command to prevent a key file in the system from being modified (locked):
[root@linuxstar ~]# chattr +i /etc/resolv.conf
Unlock:
[root@linuxstar ~]# chattr -i /home/omd/h.txt
Let a file only add data to it, but not delete it, applicable to various log files:
[root@linuxstar ~]# chattr +a /var/log/messages