Zend Certified Engineer

phpguru.org

Free PHP, Javascript and C# code


Linux file attributes

A little known, or at least little mentioned, aspect of the Linux ext2fs is file attributes. Not permissions, attributes. They allow you to do some quite useful things that you might think you need other software for, but they're actually built into your file system. To view or change these you can use the lsattr and chattr commands. The available attributes are:

  • a Append only. Files with this attribute set can only be added to.
  • A No atime update. When a file with this attribute is set and the file is accessed, the atime update is not updated. It can produce a performance increase in some circumstances.
  • c Compressed. A file with this attribute is compressed when written to disk, so you don't have to do it. Check support though because (IIRC) this one isn't totally implemented (if at all).
  • D Changes to files in a directory with this attribute are written synchronously to disk.
  • d A file with this attribute set is not a candidate for backup when the dump command is run.
  • E Used to indicate a compression error. It cannot be set or reset using chattr, but can be displayed using lsattr.
  • I This 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.
  • i Immutable. When this attribute is set the file cannot be modified, deleted or renamed. No link can be created to this file.
  • j A file with the this attribute set has its data written first to the ext3 journal before being written to the disk.
  • s When a file with this attribute is set its blocks on the disk are zeroed first.
  • S Files with this attribute set are written synchronously to the disk. Same as mounting a disk with the sync option but only on a subset of files.
  • T When a directory has this attribute set, it 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).
  • t When files have this attribute set they will not have a partial block fragment at the end of the file merged with aother (for those filesystems which support tail-merging). This is necessary for applications such as LILO which read the filesystem directly, and which don't understand tail-merged files.
  • u When a file with this attribute is set, its contents are saved. Permitting undeletion.
  • X Used by experimental compression patches to indicate a files raw contents may be accessed directly.
  • Z This attribute is used by the experimental compression patches to indicate a compressed file is dirty (much like my girlfriend). It may not be set or reset using chattr, although it can be displayed by lsattr.

Some quite handy things I think you'll agree. The most useful ones I think are a, A and i. And c, but my man page says it's experimental (bear in mind my version of Linux is rather old). If you want more information, try man chattr.

See also

http://www.securityfocus.com/infocus/1407 More information

Link to me

If you use any of the code on this site (and if you don't I guess) or it makes your life easier, I'd appreciate a link - http://www.phpguru.org. Thanks.

Last modified: 14:59 26th May 2008