Linux lsattr Command
The Linux lsattr command is used to display file attributes.
To change the attributes of a file or directory with chattr, you can use the lsattr command to query its attributes.
Syntax
lsattr [-adlRvV][file or directory...]
Parameters:
-a Displays all files and directories, including those with names starting with ".", the current directory ".", and the parent directory "..".
-d Displays the directory name rather than its contents.
-l This parameter currently has no effect.
-R Recursively processes all files and subdirectories under the specified directory.
-v Displays the version of the file or directory.
-V Displays version information.
Examples
- Use the chattr command to prevent a critical file in the system from being modified:
# chattr +i /etc/resolv.conf
Then, commands like mv /etc/resolv.conf will result in "Operation not permitted".
When editing the file with vim, it will prompt "W10: Warning: Changing a readonly file". To modify this file, the i attribute must be removed:
chattr -i /etc/resolv.conf
Use the lsattr command to display the file attributes:
# lsattr /etc/resolv.conf
The output will be:
----i-------- /etc/resolv.conf
- Allow a file to only have data appended to it but not deleted, suitable for various log files:
# chattr +a /var/log/messages