chattr is the command in Linux that allows a user to set certain attributes of a file. lsattr is the command that displays the attributes of a file.
Most BSD-like systems, including macOS, have always had an analogous chflags command to set the attributes, but no command specifically meant to display them; specific options to the ls command are used instead. The chflags command first appeared in 4.4BSD.
Solaris has no commands specifically meant to manipulate them. chmod[1] and ls[2] are used instead.
Other Unix-like operating systems, in general, have no analogous commands. The similar-sounding commands chatr (from HP-UX) and lsattr (from AIX) exist but have unrelated functions.
Among other things, the chattr command is useful to make files immutable so that password files and certain system files cannot be erased during software upgrades.[3]
In Linux systems (chattr and lsattr)
File system support
The command line tools chattr (to manipulate attributes) and lsattr (to list attributes) were originally specific to the Second Extended Filesystem family (ext2, ext3, ext4), 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 (CoW) feature of btrfs due to slower performance associated with CoW.
chattr description
The form of the chattr command is:
chattr[-RVf][-+=AacDdijsTtSu][-vversion]files...
-R recursively changes attributes of directories and their contents
-V is to be verbose and print the program version
-f suppresses most error messages
lsattr description
The form of the lsattr command (gnu 1.41.3):
lsattr[-RVadv][files...]
-R recursively lists attributes of directories and their contents
-V displays the program version
-a lists all files in directories, including dotfiles
-d lists directories like other files, rather than listing their contents
Attributes
Some attributes include:
File attributes on a Linux file system according to the chattr(1) Linux man page
Attribute
lsattr flag
chattr option
Semantics and rationale
No atime updates
A
+A to set -A to clear
When a file with the A attribute set is accessed, its atime record is not modified.
This avoids a certain amount of disk I/O operations.
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.
When the filesystem is mounted with the "data=journal" option all file data is already journaled, so this attribute has no effect.
When a file with the s attribute set is deleted, its blocks are zeroed and written back to the disk.
Synchronous updates
S
+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 syncmount option, applied to a subset of the files.
Top of directory hierarchy
T
+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 ext3 and ext4 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-merging
t
+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.
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 file
Z
(unavailable)
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.
Notes
^ abOnly the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear these attributes.
^ abcThese attributes are not honored by the ext2 and ext3 filesystems as implemented in the current mainline Linux kernels.
^These attributes only make sense for Copy-on-Write file-systems such as btrfs.
^Only the superuser or a process possessing the CAP_SYS_RESOURCE capability can set or clear this attribute.
^This attribute is not honored by the ext4 filesystem as implemented in the current mainline Linux kernels as reported in Bug #17872.
In BSD-like systems (chflags)
File system support
The chflags command is not specific to particular file systems. UFS on BSD systems, and APFS, HFS+, SMB, AFP, and FAT on macOS support at least some flags.
chflags description
The form of the chflags command is:
chflags[-R[-H|-L|-P]]flagsfile...
-H If the -R option is specified, symbolic links on the command line are followed. (Symbolic links encountered in the tree traversal are not followed.)
-L If the -R option is specified, all symbolic links are followed.
-P If the -R option is specified, no symbolic links are followed. This is the default.
-R Change the file flags for the file hierarchies rooted in the files instead of just the files themselves.
Displaying
BSD-like systems, in general, have no default user-level command specifically meant to display the flags of a file. The ls 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:
File attributes
Attribute
ls flag
chflags flag
Owner-settable
OS support
Semantics and rationale
Archived
arch
arch, archived
No
All
File is archived
Opaque
opaque
opaque
Yes
All
Directory is opaque when viewed through a union mount
No dump
nodump
nodump
Yes
All
File cannot be dumped
System append-only
sappnd
sappnd, sappend
No
All
Existing data in the file can't be overwritten and the file cannot be truncated
User append-only
uappnd
uappnd, uappend
Yes
All
Existing data in the file can't be overwritten and the file cannot be truncated
System immutable
schg
schg, schange, simmutable
No
All
File cannot be changed, renamed, moved, or removed
User immutable
uchg
uchg, uchange, uimmutable
Yes
All
File cannot be changed, renamed, moved, or removed
System no-unlink
sunlnk
sunlnk, sunlink
No
FreeBSD, DragonFly BSD, macOS
File cannot be removed, renamed or mounted on; on macOS this flag needs to be set or cleared from single user mode
User no-unlink
uunlnk
uunlnk, uunlink
Yes
FreeBSD, DragonFly BSD
File cannot be removed, renamed or mounted on
Hidden
hidden
hidden, uhidden
Yes
macOS, FreeBSD
File is hidden by default in the GUI (but not in ls)