Computing.Net > Forums > Linux > find string and copy file script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

find string and copy file script

Reply to Message Icon

Name: jrmontg
Date: August 6, 2007 at 07:56:39 Pacific
OS: Windows 2000 Server
CPU/Ram: Intel Xeon
Product: HP
Comment:

I am needing some help on a script to find a phrase in a file and to take the file name that it finds and move that file to another directory.

THanks


jrmontg



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 6, 2007 at 09:05:18 Pacific
Reply:

finds and moves the file to the /tmp directory:

#!/bin/bash

find . -type f -name "mytest.file" -print|while read str
do
mv "$str" /tmp
done


0

Response Number 2
Name: nails
Date: August 6, 2007 at 09:26:30 Pacific
Reply:

Sorry, but I misread your question. This finds all files with the txt extension, searches for the string "my text" and moves the files to the /tmp directory:

#!/bin/bash

find . -type f -name "*.txt" -exec grep -l "my text" {} \; -print | while read s
tr
do
mv "$str" /tmp
done


0

Response Number 3
Name: jrmontg
Date: August 6, 2007 at 10:07:33 Pacific
Reply:

I did get a error.

tr: two strings must be given when translating

Suppose I want to just seach /home for the word "test" in any file?

Thanks

jrmontg


0

Response Number 4
Name: nails
Date: August 6, 2007 at 16:13:07 Pacific
Reply:

Sorry, again, but, my cut and paste inadvertly inserted a CR. Note the variable str was wrongly split. This should fix it:

#!/bin/bash

find . -type f -name "*.txt" -exec grep -l "my text" {} \; -print | while read str
do
mv "$str" /tmp
done

The . means search the current directory. You can do something like this to search every file:

# untested
find /home -type f -exec grep "test" {} \; -print | while read str
do
mv "$str" /tmp
done
# end script

The problem is that if any of your files happen to be binary, you could have a problem grepping them depending on the OS.

There is a way of eliminating binary files, but it takes more options to the find command. Do you know if you are searching binary files?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: find string and copy file script

copying files and folders www.computing.net/answers/linux/copying-files-and-folders/25486.html

How to find Gnome and Xsession file www.computing.net/answers/linux/how-to-find-gnome-and-xsession-file/24087.html

RH9 and sharing files www.computing.net/answers/linux/rh9-and-sharing-files/23699.html