![]() |
how to grep for [string]
how do you use grep to search for something that has brackets in it?
i'm trying to find all files in a directory structure that contain: "[music]" but i only want results for that exact string. searching for "music" returns way too much. |
Escape them with a backslash.
|
Quote:
Code:
grep -r "\[music]" * |
You need the backslash before every bracket, including the closing bracket so more like:
Code:
grep -r "\[music\]" * |
Quote:
|
You don't need a backslash just use quotes, Just confirmed:
touch blah 09:59:13 root in ~ |}> cat blah 09:59:17 root in ~ |}> echo "[blah]" >> blah 09:59:29 root in ~ |}> grep "[blah]" blah [blah] 09:59:37 root in ~ |}> |
Quote:
Code:
$ echo bbbbb | grep "[blah]" |
Yeah, here is an example scenario for my original situation. I was trying to find only the exact phase [music] inside of "file1.conf"
One problem is that the word "music" would appear thousands of times inbetween the original path of "~/sandbox/" and the actual file1.conf and also I didn't exactly know that it was file1.conf that I was looking for. there are hundreds of files that I needed to sift through to find "[music]" below is a scenario I built to test Code:
23:53:35_somename@somePCname:~/sandbox$ dir -lRbut i don't know that in my real situation. in my scenario i have placed similar text that will return if my search string is not correct. Code:
GNU nano 2.2.6 File: somedirectory/file1.conf my term window has color coding which makes it a lot more clear what is being returned but if you try yourself you should see that it finds anything with an m, u, s, i, or c is returned and it doesn't search at all for the [ or ] Code:
00:02:26_somename@somePCname:~/sandbox$ dir -Rl | grep "[music]"you see here, that it does find [music] in the file, file1.conf which is what I am looking for, but it does so only by searching for the word music. color coding will show that it doesn't consider the [ or ] and in my real situation it's going to return thousands more music results. Code:
00:17:57_somename@somePCname:~/sandbox$ grep -R "[music]" *Code:
23:57:16_somename@somePCname:~/sandbox$ grep -r "\[music\]" * |
| All times are GMT -5. The time now is 09:40 PM. |