Find files in multiple directories taken from list

2020-05-07 11:13发布

问题:

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