The ext2fs allows you to setup several attributes for each file or directory, each attribute modifies the behavior of a file, you can see the current attributes of a file with the lsattr command and you can change the attributes with the chattr command. For example, open a terminal window, then create a new “test” directory:

# mkdir test

create a new file:

# touch gg1.txt

then see the current attributes for this file using the lsattr command:

# lsattr gg1.txt

------------------ gg1.txt

 

  • Setting the A attribute for a file, the atime record is not modified. This avoids a certain amount of disk I/O for laptop systems.

    # chattr +A gg1.txt

    # lsattr gg1.txt

    -------A---------- gg1.txt

    you can reset the attribute by typing the following command:

    # chattr -A gg1.txt

    # lsattr gg1.txt

    ------------------ gg1.txt

  • You may want a file can only be open in append mode for writing, to do this you shall set the “a” attribute as superuser:

    # sudo chattr +a gg1.txt

    [sudo] password for user:

    # lsattr

    -----a------------ ./gg1.txt

    # echo "This is a test" > gg1.txt

    bash: gg1.txt: Operation not permitted

    # echo "This is a test" >>gg1.txt

    # more gg1.txt

    This is a test

    you can reset the attribute by typing the following command:

    # sudo chattr -a gg1.txt

    [sudo] password for user:

    # lsattr

    ------------------ ./gg1.txt

  • If you want a file to be automatically compressed by the kernel you shall set the ‘c’ attribute. A read from this file returns uncompressed data. A write to this file compresses data before storing them on the disk.

    # chattr +c gg1.txt

    # lsattr

    --------c--------- ./gg1.txt

    you can reset the attribute by typing the following command:

    # sudo chattr -c gg1.txt

    [sudo] password for user:

    # lsattr

    ------------------ ./gg1.txt

  • You can apply the ‘D’ attribute to a directory, each time the directory will be modified, the changes are written synchronously on the disk; this is equivalent to the “dirsync” mount option applied to a subset of the files.

  • If you do not want a file candidate for backup when the dump program is run you shall set the ‘d’ attribute.

    # chattr +d gg1.txt

    # lsattr

    ------d----------- ./gg1.txt

    # chattr -d gg1.txt

    # lsattr

    ------------------ ./gg1.txt

  • The ‘E’ attribute is used by the experimental compression patches to indicate that a compressed file has a compression error. It may not be set or reset using chattr, although it can be displayed by lsattr.

  • The ‘I’ attribute is used by the htree code to indicate that a directory is behind indexed using hashed trees. It may not be set or reset using chattr, although it can be displayed by lsattr.

  • A file with the ‘i’ attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser can set or clear this attribute. View the post ……………..

  • A file with the ‘j’ attribute has all of its data written to the ext3 journal before being written to the file itself, if the filesystem is mounted with the "data=ordered" or "data=writeback" options.

  • If you want a secure (!?!) remove you shall apply the ‘s’ attribute to a file, so when the file will be removed, its blocks will be zeroed and written back to the disk.

  • When a file with the `S’ attribute set is modified, the changes are written synchronously on the disk; this is equivalent to the `sync’ mount option applied to a subset of the files.

  • A directory with the ‘T’ attribute will be deemed to be the top of directory hierarchies for the purposes of the Orlov block allocator (which is used in on systems with Linux 2.5.46 or later).

  • A file with the ‘t’ attribute will not have a partial block fragment at the of the file merged with other files (for those filesystems which support tail-merging). This is necessary for applications such as LILO which read the filesystem directly, and who don’t understand tail-merged files.

  • When a file with the ‘u’ attribute set is deleted, its contents are saved. This allows the user to ask for its undeletion.

  • The ‘X’ attribute is used by the experimental compression patches to indicate that a raw contents of a compressed file can be accessed directly. It currently may not be set or reset using chattr, although it can be displayed by lsattr.

  • The ‘Z’ attribute is used by the experimental compression patches to indicate a compressed file is dirty. It may not be set or reset using chattr, although it can be displayed by lsattr.