|
Each time I need to search for some text in files on *NIX machines I get to google and search for it. Not always can find it quickly, so now is the time to put it right once and for all 
ok, so the tas is to search for a text: little fox
in all files that are in /home/username
I will use grep for this, with option -H so I can see the filenames in where the text is and the line of the text where the search text appeared.
grep -H -r
-r is used to search in any subdirectories of /home/username, so if there is a file in /home/username/subfolder/file.txt with text little fox it will get me the filename where the text is and the line that has the text I was searching for:
username@machine:~$ grep -H -r "little fox" /home/username
/home/username/subfolder/file.txt:Once I saw a little fox running through the woods.
/home/username/subfolder/file.txt:Then the little fox jumped out and scared the bear away.
ok, so options for grep:
-H - print filename
-r - recursively look in subfolders
Another nice option is: -n - print line number where the text was found, so lets say we run the same search again but with added -n:
username@machine:~$ grep -H -r -n "little fox" /home/username
/home/username/subfolder/file.txt:2:Once I saw a little fox running through the woods.
/home/username/subfolder/file.txt:13:Then the little fox jumped out and scared the bear away.
the result may be passed to another program to parse it just in case you want to do some more manipulations with the results.
but for now its enough! hopefully helped someone with this little tip 
|