Linux lsattr Command

Linux 命令大全Linux Commands Encyclopedia

The Linux lsattr command is used to display file extended attributes.

usechattrTo change the attributes of files or directories, you can run the lsattr command to query their attributes.

Syntax

lsattr [选项] [文件或目录...]

Common Options:

  • -R: Recursively display attributes of all files in a directory and its subdirectories
  • -a: Display all files, including hidden files (files beginning with.a dot)
  • -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 an attribute(similar tols -lpermission bits).
  • -- Indicates that the attribute is not set,Letters indicate that it is set(such asi、a)。
  • common attributes(corresponding tochattrchattr):
    • s: secure deletion (secure deletion)
    • u: undeletable (undeletable)
    • c: compressed (compressed)
    • S: synchronous updates (synchronous updates)
    • i: immutable (immutable)
    • a: append only (append only)
    • d: no dump (no backup)
    • A: no atime updates (do not update access time)
    • I: indexed directory (indexed directory)
    • j: data journalling (data journal)
    • t: no tail-merging (no tail merging)
    • T: top of directory hierarchy (top of directory hierarchy)
    • e: extent format (extent format)

Examples

1. View the attributes of a single file

lsattr /etc/passwd

Example output:

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

iIndicates that the file cannot be modified/deleted.

2. View a directory and its contents

lsattr -R /var/log/  # 递归查看 /var/log/ 下所有文件

3. View only the attributes of the directory itself (do not display child files)

lsattr -d /tmp/

4. Display the attributes of hidden files

lsattr -a ~/.bashrc

5. Use in combination with chattr

sudo chattr +i important_file.txt  # 设置为不可变
lsattr important_file.txt          # 检查是否生效

Output:

----i--------- important_file.txt

Linux 命令大全Linux Commands Encyclopedia

Other Extensions