#!sh grep and move files

Jim Trigg jtrigg at spamcop.net
Thu Apr 3 11:15:21 PST 2003


On Thu, Apr 03, 2003 at 11:47:30AM -0600, Matthew Bettinger wrote:
> 
> I am trying to find the best way to search through several thousand files and 
> move some to a different directory.  The files are all prefixed with LB.  
> Like, LBX99.DAT141683.
> 
> These are data transactions and contain one line.  The lines i am trying to 
> search for all begin with 
> 
> 1~TA~  (standing for timeand attendance labor transactions)
> 
>  I've tried
> 
> #!/usr/bin/sh
> for x in `find /dir -type f -exec grep '1~TA' [] \;`

This will print matching lines; what you want here is grep -l, but see below.
> do
> mv $x /newdir
> done
> ----------------------------
> 
> 
> There  seems to be something i am missing like some output redirection or an 
> ls listing... probably alot more.
> 
> I know this can be done in a one liner somehow but I need it to be executed 
> from cron.

find is actually a bit of overkill; this can be done as follows:
#!/usr/bin/sh
mv `grep -lr '1~TA' /dir` /newdir

If you're using it from cron, make sure to use full paths for mv and grep
(and find, if you keep using it) as the cron path may not be what you
expect.

Jim
-- 
Jim Trigg, Lord High Everything Else  O-          /"\
                                                  \ /  ASCII RIBBON CAMPAIGN
Hostmaster, Huie Kin family website                X    HELP CURE HTML MAIL
Verger, All Saints Church - Sharon Chapel         / \


More information about the freebsd-questions mailing list