viv*_*vek 6 linux concurrency ubuntu
我在同一级别有两个目录,我可以这样做:
rm -rf dir1/; rm -rf dir2/
Run Code Online (Sandbox Code Playgroud)
但它们将按顺序运行,我怎么能并行删除它们?是否有一个通用的解决方案,允许我扩展到许多文件夹?
更新
目录可能深层嵌套,包含其他目录,依此类推.
Raj*_*aju 10
在后台运行命令
rm -rf dir &; rm -rf dir2 &;
Run Code Online (Sandbox Code Playgroud)
句法
long_command with arguments > redirection &
Run Code Online (Sandbox Code Playgroud)
您可以通过将命令输出重定向到文件来捕获任何消息.
这个链接将有助于==> http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
问题标题和给出的例子给人的印象就是问题非常小.但是额外的赏金表明了问题的严重性.
如果指定文件的性质会更好.但是,我提供了一些基于拆分的删除,它可以作为并行执行实现您可以根据您的要求尝试以下选项.
find /yourpath/folder1 -size +1048576 -exec rm -f {} \; &
find /yourpath/folder2 -size +1048576 -exec rm -f {} \; &
Run Code Online (Sandbox Code Playgroud)
ls -l /yourpath/folder1 | awk '{print $9}' | awk -F. '{print $(NF)}' |sort |uniq
Run Code Online (Sandbox Code Playgroud)
你可能得到像这样的结果
.txt
.log
.tmp
.zip
Run Code Online (Sandbox Code Playgroud)
现在,删除基于扩展名的文件
find yourpath/folder1 -name '*.txt' -exec rm {} \; &
find yourpath/folder1 -name '*.tmp' -exec rm {} \; &
find yourpath/folder1 -name '*.log' -exec rm {} \; &
find yourpath/folder2 -name '*.txt' -exec rm {} \; &
find yourpath/folder2 -name '*.tmp' -exec rm {} \; &
find yourpath/folder2 -name '*.log' -exec rm {} \; &
Run Code Online (Sandbox Code Playgroud)
find yourpath/folder1 -mtime +5 -exec rm {} \;
Run Code Online (Sandbox Code Playgroud)
要么
find yourpath/folder2 -mtime +5 |xargs rm
Run Code Online (Sandbox Code Playgroud)
find foldername -exec rm -rf {} \; &
Run Code Online (Sandbox Code Playgroud)
如果你想要做的不仅仅是并行删除目录,你可以用GNU parallel做很多平行的花哨的东西.由于它通常不是发行版中的基础实用程序,您可能需要使用您喜欢的包管理器安装它,例如apt-get install parallel.
但是,你可以做这样很酷的事情,比如你运行4个并行进程,想要显示进度,没有唠叨通知,并且让并行运行睡眠命令,每个等待5s,10s,15s,20s.
$ parallel -j 4 --progress --no-notice sleep ::: 5 10 15 20
Computers / CPU cores / Max jobs to run
1:local / 4 / 4
Computer:jobs running/jobs completed/%of started jobs/Average seconds to complete
local:0/4/100%/5.0s
Run Code Online (Sandbox Code Playgroud)
你的例子会像这样运行:
$ parallel --no-notice rm -rf ::: dir1 dir2 dir3
Run Code Online (Sandbox Code Playgroud)
随意咨询精细教程.