Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Unix&Linux>>simple use of grep & cp commands question

simple use of grep & cp commands question

karmattol
05-24-2005, 01:26 PM
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...

hayne
05-24-2005, 02:11 PM
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.

msq6g
05-24-2005, 05:00 PM
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.

hayne
05-24-2005, 05:06 PM
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.


 

TOP

Windows Server Outsell
Unix Signals And C++ E

For more info

Unix Signals And C++ E
Windows Server Outsell
ssh setup for password
Bash script does not w
esc code 
ARD send unix command 
question about binarie
Scanning mail 
Issuing multiple comma
How do I install Linux

News Archive

/etc/hosts? 
Manually Start a Start
mounting missed hard d
Using Netinfo in Singl
darwin/bsd login probl
system_profiler and fi
Send mail from script 
mounting a drive 
OS X disk first aid ha
system.log shows steal

Related stories:

ditto or just drag and drop
Problem opening text files from command line using nedit
Output system.log in terminal
cron running tasks twice simultaneously!
How to figure out devicename for my CDROM
How do you mount an SMB share in terminal?
Changing my shell to Bash

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics