Linux Commands

How to Check and Set the Linux File Attributes on the Command Line

In Linux, file attributes are additional metadata associated with a file or directory that provides additional information or functionality. You can view and change these attributes by utilizing the lsattr and chattr commands. Understanding and properly utilizing the file attributes can improve the security and performance of a Linux system by checking and setting the Linux file attributes, but many beginners don’t know how to do it in Linux. In this short guide, we will explain the simple ways to check and set the Linux file attributes on the command line.

How to Check and Set the Linux File Attributes on the Command Line

Here, we will give you a brief discussion on the two commands that you can try to check and set the file attributes from the terminal.

Lsattr Command

The lsattr command is used to display the file attributes of a given file or directory. The command output shows a series of letters corresponding to specific attributes. For instance, the “i” attribute makes the file immutable. It means that the file cannot be modified or deleted. The “d” attribute signifies that the file is a directory.

To check the attributes of a file or directory, simply type “lsattr” followed by its name in the terminal. Here’s an example:

lsattr testFile.txt

This command outputs the attributes of the “TestFile.txt” file. The output looks something like the following image. We can see that the “e” attribute is set, indicating that the file will use extents to map the blocks on the disk.

Each letter in the output corresponds to a specific attribute:

Attribute Full Form Explanation
a Append only Using this mode, you can add more content to the file, but you cannot remove the content from the file.
c Compressed The data that is written to the file is compressed automatically by the kernel, and it is uncompressed back at the time of reading.
d No dump This attribute ensures that the file backup is not made in the backups where the dump is used.
e Extent format For block mapping on the disk, the file uses extent.
i Immutable Using this attribute, the file becomes immutable, which means that you are restricted from performing operations on the file.
s Secure deletion You cannot recover the file after deletion using this option.
t No tail-merge In this process, small data pieces that don’t fill the complete block are merged with similar data pieces from other files.
u Undeletable The contents of the deleted files are saved.
A No atime update The access timestamp of the Linux file is not updated when you access the file.
D Synchronous directory updates Modifications made to the file are promptly stored on the disk before they reach the cache.
S Synchronous updates The modifications made to the file are immediately written to the disk.

Chattr Command

You can use the chattr command to alter the attributes of a specified file or directory. This command takes a + or – flag, followed by the attribute to be added or removed. For example, this command makes the “TestFile.txt” file immutable and can no longer be deleted or modified.

chattr +i testFile.txt

For example, to remove the immutable attribute, use the following command:

chattr -i testFile.txt

File Immutability
One common use case for the chattr +i command is to make a file or directory immutable to prevent accidental deletion or modification. This can be useful for important system files or for files that are frequently backed up.

Improves Performance
Another use case for the chattr +a command is to improve the performance of your file system. The “append only” attribute (a) can be added to the files to improve the performance on the filesystems that do not support the append-only mode. This attribute causes the filesystem to write the data to the end of the file only rather than changing the existing data, which can improve the performance in certain situations. To set a file to append-only mode, use the following command:

chattr +a testFile.txt

Secure Deletion
The chattr +s command can securely erase the file when it is deleted. This attribute overwrites the file with zeros before unlinking it, making it much more difficult for someone to recover the deleted data. To set a file for secure deletion, use this command:

chattr +s testFile.txt

Conclusion

File attributes are essential to Linux systems and can provide additional functionality or security. Understanding and properly utilizing the lsattr and chattr commands can help improve your Linux system’s overall performance and security. File immutability, append-only mode, and secure deletion are examples of how the file attributes can achieve specific goals. It is recommended to use these commands with caution and after correctly understanding the file attributes and their impact on the system.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.