Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux lsattr Command

Linux lsattr Command

Linux Command Reference Linux Command Reference

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:

After running lsattr, the output format is usually as follows:



----i--------- /etc/passwd

- `s`: secure deletion - `u`: undeletable - `c`: compressed - `S`: synchronous updates - `i`: immutable - `a`: append only - `d`: no dump - `A`: no atime updates - `I`: indexed directory - `j`: data journalling - `t`: no tail-merging - `T`: top of directory hierarchy - `e`: 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 Linux Command Reference