LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-13-2008, 03:07 PM   #1
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 43
find files with pattern, then copy those files to another directory


I am trying to find files that match a certain pattern and then copy just those files to another directory.

So far I have this, which find the files that match my pattern but I can't figure out how to then copy those to another directory.

find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \;


I have tried adding
find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" -exec cp /directory {} \;



but that fails.

I've also tried a script which fails:

#!/bin/bash
for i in find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \;
do
cp $i directory
done



but all that does is copy ALL the files into the new directory







edit:


cheap and dirty way that I figured out:


#!/bin/bash
find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \; >> filename

for b in `cat filename`
do
cp $b test
done

Last edited by ncsuapex; 06-13-2008 at 03:19 PM. Reason: .
 
Old 06-13-2008, 03:25 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by ncsuapex View Post
find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \;


I have tried adding
find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" -exec cp /directory {} \;



but that fails.
Why are you passing the -l flag to grep? What you want is for grep to exit with success if the pattern is found and exit with failure if the pattern is not found. If a success is encountered, then the remaining arguments to find are processed. Also, your cp syntax is backwards.

Try something like this:
Code:
find . -maxdepth 1 -type f -exec grep -q "pattern" '{}' ';' -exec cp '{}' /path/to/destination ';'
 
Old 06-13-2008, 03:48 PM   #3
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43
thanks

find . -maxdepth 1 -type f -exec grep -q "pattern" '{}' ';' -exec cp '{}' /path ';'


worked, I didn't know how to pass 2 exec expressions on one line.


I used the -l because I wanted just the filename not the matching lines
 
Old 06-14-2008, 07:32 AM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
Why use grep in the first place?

find . -maxdepth 1 -type f -regex "pattern" -exec cp {} /directory \;
 
Old 08-13-2010, 03:38 PM   #5
PradeepKr
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Rep: Reputation: 0
How about using xargs? Something like this.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find files and copy the found files to the floppy in one command justmehere Linux - Newbie 11 05-04-2008 11:29 PM
Select the files of a directory that match a specific pattern jianelisj Linux - Newbie 2 03-17-2008 12:25 PM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 11:51 AM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 08:19 PM


All times are GMT -5. The time now is 02:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration