2
FreeBSD 9.2 RELEASE p2

I have a file dirs.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.

I think must be something like:

$ find $fromDirs.txt -type f -name 'good' | \
    grep -lr '\(NODES_\'TASK\'\)' $fromDirs.txt | > /tmp
2
  • 1
    files with name like good OR containing NODES_TASK ? or files with name like good AND containing NODES_TASK ? Commented Sep 12, 2014 at 12:52
  • files with name like good OR containing NODES_TASK, NODES_TASK with specials simbols Commented Sep 12, 2014 at 13:01

1 Answer 1

3

From my understanding, you have a file containing a list of directories. You want all files that either:

  1. are inside a directory in the list where the file content contains (NODES_'TASK')
  2. are inside a directory in the list where the file name contains good.

and then copy these matching files to /tmp.

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

This reads fromDirs.txt delimited by newline into the array $dirs.
The find then looks through those directories, if any of the directories in the path contain good, or if the file contains (NODES_'TASK'), then copy that file into /tmp.

(note: I am using a few bash-specific features here for splitting the file list by newlines)

6
  • are inside a directory in the list where the file name like good. not directory name. /home/goods/good.php, /home/goods/verygood.php, /home/goods/gooddness.php Commented Sep 12, 2014 at 13:12
  • @DmitrijHolkin adjusted. Commented Sep 12, 2014 at 13:14
  • are inside a directory in the list where the file content contains (NODES_'TASK') not grep txt file but grep in locations which specified in txt file take first dir from txt file for example /home/goods/ when grep all files in this directory and subfolders for nodes_task when do the same with another directory from txt file Commented Sep 12, 2014 at 13:26
  • fixed a few typos & minor mistakes. Created a test environment, and it works fine now. Commented Sep 12, 2014 at 13:56
  • 2.sh: cannot open fromDirs.txt : No such file or directory 2.sh: ${dirs[...}: Bad substitution File exist and running from it allocation directory, also i trying to provide full path instead of fromDirs.txt and error the same Commented Sep 12, 2014 at 14:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.