LinuxQuestions.org
Help answer threads with 0 replies.
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-29-2005, 02:19 PM   #1
NiallC
Member
 
Registered: Feb 2003
Distribution: Suse 8.1
Posts: 102

Rep: Reputation: 15
help with find and move


I'm trying to find a string of text within a file in a directory and then copy that file out to another directory if there's a hit.

But I don't know where to go from here. I'd like it to copy only the files where there are "hits" to another directory that I'd specify.

Any pointers?

TIA

NiallC
 
Old 06-29-2005, 02:45 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Code:
cp `grep -l 'string' *` other/directory
 
Old 06-29-2005, 03:33 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Note that the difference between ` and ' is very important.

Note also that a "copy" (cp), not a "move" is specified. This is probably a "good thing" to use while verifying that you've "got it right." In fact, a copy to ~/tmp" for a small set of file might be a good check before a "real" mv.
 
Old 07-01-2005, 03:10 PM   #4
NiallC
Member
 
Registered: Feb 2003
Distribution: Suse 8.1
Posts: 102

Original Poster
Rep: Reputation: 15
Great - thank you for the pointers. Howevwer, the command I tyoed as follows:

bash-2.05b# cp `grep -l 'wnix' *` /root/Desktop/new/

produced the following error:

bash: /usr/bin/grep: Argument list too long
cp: missing destination file
Try `cp --help' for more information.

the target folder existed.

TIA

NiallC
 
Old 07-01-2005, 03:20 PM   #5
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
This is because the '*' is expanded by the shell into all the file names in that directory, and the command line can have a length of only 32k (if I remember correctly - anyway a fixed length).

If there are no other directories in the directory you ran the command, you could try the following from the parent directory:

cp `grep -R -l 'wnix' directory_in_which_you_runed_the_command` /root/Desktop/new/

To be more clear, if you run the command

cp `grep -l 'wnix' *` /root/Desktop/new/

in the directory /root/tmp , you could try

cp `grep -R -l 'wnix' tmp` /root/Desktop/new/

from /root
 
Old 07-01-2005, 04:37 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Here's an alternative approch which might work:
Code:
$ ls -1 * | grep -e ' ' | gawk '{x = "echo " $0;system(x);}'
CD Player.desktop
Movie Player.desktop
RealPlayer 10.desktop
Sound Jucier.desktop
Sound Recorder.desktop
Volume Control.desktop
As written, that code just goes through my current directory (and all sub-directories) and prints the name of any files whose name contains a blank.

In your case, something like
Code:
# grep -l 'wnix' * | gawk '{x="mv " $0 " /root/Desktop/new/";system(x);}'
might work.

Do note that the "*" in your grep command, like the one in my ls command refers to all files below your cwd, including files th sub-directories. The gawk program could be modified to handle the sub-directories, and to not require the filtering by grep, but the code is simpler to follow this way.
 
Old 07-01-2005, 04:44 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
You know, after I made that post, it struck me that we may be laboring to produce a mouse. Wouldn't a simple
Code:
# mv *wnix* /root/Desktop/new
do what he wants?
 
Old 07-01-2005, 04:58 PM   #8
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
He doesn't want the string in the name of the file, he wants the files that contain the string. So your approaches are not for his problem. (and anyway if you would have many many files, it won't work because of *)

Try the following (in a test directory... it will create many many files) :

for ((I=0;I<50000;I++)); do echo a > aaaaaaaaaaaaaaaa$I; done
ls *

The ls will give an error (at least on mine, does)
 
Old 07-01-2005, 05:53 PM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Quote:
Originally posted by vladmihaisima
He doesn't want the string in the name of the file, he wants the files that contain the string.
My bad! Sorry, I wasn't paying attention. So, try this one
Code:
gawk '/wnix/ {x = "echo " $FILENAME;system(x);nextfile;}' *
which should just list the files.

Change the "echo" to a "cp" and add the destination (in double-quotes) to the x assignment if that works. (gawk will fail if there is a sub-directory referenced by the "*".
 
Old 07-01-2005, 08:14 PM   #10
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Argument list too long is a great case for xargs, I believe. As in:
Code:
grep -l 'KEYWORD' * | xargs mv {} /NEW/DIRECTORY
(Give or take a bit)
 
Old 07-01-2005, 08:48 PM   #11
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
that still gives a long argument list to grep, and the {} notation is something from find.

You could do
Code:
ls | xargs grep -l KEYWORD | while read i; do mv "$i" /NEW/DIRECTORY/; done
or to search within subdirectories of the current directory as well,

Code:
grep -rl KEYWORD . | while read i; do mv "$i" /NEW/DIRECTORY/; done
both of those are untested.

There's also a way to do it with find and -exec, and I'm sure there's some elegant way to remove the bourne shell loops in my code, which are just there to get the directory at the end of mv's argument list
 
Old 07-01-2005, 08:51 PM   #12
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Please read the man page for xargs, it does use the {} notation as well, but you are right, it would give the long list to grep. Hrrm.
 
Old 07-01-2005, 08:58 PM   #13
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
oops. my bad

Code:
ls | xargs grep -l KEYWORD | xargs mv {} /NEW/DIRECTORY/
or
Code:
grep -rl KEYWORD . | xargs mv {} /NEW/DIRECTORY/
would be the synthesis of the two schems then.
 
Old 07-01-2005, 09:04 PM   #14
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
With ls, I think you need 'ls -1' because xargs expects to split on newlines.
 
Old 07-01-2005, 09:19 PM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Retracted: The code didn't work properly. (It did the when I tested before posting, but not after. )
Not to say that xargs doen't look like a more elegant solution.

Last edited by PTrenholme; 07-01-2005 at 09:59 PM.
 
  


Reply


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
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 12:01 PM
Script to find all picture files on a HDD and move them scoops98 Linux - General 8 02-27-2007 02:40 AM
find and move most recent files in dir backnine_99 Linux - Software 2 06-14-2005 12:03 PM
Mandrake Move 2.0 doesn't seem to find my USB flash drive Jeff59 Linux - Hardware 0 01-24-2005 07:21 PM
Mandrake Move can't find partitions? everest Mandriva 0 09-29-2004 09:39 PM


All times are GMT -5. The time now is 07:21 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration