How to completely delete files in linux

1. Use the rm command to delete

  1. Use rm directly
    and first use ls -alt to check the file information and owner.
    You can see that the owner is the eve user, so just run the rm command in the eve user's terminal.
    If it is root or other, use root or other accounts first. Delete
(base) eve@Eve:~$ ls -alt a.txt
-rw-rw-r-- 1 eve eve 0 226 16:17 a.txt
(base) eve@Eve:~$ rm a.txt
(base) eve@Eve:~$
  1. If it is a folder deletion, you need rm -rf
(base) eve@Eve:~$ rm -rf a
(base) eve@Eve:~$

2. If rm cannot be deleted, you need to use chattr to modify the extended attributes of the file.

The phenomenon is as follows: Even if you use administrator rights or root rights, you cannot delete it.

(base) eve@Eve:~$ rm a.txt
rm: cannot remove 'a.txt': Operation not permitted
(base) eve@Eve:~$ sudo rm a.txt
rm: cannot remove 'a.txt': Operation not permitted
(base) eve@Eve:~$ su root
Password:
root@Eve:/home/eve# rm a.txt
rm: cannot remove 'a.txt': Operation not permitted

At the same time, you can see the attributes of a.txt as follows:

root@Eve:/home/eve# ls -alt a.txt
-rw-rw-r-- 1 eve eve 0 226 15:58 a.txt

3. Solution:

lsattr View file extension attributes

root@Eve:/home/eve# lsattr a.txt
----i---------e----- a.txt
说明:(更多说明参考四:lsattr及chattr介绍)
i:设置文件或目录不可修改、不可删除、不可重命名、不可移动;
e:设置文件或目录只在系统重新启动后才能访问。

The solution is to modify and delete the i attribute through the chattr command.

(base) eve@Eve:~$ sudo chattr -i a.txt  
(base) eve@Eve:~$ lsattr a.txt
--------------e----- a.txt
(base) eve@Eve:~$ rm a.txt
(base) eve@Eve:~$

Note: chattr can only be modified with administrator privileges, so
after adding sudo to modify it, you can view the modified extended permissions through lsattr and no longer have i, so deleting rm can delete it successfully.

Note: If chattr removes the i attribute, an error will still be reported, such as:

(base) eve@Eve:~$  sudo chattr -i a.txt
Usage: chattr [-RVf] [-+=aAcCdDeijsStTu] [-v version] files...

It means there is a problem with the chattr command. It is recommended to download the source code from github , or download the source code from here :
Then execute gcc chattr.c -o chattr.out
and then use the generated chattr.out to delete the i attribute. The command reference is as follows:

(base) eve@Eve:~$  gcc chattr.c -o chattr.out
(base) eve@Eve:~$ ls
chattr.out
(base) eve@Eve:~$ sudo ./chattr.out -i a.txt

Execute the deletion again to delete successfully:

(base) eve@Eve:~$ rm a.txt
rm: cannot remove 'a.txt': Operation not permitted

4. Introduction to lsattr and chattr

lsattr 命令是用于显示 Linux 文件和目录的扩展属性的工具。
这些属性是文件系统提供的一种机制,用于控制文件和目录的访问、更改和删除等行为。

lsattr 命令的常用选项如下:

-a:显示隐藏文件和目录的属性;
-d:如果指定的参数是目录,那么只显示目录的属性;
-R:递归显示目录及其子目录中的所有文件和目录的属性;
-v:显示每个文件或目录的版本号。
常用的文件或目录属性如下:

i:设置文件或目录不可修改、不可删除、不可重命名、不可移动;
a:设置只能在文件或目录中添加内容,不能修改和删除;
c:设置自动压缩文件或目录;
d:设置目录被删除时,其内容应该被保留在磁盘上,但不可访问;
s:设置文件或目录在被删除时,其空间将被清空,但数据可以恢复;
u:设置文件或目录被删除时,可以通过恢复工具来恢复;
e:设置文件或目录只在系统重新启动后才能访问。
这些属性可以通过 chattr 命令进行设置和修改。例如,chattr +i filename 将文件 filename 的属性设置为不可修改、不可删除、不可重命名、不可移动。

chattr 是 Linux 系统下的一个命令,用于修改文件或目录的属性。chattr 命令可用于设置或清除某些扩展属性,这些属性有助于在文件或目录中启用或禁用某些高级功能。chattr 命令只能被 root 用户或具有 CAP_LINUX_IMMUTABLE 许可的用户执行。

chattr 命令的一些常用选项和属性如下:

-R 递归地更改目录及其下面的所有文件和子目录的属性。
-v 显示修改属性的详细信息。
+ 启用某个属性。
- 禁用某个属性。
= 设置属性,删除不在命令行中指定的所有属性。
a 使文件或目录只能以追加方式打开(只允许在文件末尾添加数据)。
c 允许文件自动压缩。
d 允许文件被写入后进行同步写入。
i 防止文件被修改、重命名、删除或链接。
j 允许文件被写入后进行同步写入,但仅适用于 Ext3 和 Ext4 文件系统。
s 允许文件被安全地删除。
u 允许文件被恢复。
例如,要将 /var/log/messages 文件设置为不允许被修改,可以使用以下命令:

css
Copy code
sudo chattr +i /var/log/messages
如果要取消该属性,则可以使用以下命令:

css
Copy code
sudo chattr -i /var/log/messages

Guess you like

Origin blog.csdn.net/sinat_29891353/article/details/129227812