有時身為 root 使用者時,還是會有不小心誤刪檔案,以防被自己雷才有這篇 …

在 Linux 中有一個指令 chattr 可以改變檔案屬性;還有另外一個指令 lsattr 可以列出檔案屬性(檔案屬性並不是檔案權限,在 ls 中看不出來)

attributes 屬性

總共有這些屬性,詳細請看手冊

a: append only
A: no atime updates
c: compressed
C: no copy on write
d: no dump
D: synchronous directory updates
e: extent format
i: immutable
j: data journalling
P: project hierarchy
s: secure deletion
S: synchronous updates
t: no tail-merging
T: top of directory hierarchy
u: undeletable

我自己常用的屬性是 a, i

a 屬性

  • 只能以追加模式寫入檔案

i 屬性

  • 不能被刪除、重新命名
  • 不能建立 link
  • 不能修改 metadata
  • 不能寫入檔案

chattr

語法如下

chattr [ -RVf ] [ -v version ] [ -p project ] [ +-=[attributes] ] files...
  • + : 是添加選定的屬性
  • - : 是移除選定的屬性
  • = : 是指定選定的屬性

後記

除了給予檔案屬性來保護外,另外也可以使用 safe-rm 來避免發生誤刪的情況