Go Back   The macosxhints Forums > OS X Help Requests > UNIX - Newcomers



Reply
 
Thread Tools Rate Thread Display Modes
Old 05-24-2005, 01:26 PM   #1
karmattol
Prospect
 
Join Date: Dec 2002
Location: Virginia
Posts: 2
simple use of grep & cp commands question

Hello,

A Unix question born of ignorance:

I want to search through some files for a particular word and then copy those files to a new folder. So I tried....

grep -al theSearchWord sourceFolder | cp * targetFolder

That didn't work. So I tried...

cp 'grep -al theSearchWord sourceFolder' targetFolder

That didn't work. I'm sure there's a simple answer, but I'm not familiar enough with Unix to find the simple answer.

I'm running OSX10.3.9 with Terminal straight out the box. I wouldn't know, nor necessarily want to know, how to modify it.

Thanks in advance...
karmattol is offline   Reply With Quote
Old 05-24-2005, 02:11 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,935
Quote:
Originally Posted by karmattol
cp 'grep -al theSearchWord sourceFolder' targetFolder

You are close. What you want is for the output of the 'grep' command to be used as the arguments to the 'cp' command. But you used single-quotes in the above. Single-quotes are used when you want something to be taken literally. To get something to be executed, you need to use back-quotes (`) - the character in the top left of standard US keyboards.
And of course you need to supply the names of the files to be searched, not the folder.

But since there is usually a limit to the length of an argument list, that won't work if the number of matching files gets large. If that happens you will need to learn about 'xargs'.

But there is another possible problem - if any of the filenames contain spaces. Again, we'll cross that bridge as we come to it.
hayne is offline   Reply With Quote
Old 05-24-2005, 05:00 PM   #3
msq6g
Triple-A Player
 
Join Date: Jul 2002
Location: Virginia
Posts: 106
Thanks (the login name changes because it's linked somehow to the different computers that I use).

I tried the backtick instead the quote and it worked like a charm.
msq6g is offline   Reply With Quote
Old 05-24-2005, 05:06 PM   #4
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,935
Quote:
Originally Posted by msq6g
the login name changes because it's linked somehow to the different computers that I use

If I understand you correctly, you have more than one account on these forums. Your computer retains a cookie that automatically logs you in to the forums without asking for a username or password.
It would be better if you decided which of your accounts you wanted to use and then used that account on all computers. All you would have to do is use the "logout" link that is at the top right of each page. Then log in using the account you have decided to use. If you have forgotten the password for an account, there is a provision to get a new password emailed to you - you'll see this if you mistype the password.
hayne is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 03:01 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.