Copy 'grep' resulting files into subfolder


Don't know much about Linux, some unix long ago, so I need simple command help.

I need to copy text files containing the word 'amount'

I can get the list of files with grep:

cat *.DOC ¦ grep -i amount

An unelegant way would be making a file with the results but I need the next step: How to copy the files into a subfolder?

Thanks for the help
Who is Participating?
TintinConnect With a Mentor Commented:
Works fine for me.

-rw-r--r--   1 tintin   tintin       13 Oct 19 08:59 a.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 08:59 b.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 08:59 c.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 08:59 d.DOC
drwxr-xr-x   2 tintin   tintin      512 Oct 19 08:59 destinationfolder
-rw-r--r--   1 tintin   tintin       13 Oct 19 08:59 e.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 08:59 f.DOC

$ cp `grep -il "total amount" *.DOC` ./destinationfolder

$ ls -l destinationfolder/
total 12
-rw-r--r--   1 tintin   tintin       13 Oct 19 09:00 a.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 09:00 b.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 09:00 c.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 09:00 d.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 09:00 e.DOC
-rw-r--r--   1 tintin   tintin       13 Oct 19 09:00 f.DOC

Please post the French errors, as there are lots of non-English speakers on EE and I can speak a little French.
try this:

run it on command line in directory where you have your files:

for i in $(grep -l amount *); do cp $i /tmp; done

this will copy files with amount string to /tmp

or you can create small bash script to do that job:

hope this helps
be in the folder you are seraching for the file and do
find . -name '*doc' -exec cp {} /destenationfolder/ \;
find. -name '*doc' --> this does the same as your above ls | grep *doc

-exec is part of the find command which tells it to run a command for each result
in this case the cp {} /destenationfolder/ \; is the command
{} replaces whatever was foud so the list of the files *doc
and then just like the cp command you need a destination folder
after the destination folder make sure you include a space and the \; as its needed for the -exec portion of find.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

sorry i misread your question, the above is to copy all *doc files.

to do your following take your code and use the xargs function so
ls * doc| grep -l value * | xargs -i  cp {} /destinationfolder/

so really the xargs just does the same as the -exec option in find. it takes the list of files and replaces the {} (thats what the -i option is telling it to do)
ornicarAuthor Commented:
Luxana: I'm not sure of the linux install on the machine, so would prefer not using scripting.

Taylor: I searched for something with xargs, and would like to try your pipe.
But you use ls. I need to look in the content of the files, not the list of their names.
So, shouldn't it be cat instead of ls?

cat *.DOC | grep -l poursuite * | xargs -i  cp {} /poursuite/
awk 'BEGIN{ destination="/tmp" }
        cmd = "cp " "\047" FILENAME "\047" " " destination
}' *txt
Taylor's suggestion works perfectly. I tried it on some test files I created and it worked.
ls * doc| grep -l value * | xargs -i  cp {} /destinationfolder/
No need for the UUOLS

grep -il amount *.DOC | xargs -i  cp {} /destinationfolder/

or you could do

cp `grep -il amount *.DOC` /destinationfolder
ornicarAuthor Commented:
Hello, thanks. Here some feedback:

All commands give an error message. It is a french linux and is quite nonsense to me, surely not you.

This one works fine:
cp `grep -il amount *.DOC` /destinationfolder

but I need to add a period:

cp `grep -il amount *.DOC` ./destinationfolder

Problem is getting more complex ;)  I need to find the expression "total amount": I tried different quotes but no success. What should I use to find "total amount"??
cp `grep -il "total amount" *.DOC` ./destinationfolder


grep -il "total amount" *.DOC | xargs -i  cp {} ./destinationfolder/
ornicarAuthor Commented:
cp `grep -il "total amount" *.DOC` ./destinationfolder
yes, I tried this, but I got an error message. My system is in french. Can you try it on an english system and check if there is an error? It is saying something about quotes but I don't understand what is wrong.
ornicar was requested to post the error message he/she was receiving and has not done so.

Recommend no delete.
Whoops, missed the object button.
ornicarAuthor Commented:
I did not have the occasion to retry the command. Maybe I can accept Tintin's:

cp `grep -il amount *.DOC` /destinationfolder

Won't be an A because the first proposition didn't work.

Could we leave the question open, after all? I'm sure my client will ask the question later on and I could give the error message. But this could be in a very long time.
Vijay kumar MohanrajCloud ArchitechCommented:
this thread was helpful..
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.