从文件中的列表中查找多个目录中的文件? [英] Find files in multiple directories taken from list in a file?

查看:16
本文介绍了从文件中的列表中查找多个目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FreeBSD 9.2 RELEASE p2

我有一个文件 fromdirs.txt.在这个文件中是一个新行分隔的目录列表,如下所示:

I have a file fromdirs.txt. In this file is a new line separated directory list like so:

/etc
/home
/home/goods/

我需要首先在所有目录的文件中找到名称为good"或包含字符串(NODES_'TASK')"的文件,然后将所有这些文件复制到目录/tmp中.

I need to first find in all directory's files which have names like "good" or contain string "(NODES_'TASK')" and then copy all these files into the directory /tmp.

2.sh 文件 chmod +x 是 755fromDirs.txt 文件 chmod +x 是 755

2.sh file chmod +x and is 755 fromDirs.txt file chmod +x and is 755

这段代码给我错误

IFS=$'
' read -d '' -r -a dirs < fromDirs.txt
find "${dirs[@]}" -type f ( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} ; ) -exec cp {} /tmp/ ;

2.sh: 无法打开 fromDirs.txt : 没有那个文件或目录

2.sh: ${dirs[...}: 替换错误

但是文件 fromDirs.txt 存在并且 2.sh 从它的分配目录中运行,我也尝试提供完整路径而不是 fromDirs.txt 并且错误相同

But File fromDirs.txt exist and 2.sh running from it allocation directory, also i trying to provide full path instead of fromDirs.txt and error the same

这段代码给我错误

FILE=fromDirs.txt
IFS='
'
while read -r dirs
do
find "$dirs" -type f ( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} ; ) -exec cp {} /tmp/ ;
done < "$FILE"

2.sh: 6: 语法错误:done"意外(期待do")

这段代码也给我错误

FILENAME=fromDirs.txt

awk '{kount++;print  kount, $0}
     END{print "
Total " kount " lines read"}' $FILENAME

2.sh: : not found awk: can't open file fromDirs.txt source line number 2

那么如何逐行读取文件并执行我需要的操作?

So how to read file line by line and do what i need?

推荐答案

这对我有用

 for line in "`cat fromDirs.txt`"; do find "$line" -type f ( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} ; ) -exec cp {} /tmp/ ;; done

这篇关于从文件中的列表中查找多个目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