Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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

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

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
doneThe . 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 scriptThe 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?

![]() |
![]() |
![]() |

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