79

I am using Ubuntu on VMWare but I cannot connect to internet because of security restrictions.

I was wondering if there was a way to search for strings through the terminal and find which line the string is located on within the file.

2
  • 1
    Are you talking of grep or something more advanced?
    – muru
    Aug 18, 2014 at 18:55
  • Have u searched for it first.
    – xxbinxx
    Aug 22, 2014 at 5:48

3 Answers 3

120

There are almost too many options to list

grep -r 'pattern_to_match' directory_to_search

Will output the file name and full line matching the pattern.

36

The best I use is grep command with options -ri (Recursive and case insensitive search):

$ grep -r <text_pattern_to_search> directory_or_path_to_search

options that might be useful to you:

    -i - case insensitive
    -r, --recursive  like --directories=recurse
    -R, --dereference-recursive  likewise, but follow all symlinks
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.

for deep info you can do grep --help or man grep in linux terminal.

Cheers

0
10


If you want to find only line number where the sting is located within the file use this:

grep -n '/string_To_Find/=' directory/file_Name

If you want to find line number and also output the full line name where the string is located in the line use this:

grep -n 'string_To_Find' directory/file_Name

And if you only want to find full line name where the string is, use this:

grep -r 'string_To_Find' directory/file_Name

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .