LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-13-2005, 04:29 AM   #1
dc6463
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Rep: Reputation: 0
need script to copy all files containing a certain string to a directory


i need a script to copy all files containing a certain string (in the file) to a directory. any help would be greatly appreciated.

i got the grep part:
grep -f *.txt -e stringpattern -l

not sure how to put it together with copy.

thanks in advance,
 
Old 06-13-2005, 04:34 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Well if this works for you, then you might just add this:

grep -f *.txt -e stringpattern -l | xargs cp directoryname

What xargs does is it takes one line at a time and passes it to the command given (in this case cp.
 
Old 06-13-2005, 06:29 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
No, this won't work, because directory name has to be the last argument.
There are two possibilities:
Code:
grep ... -l | eval cp $(xargs -if echo '"f"') /destination/
or
grep ... -l | xargs tar cf - | (cd /destination/ && tar xpf -)
I think there's also a simpler way to use xargs, but I don't remember how.

Yves.
 
Old 06-13-2005, 06:49 AM   #4
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
This will work too

for Eachfile in `find . -iname "*pattern*" `
do
cp $Eachfile dirname
done

Cheers
Z
 
1 members found this post helpful.
Old 06-13-2005, 07:28 AM   #5
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Yes what zulfilee provided would be the best choice I presume. Sorry for my bad code. I typed it of my head using windows, never tried it though. It was just an idea...
 
Old 06-13-2005, 07:46 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
No need for a loop though, this command should suffice :
Code:
find . -iname "*pattern*" -exec cp {} dirname \;
 
Old 06-13-2005, 08:01 AM   #7
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
This is what I like about Linux and shell scripting. With so many different yet similar commands, you can write a script with the same output in many different ways... You have a couple of them here already.
 
Old 06-13-2005, 08:34 AM   #8
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
I thought dc6463 asked how to do it with files containing a certain string?

for file in *; do if grep -q "string" "$file";then cp "$file" directory/; fi; done
 
Old 06-13-2005, 08:53 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You right, I misread the original post...
But again, you can avoid the loop
Code:
cp `grep -il "string" *` directory
 
Old 06-13-2005, 08:53 AM   #10
dc6463
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Original Poster
Rep: Reputation: 0
thanks all for your help. that was quick.

actually, the code in the first post from marghorp worked fine -on os x at least.

now, my new problem is how to make this work on a folder of 10,000 text files without getting the 'Argument list too long." our mail server throws the backup of all outgoing mails for all users into one folder and i need a way to restore for just one user.

this script works great, but i have to split up the folder first.

any other ideas?

thanks again!

Last edited by dc6463; 06-13-2005 at 09:09 AM.
 
Old 06-13-2005, 08:57 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Why do you have to do that ? grep is too slow ?
 
Old 06-13-2005, 09:12 AM   #12
dc6463
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Original Poster
Rep: Reputation: 0
sorry, i don't know the exact reason, but i was told that the reason i get the 'Argument list too long error' is because i'm searching through too many files.

the script works fine if i run it on a directory with (a lot) fewer files in it.
 
Old 06-13-2005, 09:45 AM   #13
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
There are two advantages to the loop:
a) handles spaces
b) you avoid the argument list too long error.
 
Old 04-15-2013, 05:31 AM   #14
floorripper
Member
 
Registered: Oct 2011
Location: Central Europe
Distribution: ubuntu 14.04
Posts: 45

Rep: Reputation: Disabled
Quote:
Originally Posted by zulfilee View Post
This will work too

for Eachfile in `find . -iname "*pattern*" `
do
cp $Eachfile dirname
done

Cheers
Z

This workded super for me! Thanks!! I have just replaced pattern and dirname. Amazing, thanks!
 
Old 04-15-2013, 06:47 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Fix:

Code:
Old: grep -f *.txt -e stringpattern -l | xargs cp directoryname
New: grep -f *.txt -e stringpattern -l | xargs cp -t directoryname
PS: I don't really get this grep, I'd use this:

Code:
grep -l -e "stringpattern" -- *.txt
 
  


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
script for changing a specific string in a directory irfanhab Programming 5 06-28-2005 01:27 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 11:51 AM
Shell script to copy file name with part of directory Transition Linux - General 5 01-18-2005 05:40 PM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 06:57 AM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 08:19 PM

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

All times are GMT -5. The time now is 11:24 PM.

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