Extended Attributes in Linux filesystems like ext4

The capacity of storage has increased by leaps and bounds over the past decade. This has resulted in an explosion of the variety and quantum of data stored on it. There are no clear measures to control this explosion. For example, if you create a temporary file to store some comments on it, you always leave it there, unless you are particular to go back and remove it after you are done working with it.

In Linux, you have options called Extended Attributes to define extended attributes for a file, set them, query them and take actions upon their value.

To use extended attributes, install attr from your distribution’s repository (below is applicable to Ubuntu):

$ sudo apt-get install attr <enter>

Next, create an extendeded attribute. I would like to create an extended attribute to identify if a file is to be stored permanently or temporarily. Therefore, I will call this extended attribute tempperm and it can have 2 possible values t and p, where t is temporary and p is permanent.

So here is how it is created.
$ attr -s tempperm -V "t" UH.txt <enter>
Attribute “tempperm” set to a 1 byte value for UH.txt:
t

Now the attribute is defined as tempperm and attr will always show it as user.tempperm when you query it.

Here is how to query the value of the extended attribute created above.
$ getfattr -d UH.txt <enter>
# file: UH.txt
user.tempperm=”t”

Now, we want to query the value of the tempperm extended attribute in our path, we give the following command:

$ getfattr -n user.tempperm -m *.txt . <enter>
abcd.txt: user.tempperm: No such attribute
xyz.txt: user.tempperm: No such attribute
# file: UH.txt
user.tempperm=”t”

DU.txt: user.tempperm: No such attribute
.: user.tempperm: No such attribute

See the result above. The only file in the folder that has the tempperm extended attribute set as t is UH.txt. The output from the above command can be used to do any kind of action on the file with the user.tempperm=”t”.

You can use a program called xattr to get formatted results to script actions on the files as you now get the : as a delimiter.
$ xattr -lp user.tempperm *.txt <enter>
UH.txt: user.tempperm: t