Linux/Mac Terminal Tutorial: The Grep Command – Search Files and Directories for Patterns of Text
Linux/Mac Terminal Tutorial: The Grep Command – Search Files and Directories for Patterns of Text
In this Linux/Mac terminal tutorial, we will be learning how to use the grep command. The grep command allows us to search files and directories for patterns of text. You can also pipe the output of one command into grep to get certain matches. It’s extremely useful once you learn the ins and outs. Let’s get started…
The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Terminal/Grep
Regular Expressions Tutorial: https://youtu.be/sa-TUpSx1JA
Regular Expressions Tutorial (Python): https://youtu.be/K8L6KVGG-7o
✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms
✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join
✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey’s Public Amazon Wishlist
http://a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website – http://coreyms.com/
My Second Channel – https://www.youtube.com/c/coreymschafer
Facebook – https://www.facebook.com/CoreyMSchafer
Twitter – https://twitter.com/CoreyMSchafer
Instagram – https://www.instagram.com/coreymschafer/
#Linux #Mac
Awesome video! When you search in sub-directories, can we add an option to do not search in a given directory? Does such a thing exist 🙂 Thanks!!
Thank you
1. one can’t use the "d" for the digit in grep/egrep (if not use "-P")
2. to use "{3}" for "three times" one need to use ‘egrep’ instead of ‘grep’ (if not use "-P")
3. there is no option "–with-default-names"
4. => one need to install "brew install grep" and use ‘ggrep/gegrep’ instead of ‘grep/egrep’
5. there is a very useful option "–color" to highlight found items (as well as filenames and row numbers when using GNU grep)
Thanks for covering this. Man, do you make things understandable and simple. I wish that I would have just used Youtube to learn Linux, instead of doing a community college course.
Im using grep -wircl "name" . and there is a file that has like 30 matches but it shows only a 1
The new version of HomeBrew does not support ‘–with-default-names’
the workaround:
install grep:
brew install grep
add to PATH:
export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
can you share your .bashrc file. I love the way how your shell looks like
Very cool and informative video – thanks! One question though – I’m on OSX 10.14.6 and when I try "brew install grep –with-default-names" I get:: "Error: invalid option: –with-default-names" ???
Hi Corey, I created a file and included a telephone number. Used the grep command with regular expressions but couldn’t see the phone number. Can you explain why? I am using a VM running CentOS 7 on a Windows computer.
great video! What about sed and awk? are you ever gonna cover them? Keep it up, pretty good work 😉
The video is excellent, very clear, I learned a lot
After seeing one video, i am addicted to watch more. Thank you friend
great tutorial, thanks!
Not sure if you did it purposely but -win is an easy, useful flag to remember
Homebrew UPDATE Aug 2020
If you’re a mac user and want to use corey’s homebrew trick, read on…
At 16:07 Corey says to use this command: brew install grep –with–default-names
I tried doing this and it doesn’t work. Per a google search that optional argument is no longer allowed. Here’s the work-around.
1. Inside terminal: *brew install grep*
2. inside your .bashrc file, put this line of code, preferably at the top so this path is checked first: *export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"*
3. open a new terminal window (command+n) and type in *grep -V* . Should say GNU grep.
awesome tutorial, full of details. Thanks for that ! Subscribed 😉
Good job bro
There are very few tutorial which gets straight to the point, this is one of them. Really enjoyed learning, Extremely instructive.
Thank you.
there is a chance i skipped this part by mistake but if i want to for example move every file with .flf in its name to another file how do i pipe grep to mv to do that?
very helpful
Suppose the file contents are in the format below
ID:Name:DOB:designation
From this, we want to print the details of people whose first name is greater than 6..how can we do that ?
Excellent tutorial! Thanks a ton for sharing.
you are awesome
what does grep -v do? (lowercase v)
can we do grep on files which are modified on or after perticular date
6:35 $ grep -win -C 2 "find this string" ./*
8:05 $ grep -winr "Find that string even in subdirs" ./*
9:35 $ grep -wirl "List those files that contain this string in current directory dot" .
10:20 $ grep -wirc "List those files that contain this string and number of occurrences in current directory dot" .
11:10 $ history | grep "search for git commits in history"
12:00 $ history | grep "search for git commits" | grep "now narrow down to some string"
15:40 only for mac: $ brew install grep –with-default-names #to allow regular expressions based on perl, as shown in 17:56.
19:13 Homework!
I just ended up watching your Python OOP series. and i randomly looked for some Linux tutorials, and i was SOOO happy to see your name!
On your patreon, can you pls create a one time donation icon? Would be helpful if I want to make a 20 dollar donation once instead of doing it over the course of 20 months. Thanks
This made so much sense 😀
the only youtuber to speak at playback speed 1.5 by default! 😀
Some folks are intelligent and smart but are horrible at teaching. Corey is smart, intelligent and born to be a teacher. While I am going to contribute on patreon right away, you have a gift that you can use to sell your videos at a small price to benefit and educate the population. Amazing
Finally a great explanation of GREP for Mac terminal nerds like me! GOLD!
you saved my day sir, thank you so much
I have a Linux (POP os) so I am watching your tutorials. Can’t help thinking why I need the commands when I can just click on files as if were windows. The operating environment takes away the need for command line terminal input. Im guessing there’s something I can’t do by clicking, right clicking, search, copy paste etc.
👍👍👍
Mann….You are a legend…Only few people are out there like you..You really are a gemstone. Thank you soo much for this awesome tutorial brother ❤️ No wasting time, straight to the point. God bless you.
Love from Kerala ❤️
This is very helpful, thank you very much!
That’s a great video!
thank you sir.
I’m looking at learning to use grep and vim as I’m learning to program in uni because I hear it’s what professionals use, but honestly I’m struggling to see the advantages over notepad++ (also because I’m running out of memory in my head and having to learn new stuff on top of school is hard)
**Thanks!!* #2020Nov
how did you colour your Mac terminal for text and directory, as mine is only dual colour one for background and one for text
Man, you’re awesome. You made everything sound so easy.
Thank you great video!
Hi Corey, thanks for last bit on Mac BSD grep distinction!
How do you get your terminal to look like that? Mine doesn’t display matches in orange…is that something you can make happen on Mac OS?
#LinuxMasterRace
In case any one is having issues with the –with-default-names flag on Mac – it is apparently not available anymore in the latest version of homebrew. One workaround that worked for me is to 1. install the package via "brew install grep"; 2. set up an alias pointing from grep to ggrep. You can do this by adding ‘alias grep=’ggrep’ somewhere in your .bash_profile file.
Steps:
1. brew install grep
2. nano ~/.bash_profile
3. (add ‘alias grep=’ggrep’ somewhere in the file and then save it)
4. open new terminal for the change to become effective
5. check with grep -V (should display "ggrep (GNU grep) …")
Does grep work with every type of file that contains text like, .log file as well?
your videos are great
Thankyou sir