linux 下删除 几百G 大小的目录,快速方法

890 views
Skip to first unread message

flyer

unread,
Feb 20, 2013, 7:19:42 AM2/20/13
to 西电开源社区邮件列表
假设有一个目录 testdir/ 大小是 700G,通过命令行,有没有较为快速的方法 (比如 30s 内) 删除掉这个目录?

通过 'rm' 命令删除时,得运行几分钟,有没有通过组合 bash 命令的方法快速删除这样的目录?

--
宠辱不惊,闲看庭前花开花落;去留无意,漫随天边云卷云舒。

linux0ne

unread,
Feb 20, 2013, 8:12:48 AM2/20/13
to xidian...@googlegroups.com
简单,这个方法是我在linux公社上学得。
 
对付这样的谁的需要用到rsync这个软件,别的都是浮云:

1、sudo aptitude install rsync
2、touch /root/empty      建立一个空目录
3、rsync --delete-before -d --progess --stats /root/empty /root/data     用empty这个空目录来删除data这个目录   

蒋颿

unread,
Feb 20, 2013, 8:41:04 AM2/20/13
to xidian...@googlegroups.com
the log writer process may take a large amount of time

// initialization
> mkfs.something /dev/device
> mount /dev/device "$dir"
// when you want to delete it:
> umount "$dir"
// re-init
> mkfs.something /dev/device
> mount /dev/device "$dir"

or just > mv files to /dev/null ;P

and another trick with no change of the fstab "data=writeback" :
> mkdir /home/emptydir/
> rsync --delete-before -a -H -v --progress --stats  /home/emptydir/  /targetdir/


--
您收到此邮件是因为您订阅了“西电开源社区”邮件列表。
要向此邮件列表发帖,请发送电子邮件至 xidian...@googlegroups.com
要取消订阅,请发送电子邮件至 xidian_linux...@googlegroups.com
请通过 https://groups.google.com/group/xidian_linux?hl=zh-CN 访问此网上论坛。
通过 [ipv6 enabled] http://xdlinux.info/http://xdl.in/
[ipv4 only] http://linux.xidian.edu.cn/
[手机]:http://m.xdlinux.info/
访问西电开源社区。
---
您收到此邮件是因为您订阅了 Google 网上论坛的“西电开源社区邮件列表”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 xidian_linux...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 

flyer

unread,
Feb 20, 2013, 9:09:53 AM2/20/13
to xidian...@googlegroups.com
开眼了……

我周末时尝试下这两种方法:
  • 把文件 mv 到 /dev/null 中。有个疑惑,这会比 rm 快吗?如果是,原理是什么?
  • 用 rsync 试试

挂载目录的方法有些看不懂……

2013/2/20 蒋颿 <fan.t...@gmail.com>

shanzi

unread,
Feb 20, 2013, 9:20:02 AM2/20/13
to xidian...@googlegroups.com
删除文件只删除了文件的控制块,rm -r 的时候是递归删除,所以其实不在于文件夹的大小,而是文件的数量,几百G如果只是一个文件,删除的速度和一个小文件是一样的。

mv则不是了,如果在同一个介质内,移动一个目录也是仅仅修改根目录的文件控制块,与子目录无关,当然速度很快咯。如果跨介质移动就不行了

rsync这个不清楚为何,原则上应该没有mv的速度快把?

如果机器能重启,移动到tmp文件夹说不定也可以。

-- 
shanzi
Sent with Sparrow

蒋颿

unread,
Feb 20, 2013, 9:39:58 AM2/20/13
to xidian...@googlegroups.com
Excuse me, moving file to /dev/null is just a joke, it's only for file not directory.We usually cat /dev/null > a_logfile to clean it all over.
The mount&umount way is just like "mv you dir to /dev/null" so that you can re-init it like a device.
U'd better use rsync for security.

flyer

unread,
Feb 20, 2013, 10:25:42 AM2/20/13
to xidian...@googlegroups.com
刚又查看 linux 创建、删除文件的原理,找到了这两篇文章:Linux下删除文件原理及恢复 和 linux下文件删除的原理精华讲解

