0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ファイル属性を変更して、ファイルを削除できないようにする

Last updated at Posted at 2022-05-28

環境

Ubuntu20.4

はじめに

rmをして、思わず、必要なファイルを削除してしまった経験はないだろうか。そのような事故を未然に防止するために、ファイル属性を変更して対応することができます。ファイルを削除しようとしても、削除できないようにするためのファイル属性があり、その設定を変更して対応することができます。

ファイル属性を確認するコマンドは、lsattrコマンドです。そして、ファイル属性を変更するコマンドは、chattrコマンドです。2つのコマンドを実行して、動作を確認してみましょう。

やったこと

空ファイルを作成します。

touch test.txt

作成した、test.txtのファイル属性を確認します。デフォルトではこのように表示されます。削除できない属性は設定されていない状態です。

lsattr test.txt
--------------e----- test.txt

test.txtを削除できないファイル属性に変更します。変更は管理者権限でないと変更できません。

sudo chattr +i test.txt

このようにiが付きました。iが付くと、削除できないファイル属性になっていることを表しています。

lsattr test.txt
----i---------e----- test.txt

削除します。しかし、確かに削除ができません。

rm test.txt
rm: cannot remove 'test.txt': Operation not permitted

ファイルを削除できないファイル属性を取り除いて、元の状態に戻します。

sudo chattr -i test.txt

確かに、iの表示が消えました。

lsattr test.txt
--------------e----- test.txt

test.txtを削除しようとすると、削除されるようになりました。

rm test.txt

補足

lsattr、chattrは、e2fsprogsという、ext2/ext3/ext4ファイルシステムを操作できるユーティリティに含まれるコマンドです。上の記事で、ファイル属性という表現を使ったが、もう少し詳しくいえば、ファイルシステムのアトリビュート項目という言い方もできます。アトリビュートでiをたてれば、ルート権限でrmしても削除ができなくなります。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?