Linux Comm Chattr
# Linux chattr Command
[ Linux Command Manual](#)
The Linux chattr command is used to change the attributes of files or directories. These attributes can control the behavior of the file system and provide more advanced file management functions.
Common Attributes
### Syntax
chattr [+/-/=attribute] file or directory
### Common Options
* `-R`: Recursively process directories and their subdirectories
* `-V`: Display verbose information
* `-v`: Display version information
### Attribute Modes
* `+` : Add an attribute
* `-` : Remove an attribute
* `=` : Set to the specified attribute
### Common Attributes
| Attribute | Description |
| --- | --- |
| `a` | **Append Only**: The file can only have content appended to it; existing content cannot be deleted or modified (requires root privileges). |
| `i` | **Immutable**: The file cannot be deleted, modified, renamed, or have hard links created (requires root privileges). |
| `A` | Do not update the file's last access time (`atime`). |
| `c` | The file is automatically compressed on disk (supported by some file systems). |
| `s` | Secure deletion: When the file is deleted, its data is zeroed out (irrecoverable). |
| `u` | When the file is deleted, its contents can still be recovered (opposite of `s`). |
| `d` | The file is skipped during `dump` backups. |
### Examples
**Adding an attribute (+):**
sudo chattr +i file.txt # Set the file to immutable (prevent deletion/modification) sudo chattr +a /var/log/syslog # Log file can only be appended to
**Removing an attribute (-):**
sudo chattr -i file.txt # Remove the immutable attribute
**Resetting attributes (=):**
sudo chattr =a file.txt # Remove all attributes, only keep `a`
**Protect important configuration files:**
chattr +i /etc/passwd chattr +i /etc/shadow
**Set a log file to append-only:**
chattr +a /var/log/messages
**Recursively set directory attributes:**
chattr -R +i /etc/important/
**View file attributes (using the lsattr command):**
lsattr filename
Example output:
----i--------- file.txt # `i` indicates immutable
[ Linux Command Manual](#)
YouTip