Last Updated: 2023-07-28 07:52:02 Friday
-- TOC --
本文尝试总结Linux下删除文件或目录的相关命令。
rm,remove,删除文件或目录都可以
-i
,删除时,有个用户确认的prompt。
-f
,--force,如果文件不存在,不会出错,也不会prompt,脚本中常常见到此参数,不会因为prompt而终端脚本的执行。
-d
,删除空目录时使用,同rmdir
命令。
-r
,recursive rm。
非常著名的一行命令:
rm -rf /
,删除一切!
-v
,verbose。
删除文件名前有-
的文件:
$ rm -- -foo
$ rm ./-foo
注意:用rm命令删除的文件或目录,是有可能而且还挺容易恢复的!
unlink是一个系统调用,它的功能如下:
unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse.
If the name was the last link to a file but any processes still have the file open, the file will remain in existence until the last file descriptor referring to it is closed.
If the name referred to a symbolic link, the link is removed.
If the name referred to a socket, FIFO, or device, the name for it is removed but processes which have the object open may continue to use it.
unlink命令就是对unlink系统调用的简单封装。
需要注意:
删除空目录的专用命令,如果目录不为空,rmdir返回错误。
--ignore-fail-on-non-empty
,当目录不为空的时候,不返回错误,script saver。
-p
,类似mkdir -p
,如果为目录为空,连同ancestor一同删除。
$ mkdir -p abc/123/jkl
$ rmdir -p abc/123/jkl
glibc中有个接口叫remove,当remove single file的时候,它其实call unlink,当是个目录时,它call rmdir。
Linux下的文件粉碎机!
shred命令是通过对文件进行重复随机写入的方式,来达到粉粹文件的效果。
$ shred -n4 -xzv tt.txt
shred: tt.txt: pass 1/5 (random)...
shred: tt.txt: pass 2/5 (ffffff)...
shred: tt.txt: pass 3/5 (000000)...
shred: tt.txt: pass 4/5 (random)...
shred: tt.txt: pass 5/5 (000000)...
-v
,verbose
-n
,指定重复覆盖写入的次数,默认为3次,如上。
-z
,追加一次写入全0的pass,如上。
-x
,exact,不执行round up,使file size包含最后的full block,如果不使用这个参数,shred后文件的size会变大。
-u
,shred后删除文件,如果没有这个参数,shred只是粉碎文件内容,不删除文件本身。对device file
操作时,就不要有这个参数了。
$ shred -v -u tt.txt
shred: tt.txt: pass 1/3 (random)...
shred: tt.txt: pass 2/3 (random)...
shred: tt.txt: pass 3/3 (random)...
shred: tt.txt: removing
shred: tt.txt: renamed to 000000
shred: 000000: renamed to 00000
shred: 00000: renamed to 0000
shred: 0000: renamed to 000
shred: 000: renamed to 00
shred: 00: renamed to 0
shred: tt.txt: removed
removing后面的几行打印,貌似是shred在重复改写directory file中的file name!这删的够彻底呀...:)
-s
,指定一个shred的size
--random-source=FILE
,指定一个file来作为random数据的来源
-f
, change permissions to allow writing if necessary
将整个device清0,我以前常用dd命令。其实shred命令也可以,网上说shred速度更快,个人认为是个错误,shred默认都要写3次。
融掉硬盘是清除数据最好的选择!
https://www.gnu.org/software/coreutils/manual/html_node/shred-invocation.html
The best way to remove something irretrievably is to destroy the media it’s on with acid, melt it down, or the like. For cheap removable media this is often the preferred method. However, some storage devices are expensive or are harder to destroy, so the shred utility tries to achieve a similar effect non-destructively, by overwriting the file with non-sensitive data.
shred也不是万能,shred命令有个假设,即shred relies on a crucial assumption: that the file system and hardware overwrite data in place. 有些文件系统并不能满足这个假设,这是否就是shred命令要覆写多次的原因呢!?
在不能把硬盘用酸水融化掉的情况下,shred是个比较好的选择。
本文链接:https://cs.pynote.net/hd/hdisk/202306291/
-- EOF --
-- MORE --