I would like to grep for the following strings in a file:
directory1
directory2
directory3
Is there a way to grep for all 3 simultaneously with grep?
For instance:
cat file.txt | grep directory[1-3]
Unfortunately, the above doesn’t work
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If those are the only strings you need to search for, use
-F(grep for fixed strings):If you want to grep using more advanced regex, use
-E(use extended regex):Note that some
greps (like GNU grep) won’t require-Efor this example to work.Finally, note that you need to quote the regex. If you don’t, your shell is liable to expand the regex according to pathname expansion first (e.g. if you have a file/directory called “directory1” in the current directory,
grep directory[1-3]will be turned intogrep directory1by your shell).