看来删除时是与目录中文件个数有很大关系,谢谢 shanzi 提醒。

但查看 linux 下创建、删除文件原理时,发现通过 rm 删除服务器上文件时其实是不安全的,参见 Web服务器磁盘满故障深入解析 这篇文章。
不知道通过 rsync 实现删除文件时会不会解决这个问题?

2013/2/20 蒋颿 <fan.t...@gmail.com>
Excuse me, moving file to /dev/null is just a joke, it's only for file not directory.We usually cat /dev/null > a_logfile to clean it all over.
The mount&umount way is just like "mv you dir to /dev/null" so that you can re-init it like a device.
U'd better use rsync for security.

--
您收到此邮件是因为您订阅了“西电开源社区”邮件列表。
要向此邮件列表发帖,请发送电子邮件至 xidian...@googlegroups.com
要取消订阅,请发送电子邮件至 xidian_linux...@googlegroups.com
请通过 https://groups.google.com/group/xidian_linux?hl=zh-CN 访问此网上论坛。
通过 [ipv6 enabled] http://xdlinux.info/http://xdl.in/
[ipv4 only] http://linux.xidian.edu.cn/
[手机]:http://m.xdlinux.info/
访问西电开源社区。
---
您收到此邮件是因为您订阅了 Google 网上论坛的“西电开源社区邮件列表”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 xidian_linux...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 

flyer

unread,
Feb 20, 2013, 10:43:15 AM2/20/13
to xidian...@googlegroups.com
刚做了一个简单的实验,对 360M 大小、含有 430 个文件的目录通过 rm 和 rsync 两种方式进行删除,结果有些意外:
  • 通过 rm 耗时 0.355s
  • 通过 rsync 耗时 0.919s
看来得对通过 rsync 删除目录的方法进一步验证效率。

另外,再问下 linux 下 cp 的原理,谢谢。

2013/2/20 flyer <flye...@gmail.com>

蒋颿

unread,
Feb 20, 2013, 11:04:03 AM2/20/13
to xidian...@googlegroups.com
终于把拼音搞定了……,话说430个文件的测试量可能不够,之前有rm过一个存放了5G不到的爬虫数据的目录,用了少说有一分钟,rsync应该不需要这么久。
至于rm服务器文件不安全,这个感觉一般rm之前之后考虑一下进程占用应该问题不大,lsof | grep deleted一下,找出占用已rm文件的进程来kill掉。
而且这个正是之前说的cat /dev/null > a.log来代替rm的意义所在。

蒋颿

unread,
Feb 20, 2013, 11:12:11 AM2/20/13
to xidian...@googlegroups.com
这个文章里有cp等操作的模拟源码,结合linuxFS来看应该不错


在 2013年2月20日下午11:43,flyer <flye...@gmail.com>写道:

flyer

unread,
Feb 20, 2013, 11:14:06 AM2/20/13
to xidian...@googlegroups.com
那篇文章我搜到了,它实现 cp 本质是又重新建了相应的 blocks,不知道 linux 下实现 cp 的原理和这一样,还得找源码。

2013/2/21 蒋颿 <fan.t...@gmail.com>

蒋颿

unread,
Feb 20, 2013, 11:24:47 AM2/20/13
to xidian...@googlegroups.com
http://git.savannah.gnu.org/cgit/coreutils.git
源码有特别多参数可能不太方便阅读吧,单一个cp.c就有1176行

flyer

unread,
Feb 20, 2013, 11:42:56 AM2/20/13
to xidian...@googlegroups.com
大概看了一下,但总体感觉不像是那篇文章中讲的 cp 的实现方法。

coreutils 文件中 "cp.c" 中 "do_copy()" 函数,它引用 "copy.c" 中的 "copy()" 函数,而这个函数引用本文件中的 "copy_internal()" 函数。所以关键是理解 "copy.c" 中 "copy_internal()" 的实现。
周末时研究下。

2013/2/21 蒋颿 <fan.t...@gmail.com>
Reply all
Reply to author
Forward
0 new messages