Copy 'grep' resulting files into subfolder

Hello,

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
LVL 9
ornicarAsked:
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.
0
 
LuxanaCommented:
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:
http://www.linuxconfig.org/Bash_scripting_Tutorial#Bash_for_loop

hope this helps
0
 
taylorludwigCommented:
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.
0
Get your problem seen by more experts

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

 
taylorludwigCommented:
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)
0
 
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/
0
 
ghostdog74Commented:
awk 'BEGIN{ destination="/tmp" }
     /AAAAA/{
        cmd = "cp " "\047" FILENAME "\047" " " destination
        system(cmd)
        nextfile        
}' *txt
0
 
ckhsu1977Commented:
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/
0
 
TintinCommented:
No need for the UUOLS

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

or you could do

cp `grep -il amount *.DOC` /destinationfolder
0
 
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"??
0
 
TintinCommented:
cp `grep -il "total amount" *.DOC` ./destinationfolder

or

grep -il "total amount" *.DOC | xargs -i  cp {} ./destinationfolder/
0
 
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.
0
 
TintinCommented:
ornicar was requested to post the error message he/she was receiving and has not done so.

Recommend no delete.
0
 
TintinCommented:
Whoops, missed the object button.
0
 
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.
0
 
Vijay kumar MohanrajCloud ArchitechCommented:
this thread was helpful..
0
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.