in reply to grep or perl
The Unix way is to use find. (Though, of course, you're allowed to drop the backslashes and have it all on one line.) (It's a lot easier if the directory containing the files has no subdirectories.)
find $DIR \ -exec egrep 'N+' {} > /dev/null\; -o \ -exec cp {} $otherDir\;
1 - Run the find command, starting the search at the required directory
3 - After the nodes above, everyone knows how to find the files which don't contain NNNN. Instead of feeding the names to xargs, we can also implement the action in find>. The return value of egrep is true if the pattern was found, false if it wasn't found, regardless of whether options reverse the send of the search.
3b - If the pattern matches the file, we want to stop there. That's what the -o option does. We only continue further if the pattern did NOT match anywhere in the file.
4 - Copy the file to the other directory.
Note: -exec takes an ordinary command with curly braces where you would have the name of the file, and an escaped semi-colon to mark the end of the -exec.
More Notes: If the directory has subdirectories, and you don't want to descend into them, it becomes more complicated. You want to -prune directories (-type d), but that stops the search with the first thing found, unless you search depth first (-deepth>) .
|
|---|