?
Solved

Copy 'grep' resulting files into subfolder

Posted on 2007-10-03
16
Medium Priority
?
4,311 Views
Last Modified: 2010-11-22
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
0
Comment
Question by:ornicar
  • 5
  • 4
  • 2
  • +4
15 Comments
 
LVL 10

Expert Comment

by:Luxana
ID: 20004675
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
 
LVL 3

Expert Comment

by:taylorludwig
ID: 20004691
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
 
LVL 3

Expert Comment

by:taylorludwig
ID: 20004728
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 9

Author Comment

by:ornicar
ID: 20004888
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20004928
awk 'BEGIN{ destination="/tmp" }
     /AAAAA/{
        cmd = "cp " "\047" FILENAME "\047" " " destination
        system(cmd)
        nextfile        
}' *txt
0
 
LVL 3

Expert Comment

by:ckhsu1977
ID: 20007738
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20010323
No need for the UUOLS

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

or you could do

cp `grep -il amount *.DOC` /destinationfolder
0
 
LVL 9

Author Comment

by:ornicar
ID: 20102578
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20103522
cp `grep -il "total amount" *.DOC` ./destinationfolder

or

grep -il "total amount" *.DOC | xargs -i  cp {} ./destinationfolder/
0
 
LVL 9

Author Comment

by:ornicar
ID: 20104083
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
 
LVL 48

Accepted Solution

by:
Tintin earned 800 total points
ID: 20104159
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
 
LVL 48

Expert Comment

by:Tintin
ID: 22054139
ornicar was requested to post the error message he/she was receiving and has not done so.

Recommend no delete.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22054144
Whoops, missed the object button.
0
 
LVL 9

Author Comment

by:ornicar
ID: 22060328
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
 
LVL 4

Expert Comment

by:mail2vijay1982
ID: 34189994
this thread was helpful..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question