Finding my lost file by searching for words in it

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding my lost file by searching for words in it
# 1  
Old 07-24-2011
Finding my lost file by searching for words in it

Got a question for you guys...I am searching through a public directory (that has tons of files) trying to find a file that I was working on a longggggg time ago. I can't remember what it is called, but I do remember the content. It should contains words like this:


There are several hundred files in this directory, so I was trying to figure out a command that will locate this file, which contains all of these words. I wanted to put these 6 words into a file using VI, and then use that file to find what I am looking for.

I know I could do:

grep -R -l 'String to find'

but I would rather use the file to make sure all of the words are in it. I think I need to write a short script so I can replace 'String to find' with an ambiguous term, which is my problem. Could I just use $n instead of 'String to find' and increment $n so it will move lines down each time, thus searching for a new word?

Since I tend to have organizational problems, it would be nice to have this script work for any input, so my goal is to not hard code specific words into the algorithm.

# 2  
Old 07-24-2011
grep -R -l -f file_with_strings

# 3  
Old 07-24-2011
For OR patterns it's easy:
grep -f patterns -Rl PATH

If you want AND patterns, you need a script like this (it's just my attempt):

# usage: ./script 'WORD1 WORD2 WORD3 ...' PATH 


find "$mypath" -type f | while read f; do
  for word in $words; do
      if ! grep -q "$word" "$f" 2>/dev/null; then
  if [ "$found" ]; then
      echo "$f";

Last edited by yazu; 07-25-2011 at 02:30 AM.. Reason: minor fixes
# 4  
Old 07-24-2011
That looks good, but I was wondering if I could do it this way. Say those words are located in a file, and for example we will call it words.txt. Can I just plug in that file, instead of listing all of the words? Like so...

grep -f (wc -l words.txt) -R1 *.txt

??? I really don't know if the line count part would actually work, and if it would check all of the words or just the first occurrence (I could probably use sed for that too), but it just seems easier than writing out a whole script.

Basically, I am wondering if I can replace the "pattern" with an actual file.
# 5  
Old 07-24-2011
grep -f <filename>

The -f means: use the content of the coming file as the pattern to search for. Passing it the argument of how many lines the file has will not work.

Last edited by Aia; 07-24-2011 at 10:41 PM.. Reason: Missing a n
# 6  
Old 07-25-2011
The main question - if you want find files with ALL your words or just with ANY of your words.

For the latter grep command is enough, for the former - you need a script. Whether this script will get your patterns (words) from a file or on the command line - it's not a problem at all. You can change my script like this (you can't have patterns-words with spaces though but it's easy enough to modify the script to catch it) :

# usage: ./script WORDS_FILE PATH 

words=`cat $1`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Finding lost machine on network trouble

Couldn't find my PC on network. Root of evil the was in bad patch-cable. (0 Replies)
Discussion started by: useretail
0 Replies

2. UNIX for Dummies Questions & Answers

searching words & print prefixed string after it

I have a text which I divided them into sentences and now printed them in a rows. I want to get the list of most of words ( the, and, a) and print 5 words after them (so 6 with the word itself). I have created an acceptfile with those rows and using grep but I have rows that have these words more... (2 Replies)
Discussion started by: A-V
2 Replies

3. Shell Programming and Scripting

Finding consecutive same words in a file

Hi All, I tried this but I am having trouble formulating this: I have a file that looks like this (this is a sample file words can be different): network router frame network router computer card host computer card One can see that in this file "network" and "router" occur... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

4. UNIX for Dummies Questions & Answers

Searching for multiple words on a line in any order issue

Hi again I have figured out how to be able to sort through lines in a file with multiple words in any order and display them using this command: cat file | grep -i $OPTION1 | grep -i $OPTION2 | grep -i $OPTION3 OPTION1 is 2008, OPTION2 is Mar, OPTION 3 is Tue Result: Tue Mar 25... (4 Replies)
Discussion started by: semaj
4 Replies

5. Shell Programming and Scripting

Finding the number of unique words in a file

find the number of unique words in a file using sort com- mand. (7 Replies)
Discussion started by: abhikamune
7 Replies

6. Shell Programming and Scripting

Perl searching special words in lines

Hi , i am a new with perl, i want to made a script that find in file rows that start with specil words, as an example a line will start with" ............................................. specialword aaa=2 bbb=5 ............................................. and to put this in a new file... (3 Replies)
Discussion started by: alinalin
3 Replies

7. Shell Programming and Scripting

searching for words between delimeters from the rear

Hi, i need to pick up dates and times from the file names which are of unequal length. The dates and time are delimited by dot. I am interested in getting the strings between the delimeter for fields -3, -4, -5 from behind (rear) so that the out put looks like : 071118.011300.556 I have... (2 Replies)
Discussion started by: oktbabs
2 Replies

8. Shell Programming and Scripting

Searching words in a file containing a pattern

Hi all, I would like to print words in a file seperated by whitespaces containing a specific pattern like "=" e.g. I have a file1 containing strings like %cat file1 The= some= in wish= born <eof> .I want to display only those words containing = i.e The= , some=,wish= ... (5 Replies)
Discussion started by: sree_123
5 Replies

9. UNIX for Dummies Questions & Answers

searching and displaying most commonly used words

Hi guys, i need to search the most commonly occuring words in a file and display their counts of about 30000 words and the words shud not be of typ specified in file 2 e. words like is,for,the,an,he,she etc... k. file1: ALICE was beginning to get very tired of sitting by... (2 Replies)
Discussion started by: arunsubbhian
2 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question