首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash :向bash函数添加线程功能,因此速度更快

Bash :向bash函数添加线程功能,因此速度更快
EN

Stack Overflow用户
提问于 2016-01-29 09:26:30
回答 1查看 63关注 0票数 0

我有一个bash函数,我经常使用它以一种很好的格式列出文件和目录。问题是,每次我执行函数时,大约需要2-3秒才能最终完成数据列表。

当我两次调用find命令时,需要双倍的时间。我想问一问,如何将find命令作为并行线程启动,然后得到输出。我认为这将使函数输出加载速度更快。我说错了吗?

剧本:

代码语言:javascript
复制
function lsa
{
 # Function to stat files and folders in current dir
 # Takes first argument as directory to stat
 # If no directory supplied, current dir assumed
 if [ -z "$1" ];then
   DIR="."
 else
   DIR="$1"
 fi

 # print directories first
 printf "*** DIRECTORIES *** \n"
 find "$DIR"  -maxdepth 1 -type d ! -name "."   -printf "%M %u %g "  -exec du -sh  {} \; 2> /dev/null 
 # print non-directories second
 printf "*** FILES *** \n"

 find "$DIR"  -maxdepth 1 ! -type d ! -name "."   -printf "%M %u %g "  -exec du -sh  {} \; 2> /dev/null 
 }

请告诉我。谢谢。

更新

具有更改的:

代码语言:javascript
复制
*** DIRECTORIES *** 
[1] 6882
*** FILES *** 
[2] 6883
[1]-  Done                    find . -maxdepth 1 -type d ! -name "." -printf "%M %u %g " -exec du -sh {} \; 2> /dev/null > dirs
[2]+  Done                    find "$DIR" -maxdepth 1 ! -type d ! -name "." -printf "%M %u %g " -exec du -sh {} \; 2> /dev/null > non-dirs
// And then the files and dirs one after other

没有更改的:

代码语言:javascript
复制
borg@borg-cube:~$lsa
*** DIRECTORIES *** 
// All directories
*** FILES *** 
// All files
EN

回答 1

Stack Overflow用户

发布于 2016-01-29 09:38:35

通过在后台运行这两个查找命令并将它们的输出保存到不同的文件中,您不能拥有多个multi-processes线程。然后,wait查找命令完成,并将输出连接起来(首先是dirs)。

这边..。

代码语言:javascript
复制
find .  -maxdepth 1 -type d ! -name "."   -printf "%M %u %g "  -exec du -sh  {} \; 2> /dev/null > dirs &
...
find "$DIR"  -maxdepth 1 ! -type d ! -name "."   -printf "%M %u %g "  -exec du -sh  {} \; 2> /dev/null > non-dirs &
...
wait
cat dirs non-dirs
....

**更新**

cat dirs non-dirs结尾处替换为:

代码语言:javascript
复制
printf "*** DIRECTORIES *** \n"
cat dirs
printf "*** FILES *** \n"
cat non-dirs
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35080941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档