Changing file attributes with chattr
chattr is a not-well known command, but it can do very useful things with files attributes. In this post I will show you some examples.
Table of Contents
chattr is part of the e2fsprogs package, which is usually already installed on most Linux systems.
The basic syntax of
chattr [options] [mode] <files>
To list the attributes of a file, use
-Rto do recursive listing.
The most relevant option is
-R to recursively change attributes of directories and their contents.
The format of a mode consist of:
=: add attributes (
+), remove them (
-) or set only the specified ones (
- One or more of the following letters:
chattrman page for a full explanation of all available attributes.
# Add an attribute chattr +i some_file
# Remove an attribute chattr -i some_file
Now, I am going to explain some of the available attributes:
a: the file can only be opened in append mode. Only a superuser can set or clear this attribute.
$ echo "Hola" > test1 $ sudo chattr +a test1 $ echo "Adios" > test1 bash: test1: Operation not permitted $ echo "Adios" >> test1 $ cat test1 Hola Adios
A: don’t change access time when the file is accessed.
i: the file cannot be modified, deleted or renamed, and no link can be created using this file. The file cannot be opened in write mode. Only a superuser can set or clear this attribute.
$ sudo chattr +i test1 $ echo "Hasta luego" >> test1 bash: test1: Operation not permitted
Some attributes such as
s (overwrite with zeros when deleted) and
u (save contents when deleted) don’t work on ext2/3/4 filesystems.
If you have any suggestion, feel free to contact me via social media or email.