[prog] Grep

Thomas Thurman tthurman at gnome.org
Mon May 19 14:26:07 UTC 2008


Ysgrifennodd anshika kalra:
> I was trying to get the name of file from directory using grep.
> grep  <filename> <directory name>
> But no result was displayed.Since linux treats directories also as files
> then why is this happening?

Can you explain what you mean by "linux treats directories as files"?  
It's true that Linux's filesystems store directory information in a 
file-like structure on disk, but this is true of almost every other 
filesystem since DOS introduced subdirectories.  The actual structure of 
these files remains opaque to the user; if it was possible to do what 
you appear to be trying, these files would need to be text files in any 
case (most of the features of grep only work on text files; with 
non-text files it would only tell you whether the word was found in 
the file).

To do what you seem to be trying to do, you need the find command-- 
something like

  find <directory name> -name <filename>

> Could someone also tell why
> grep '<word1>' '<word2>'   <filename>  displays an error.I only know that
> placing these words in a file and then using fgrep or grep -f gives result.
> Is there any other way.

Unless you use -f, the first argument of grep is the expression to 
search for and the rest are the files.  If it was otherwise, how would 
grep know where the expressions ended and the filenames began?  (grep
never sees your '' quoting: that's a matter for the shell.)

By the way, I was incorrect earlier in saying that the check for 
compression was a flag check: as someone else pointed out, it was a 
check on argv[0], which is the name of the program itself.  I apologise: 
I should read more carefully.

peace

T

-- 
Thomas Thurman, tthurman at gnome, http://blogs.gnome.org/tthurman
limestone formations


More information about the Programming mailing list