Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a string in multiple files on Linux [closed]

Tags:

linux

grep

search

How to search in multiple files or folders for a string inside a plain text file?

For example I need to find the string "foo" in all files in the folder "/home/thisuser/bar/baz/"

like image 684
Dropout Avatar asked Jun 21 '13 08:06

Dropout


1 Answers

You need to have read privileges on the files you will be searching in. If you do have them, then simply use

grep -r "foo" /home/thisuser/bar/baz/*

to search in a certain folder or

grep "foo" /home/thisuser/bar/baz/somefile.txt

if you need to search in a specific file, in this case "somefile.txt". Basically the syntax is

grep [options] [searched string] [path]
// -r is an option which states that it will use recursive search

Another useful options are "-n" to show on which line in which file the string is located, "-i" to ignore case, "-s" to suppress some messages like "can't read file" or "not found" and "-I" to ignore binary files.

If you use

grep -rnisI "foo" /home/thisuser/bar/baz/*

you will exactly know where to look.

like image 118
Dropout Avatar answered Nov 05 '22 20:11

Dropout