Original author(s) | Remy Card |
---|---|
Operating system | Linux |
Platform | Cross-platform |
Type | Command |
Operating system | BSD-like systems, includingmacOS |
---|---|
Platform | Cross-platform |
Type | Command |
chattr is thecommand inLinux that allows a user to set certainattributes of a file.lsattr is the command that displays the attributes of a file.
MostBSD-like systems, includingmacOS, have always had an analogouschflags command to set the attributes, but no command specifically meant to display them; specific options to thels command are used instead. The chflags command first appeared in4.4BSD.
Solaris has no commands specifically meant to manipulate them.chmod[1] andls[2] are used instead.
OtherUnix-like operating systems, in general, have no analogous commands. The similar-sounding commandschatr (fromHP-UX) andlsattr (fromAIX) exist but have unrelated functions.
Among other things, thechattr command is useful to make files immutable so that password files and certain system files cannot be erased during software upgrades.[3]
The command line toolschattr (to manipulate attributes) andlsattr (to list attributes) were originally specific to the Second Extended Filesystem family (ext2,ext3,ext4), and are available as part of thee2fsprogs package.
However, the functionality has since been extended, fully or partially, to many other systems, includingXFS,ReiserFS,JFS andOCFS2. The btrfs file system includes the attribute functionality, including theC
flag, which turns off the built-in copy-on-write (CoW) feature of btrfs due to slower performance associated with CoW.
The form of thechattr 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 messagesThe form of thelsattr 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, includingdotfiles-d
lists directories like other files, rather than listing their contentsSome attributes include:
Attribute | lsattr flag | chattr option | Semantics and rationale |
---|---|---|---|
Noatime updates | A | +A to set-A to clear |
|
Append only | a | +a to set-a to clear[note 1] |
|
Compressed | c | +c to set-c to clear[note 2] |
|
No Copy-on-Write (CoW) | C | +C to set-C to clear[note 3] |
|
Synchronous directory updates | D | +D to set-D to clear |
|
No dump | d | +d to set-d to clear |
|
Compression error | E | (unavailable) |
|
Extent format | e | (unavailable) |
|
Huge file | h | (unavailable) |
|
Indexed directory | I | (unavailable) |
|
Immutable | i | +i to set-i to clear[note 1] |
|
Data journaling | j | +j to set-j to clear[note 4] |
|
Secure deletion | s | +s to set-s to clear[note 2][note 5] |
|
Synchronous updates | S | +S to set-S to clear |
|
Top of directory hierarchy | T | +T to set-T to clear |
|
No tail-merging | t | +t to set-t to clear |
|
Undeletable | u | +u to set-u to clear[note 2] |
|
Compression raw access | X | (unavailable) |
|
Compressed dirty file | Z | (unavailable) |
|
Version / generation number | -v | -vversion |
|
CAP_LINUX_IMMUTABLE
capability can set or clear these attributes.CAP_SYS_RESOURCE
capability can set or clear this attribute.Thechflags command is not specific to particular file systems.UFS on BSD systems, andAPFS,HFS+,SMB,AFP, andFAT on macOS support at least some flags.
The form of thechflags 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.BSD-like systems, in general, have no default user-level command specifically meant to display the flags of a file. Thels command will do with either the-lo
, or the-lO
, depending on the system, flags passed.
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 | 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 fromsingle user mode |
User no-unlink | uunlnk | uunlnk, uunlink | Yes | FreeBSD, DragonFly BSD | File cannot be removed, renamed or mounted on |
Hidden | hidden | hidden | Yes | macOS | File is hidden by default in the GUI (but not inls) |
Hidden | hidden | hidden, uhidden | Yes | FreeBSD | File might be hidden by default in some GUI desktops (but not inls) |
Tracked | tracked | tracked | Yes | macOS | File modifications and deletions are tracked |
Restricted | restricted | restricted | No | macOS | File is protected bySystem Integrity Protection; accompanied by theextended attributecom.apple.rootless; flag needs to be set or cleared from Recovery Mode |
Compressed | compressed | No | macOS | File isHFS-compressed (read-only flag); not available onAPFS-formatted volumes | |
Data Vault | No | macOS | Hidden privacy flag sincemacOS Mojave set by the core system to prohibit any access without special entitlements | ||
Offline | offline | offline, uoffline | Yes | FreeBSD | File is offline |
Snapshot | snapshot | No | FreeBSD, NetBSD | File is a snapshot file (read-only flag) | |
Sparse | sparse | sparse, usparse | Yes | FreeBSD | Writes of all zeroes may be written as "holes" |
Must be archived | uarch | uarch, uarchive | Yes | FreeBSD | File must be archived |
ATTRIB
– analogous command in MS-DOS, OS/2 and Microsoft Windowschown
– change file/directory ownership in a Unix systemchmod
– change file access control attributes in a Unix systemcacls
– change file access control lists in Microsoft Windows NTchmod(1)
– illumos andOpenSolaris User Commands ReferenceManual from latest Sun basedOpenSolarisls(1)
– illumos andOpenSolaris User Commands ReferenceManual from latest Sun basedOpenSolarischflags(1)
– OpenBSD General CommandsManualchattr(1)
– Linux User CommandsManuallsattr(1)
– Linux User CommandsManualchflags(1)
– OpenBSD General CommandsManualchflags(1)
– FreeBSD General CommandsManualchflags(1)
– NetBSD General CommandsManualchflags(1)
– Darwin andmacOS General CommandsManual (outdated; seenewer version)