Find files in multiple directories taken from list in a file?

家住魔仙堡 提交于 2019-12-02 19:37:53

问题


FreeBSD 9.2 RELEASE p2

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

/etc
/home
/home/goods/

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 file chmod +x and is 755 fromDirs.txt file chmod +x and is 755

This code give me error

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

2.sh: cannot open fromDirs.txt : No such file or directory

2.sh: ${dirs[...}: Bad substitution

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

This code give me error

FILE=fromDirs.txt
IFS='\n'
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: Syntax error: "done" unexpected (expecting "do")

This code give me error too

FILENAME=fromDirs.txt

awk '{kount++;print  kount, $0}
     END{print "\nTotal " 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?


回答1:


This works for me

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


来源:https://stackoverflow.com/questions/25821119/find-files-in-multiple-directories-taken-from-list-in-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!