Linux lsattr Command
The Linux lsattr command is used to display file attributes.
Using chattr After changing the attributes of a file or directory, you can use the lsattr command to query its attributes.
Syntax
lsattr [选项] [文件或目录...]
Common options:
- -R: Recursively display the attributes of all files in a directory and its subdirectories
- -a: Display all files, including hidden files (files starting with .)
- -d: Display the attributes of the directory itself, rather than its contents
- -v: Display file version information
- -l: Display attribute names in long format
After running lsattr, the output format is usually as follows:
----i--------- /etc/passwd
- Each column represents one attribute(similar to the
ls -lpermission bits). -indicates the attribute is not set, a letter indicates it is set(such asi,a).- Common Attributes(corresponding to
chattr): s: secure deletionu: undeletablec: compressedS: synchronous updatesi: immutablea: append onlyd: no dumpA: no atime updatesI: indexed directoryj: data journallingt: no tail-mergingT: top of directory hierarchye: extent format
Example
** 1. View the attributes of a single file**
lsattr /etc/passwd
Example output:
----i--------- /etc/passwd
i indicates the file cannot be modified or deleted.
2. View a directory and its contents
lsattr -R /var/log/ # 递归查看 /var/log/ 下所有文件
3. View only the directory's own attributes (not sub-files)
lsattr -d /tmp/
4. Display the attributes of hidden files
lsattr -a ~/.bashrc
** 5. Use with chattr**
sudo chattr +i important_file.txt # 设置为不可变
lsattr important_file.txt # 检查是否生效
Output:
----i--------- important_file.txt
Linux Command Reference