LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 11-02-2010, 12:19 PM   #1
spivey76
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Using find and a pattern file


So I've tried a few things but nothing is working. I have a folder with ~9K files in it. I also have a file with about 500 file names in it, lets call it "matches.txt".

What I want is to find all the files in this folder that are listed in matches.txt and copy them to a another folder.

I can paste the various commands I've tried, but I don't want to bias any answers I can receive here. I know grep and find are essential but I'm missing something - everything I've used returns zero files copied.
 
Old 11-02-2010, 12:20 PM   #2
spivey76
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I just realized I put this under the wrong topic - apologies in advance.
 
Old 11-02-2010, 12:33 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by spivey76 View Post
I can paste the various commands I've tried, but I don't want to bias any answers I can receive here.
Chances are you will not get many replies until you do post what you have tried -- which is one way of ensuring few biassed answers!
 
Old 11-02-2010, 01:48 PM   #4
spivey76
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Well, ok. I am a newb at this but here it goes:

Code:
while read line; do find . -name $line *.txt < matchb.txt; done
That just runs, never returns anything. I was trying to just list the files before adding the copying funciton.

Code:
find . -maxdepth 1 -type f -exec grep -f matchb.txt '{}' ';' -exec cp '{}' files/ ';'
This is as far as I've come, but this just creates a file (matchb.txt) that lists the files I'm trying to copy.

Everything else has been a variation of the above to lines.
 
Old 11-02-2010, 03:23 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Try
Code:
while read -r line; do find . -name "$line" -exec cp -a '{}' /path/to/new/dir/ \; ; done < matchb.txt
[UNTESTED]
 
Old 11-03-2010, 07:01 AM   #6
spivey76
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Try
Code:
while read -r line; do find . -name "$line" -exec cp -a '{}' /path/to/new/dir/ \; ; done < matchb.txt
[UNTESTED]
Returned:
Code:
cp: files/rtf_doc_to_ascii.14244384.txt and ./files/rtf_doc_to_ascii.14244384.txt are identical (not copied).
But when I checked the output folder the files were there - so that works for me.

On a side note for anyone else: I did have to convert the pattern file from dos to unix, otherwise it wasn't being read correct.

Thanks for the help.
 
Old 11-03-2010, 07:08 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by spivey76 View Post
Returned:
Code:
cp: files/rtf_doc_to_ascii.14244384.txt and ./files/rtf_doc_to_ascii.14244384.txt are identical (not copied).
Probably because 'files' is a subfolder of the folder you are searching in.
 
  


Reply



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 a filename which contain particular pattern and want to move to gauravjain111 Linux - Newbie 4 11-09-2009 06:22 AM
find pattern in any file in directory sancho1980 Linux - Newbie 4 12-19-2006 08:14 AM
find | grep <pattern> not working duvalr Linux - Software 4 09-17-2006 04:07 AM
how to find a pattern using shell script sharad Linux - General 1 05-23-2006 03:50 AM
pattern search through find command. abhi Linux - Newbie 2 04-11-2005 10:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration