Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-13-2008, 03:07 PM   #1
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:

for i in find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \;
cp $i directory

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


cheap and dirty way that I figured out:

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

for b in `cat filename`
cp $b test

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

Rep: Reputation: 76
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:
find . -maxdepth 1 -type f -exec grep -q "pattern" '{}' ';' -exec cp '{}' /path/to/destination ';'
Old 06-13-2008, 03:48 PM   #3
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43

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
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
LQ Newbie
Registered: Aug 2010
Posts: 6

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


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 AM.

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