Chattr


' is the command in the GNU operating system that allows a user to set certain attributes of a file. is the command that displays the attributes of a file.
Most BSD-like systems, including macOS, have always had an analogous
' command to set the attributes, but no command specifically meant to display them; specific options to the ls| command are used instead.
Solaris has no commands specifically meant to manipulate them. chmod| and ls| are used instead.
Other Unixes, in general, have no analogous commands. The similar-sounding commands and exist but have unrelated functions.
Among other things, the command is useful to make files immutable so that password files and certain system files cannot be erased during software upgrades.

In [GNU]/Linux systems">Linux">Linux systems ( and )

File system support

The command line tools and were originally specific to the Second Extended Filesystem family, and are available as part of the e2fsprogs package.
However, the functionality has since been extended, fully or partially, to many other systems, including XFS, ReiserFS, JFS and OCFS2. The btrfs file system includes the attribute functionality, including the C flag, which turns off the built-in copy-on-write feature of btrfs due to slower performance associated with CoW.

description

The form of the command is:

chattr files...

The form of the command :

lsattr

Some attributes include:
Attribute flag optionSemantics and rationale
No atime updatesA+A to set
-A to clear

  • When a file with the A attribute set is accessed, its record is not modified.
  • This avoids a certain amount of disk I/O operations.
Append onlya+a to set-a to clear
  • A file with the a attribute set can only be open in append mode for writing.
Compressedc+c to set-c to clear
  • A file with the c attribute set is automatically compressed on the disk by the kernel.
  • A read from this file returns uncompressed data.
  • A write to this file compresses data before storing them on the disk.
No Copy-on-Write C+C to set-C to clear
  • A file with the C attribute will not be subject to Copy-on-Write updates.
  • Updates to these files may not be subject to atomic snapshots, and may lack some reliability information on some filesystems and kernels.
Synchronous directory updatesD+D to set-D to clear
  • When a directory with the D attribute set is modified, the changes are written synchronously on the disk
  • This is equivalent to the dirsync mount option, applied to a subset of the files.
No dumpd+d to set-d to clear
  • A file with the d attribute set is not candidate for backup when the program is run.
Compression errorE
  • The E attribute is used by the experimental compression patches to indicate that a compressed file has a compression error.
  • Extent formate
  • The e attribute indicates that the file is using extents for mapping the blocks on disk.
  • Huge fileh
  • The h attribute indicates the file is storing its blocks in units of the filesystem blocksize instead of in units of sectors.
  • It means that the file is, or at one time was, larger than 2TB.
  • Indexed directoryI
  • The I attribute is used by the program code to indicate that a directory is being indexed using hashed trees.
  • Immutablei+i to set-i to clear
    • 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.
    • When set, prevents, even the superuser, from erasing or changing the contents of the file.
    Data journalingj+j to set-j to clear
    • A file with the j attribute has all of its data written to the journal before being written to the file itself, if the filesystem is mounted with the "data=ordered" or "data=writeback" options.
    • When the filesystem is mounted with the "data=journal" option all file data is already journaled, so this attribute has no effect.
    Secure deletions+s to set-s to clear
    Synchronous updatesS+S to set-S to clear
    • 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.
    • This is equivalent to the sync mount option, applied to a subset of the files.
    Top of directory hierarchyT+T to set-T to clear
    • A directory with the T attribute will be deemed to be the top of directory hierarchies for the purposes of the Orlov block allocator.
    • This is a hint to the block allocator used by and that the subdirectories under this directory are not related, and thus should be spread apart for allocation purposes.
    • For example: it is a very good idea to set the T attribute on the /home directory, so that /home/john and /home/mary are placed into separate block groups.
    • For directories where this attribute is not set, the Orlov block allocator will try to group subdirectories closer together where possible.
    No tail-mergingt+t to set-t to clear
    • For those filesystems that support tail-merging, a file with the t attribute will not have a partial block fragment at the end of the file merged with other files.
    • This is necessary for applications such as LILO, which reads the filesystem directly and doesn't understand tail-merged files.
    Undeletableu+u to set-u to clear
    • When a file with the u attribute set is deleted, its contents are saved.
    • This allows the user to ask for its undeletion.
    Compression raw accessX
  • The X attribute is used by the experimental compression patches to indicate that a raw contents of a compressed file can be accessed directly.
  • Compressed dirty fileZ
  • The Z attribute is used by the experimental compression patches to indicate a compressed file is "dirty".
  • Version / generation number-v-v version
  • File's version/generation number.
  • In BSD-like systems ()

    File system support

    The command is not specific to particular file systems. UFS on BSD systems, and APFS, HFS+, SMB, AFP, and FAT on macOS support least some flags.

    description

    The form of the command is:

    chflags [-R [-H | -L | -P flags file...

    BSD-like systems, in general, have no default user-level command specifically meant to display the flags of a file. The command will do with either the -lo, or the -lO, depending on the system, flags passed.

    Attributes

    All traditional attributes can be set or cleared by the super-user; some can also be set or cleared by the owner of the file.
    Some attributes include:
    Attribute flag flagOwner-settableOS supportSemantics and rationale
    Archived, AllFile is archived
    OpaqueAllDirectory is opaque when viewed through a union mount
    No dumpAllFile cannot be dumped
    System append-only, AllExisting data in the file can't be overwritten and the file cannot be truncated
    User append-only, AllExisting data in the file can't be overwritten and the file cannot be truncated
    System immutable,, AllFile cannot be changed, renamed, moved, or removed
    User immutable,, AllFile cannot be changed, renamed, moved, or removed
    System no-unlinkAllFile cannot be removed, renamed or mounted on; on macOS this flag needs to be set or cleared from single user mode
    User no-unlinkSomeFile cannot be removed, renamed or mounted on; not supported by macOS
    HiddenFreeBSDFile is hidden by default in the GUI
    User hiddenFreeBSDFile is hidden by default in the GUI
    HiddenmacOSFile is hidden by default in the GUI
    TrackedmacOSFile modifications and deletions are tracked
    RestrictedmacOSFile is protected by System Integrity Protection; accompanied by the extended attribute ; flag needs to be set or cleared from Recovery Mode
    CompressedmacOSFile is HFS-compressed ; not available on APFS-formatted volumes
    Data VaultmacOSHidden privacy flag since macOS Mojave set by the core system to prohibit any access without special entitlements

    BSD systems offer additional flags like offline, snapshot, sparse, and uarchive; see References.