In Linux, beyond UGO/RWX permissions and ACLs (Access Control Lists), file and directory attributes add an extra layer of control over how system programs and users interact with files. These attributes help secure, manage, and customize file behavior, providing additional control over critical system and user files.
Let’s begin by exploring how to view file attributes, then dive into some commonly used attributes and learn how to set or remove them.
Viewing File Attributes with lsattr
To check the attributes of files and directories, you can use the lsattr
utility. This command is typically included in the e2fsprogs
package, which is standard on most Linux distributions.
To list attributes for all files in the current directory:
$ lsattr
--------------e------- ./Videos
--------------e------- ./Music
--------------e------- ./hello
--------------e------- ./visual-studio-code-bin
--------------e------- ./Downloads
--------------e------- ./config-2
--------------e------- ./book.png
--------------e------- ./Pictures
--------------e------- ./first-steps-with-linux
--------------e------- ./logs
----i---------e------- ./demo
The output of lsattr
shows attributes that are set on a file, like e (for extents) or - (indicating an unset attribute). In addition to e
, you may also see flags like i, a, and d, which we will cover in the next section. The lsattr
output provides a quick way to verify which attributes are active on each file or directory.
To list attributes for a specific file or directory:
$ lsattr Downloads
--------------e------- Downloads/nord.png
--------------e------- Downloads/2024-10-03_20-17.png
--------------e------- Downloads/progress.png
--------------e------- Downloads/fstab.png
--------------e------- Downloads/config
--------------e------- Downloads/nord2.png
--------------e------- Downloads/ascii.png
When you run lsattr
on a directory, it will display the attributes for each file within that directory, giving a detailed view of individual file settings.
If you want more readable output that displays attributes in word form rather than single letters, you can use the -l
option. For example:
$ lsattr -l demo
demo/configs Immutable, Extents
demo/deployment.yaml Immutable, Extents
demo/packages.json Immutable, Extents
demo/data.txt Immutable, Extents
demo/notes.pdf Immutable, Extents
demo/shitgrep Immutable, Extents
demo/shitgrep.c Immutable, Extents
demo/hel.bak Immutable, Extents
demo/shitcd.c Immutable, Extents
demo/shitcd Immutable, Extents
demo/dockerfile Immutable, Extents
demo/hello.bak Immutable, Extents
demo/config.conf Immutable, Extents
With lsattr -l
, the attributes are displayed in full words (e.g., "Immutable" and "Extents") instead of abbreviations. This can make it easier to understand at a glance which attributes are applied to each file or directory.
Managing File and Directory Attributes
File and directory attributes can be managed using the chattr
command. Let’s go through a few essential attributes and see how they work.
Keep reading with a 7-day free trial
Subscribe to sysxplore to keep reading this post and get 7 days of free access to the full post archives.