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

50 Comments

  1. felix ezequiel Andre on July 14, 2021 at 9:28 pm

    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!!



  2. Nguyễn Tấn Thành on July 14, 2021 at 9:29 pm

    Thank you



  3. Serhiy Pidkuyko on July 14, 2021 at 9:30 pm

    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)



  4. Poe Lemic on July 14, 2021 at 9:31 pm

    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.



  5. Mak Or on July 14, 2021 at 9:32 pm

    Im using grep -wircl "name" . and there is a file that has like 30 matches but it shows only a 1



  6. Lucifer on July 14, 2021 at 9:33 pm

    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"



  7. Parth Ranawat on July 14, 2021 at 9:33 pm

    can you share your .bashrc file. I love the way how your shell looks like



  8. Craig Duncan on July 14, 2021 at 9:33 pm

    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" ???



  9. Emmanuel Etoke on July 14, 2021 at 9:36 pm

    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.



  10. Simone on July 14, 2021 at 9:36 pm

    great video! What about sed and awk? are you ever gonna cover them? Keep it up, pretty good work 😉



  11. תמיר סאסי on July 14, 2021 at 9:38 pm

    The video is excellent, very clear, I learned a lot



  12. thrilochan pakkapoti on July 14, 2021 at 9:39 pm

    After seeing one video, i am addicted to watch more. Thank you friend



  13. maspoetry on July 14, 2021 at 9:41 pm

    great tutorial, thanks!

    Not sure if you did it purposely but -win is an easy, useful flag to remember



  14. Janny Wong on July 14, 2021 at 9:42 pm

    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.



  15. Mikołaj Mirski on July 14, 2021 at 9:44 pm

    awesome tutorial, full of details. Thanks for that ! Subscribed 😉



  16. We Twinz on July 14, 2021 at 9:46 pm

    Good job bro



  17. M SURAJ NAIR on July 14, 2021 at 9:48 pm

    There are very few tutorial which gets straight to the point, this is one of them. Really enjoyed learning, Extremely instructive.

    Thank you.



  18. NBBZ on July 14, 2021 at 9:50 pm

    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?



  19. 乔焜 on July 14, 2021 at 9:51 pm

    very helpful



  20. Amell Georg on July 14, 2021 at 9:54 pm

    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 ?



  21. Sekander on July 14, 2021 at 9:54 pm

    Excellent tutorial! Thanks a ton for sharing.



  22. Vicente Cortes on July 14, 2021 at 9:56 pm

    you are awesome



  23. SciSkate on July 14, 2021 at 9:58 pm

    what does grep -v do? (lowercase v)



  24. Computer Guy on July 14, 2021 at 9:59 pm

    can we do grep on files which are modified on or after perticular date



  25. latlov on July 14, 2021 at 10:00 pm

    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!



  26. Amit Moses on July 14, 2021 at 10:02 pm

    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!



  27. MrTommycat95 on July 14, 2021 at 10:02 pm

    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



  28. H S on July 14, 2021 at 10:02 pm

    This made so much sense 😀



  29. ilgas11 on July 14, 2021 at 10:02 pm

    the only youtuber to speak at playback speed 1.5 by default! 😀



  30. MrTommycat95 on July 14, 2021 at 10:07 pm

    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



  31. Mark Slima on July 14, 2021 at 10:07 pm

    Finally a great explanation of GREP for Mac terminal nerds like me! GOLD!



  32. 卡机不 on July 14, 2021 at 10:08 pm

    you saved my day sir, thank you so much



  33. bill price on July 14, 2021 at 10:10 pm

    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.



  34. Pankaj Kharade on July 14, 2021 at 10:12 pm

    👍👍👍



  35. Vish Knew on July 14, 2021 at 10:14 pm

    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 ❤️



  36. James Yen on July 14, 2021 at 10:14 pm

    This is very helpful, thank you very much!



  37. PATRICIA MIRELLA DA SILVA SCARDUA on July 14, 2021 at 10:14 pm

    That’s a great video!



  38. R T on July 14, 2021 at 10:15 pm

    thank you sir.



  39. Durragas on July 14, 2021 at 10:16 pm

    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)



  40. Raya Kelley on July 14, 2021 at 10:17 pm

    **Thanks!!* #2020Nov



  41. chandrasekhar anabatula on July 14, 2021 at 10:18 pm

    how did you colour your Mac terminal for text and directory, as mine is only dual colour one for background and one for text



  42. Yogesh on July 14, 2021 at 10:18 pm

    Man, you’re awesome. You made everything sound so easy.



  43. TJ Khara on July 14, 2021 at 10:19 pm

    Thank you great video!



  44. Paul Goddard on July 14, 2021 at 10:21 pm

    Hi Corey, thanks for last bit on Mac BSD grep distinction!



  45. Root Vegetable on July 14, 2021 at 10:22 pm

    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?



  46. Leestons on July 14, 2021 at 10:22 pm

    #LinuxMasterRace



  47. Lukas on July 14, 2021 at 10:22 pm

    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) …")



  48. Shubham on July 14, 2021 at 10:23 pm

    Does grep work with every type of file that contains text like, .log file as well?



  49. Elias Frehner on July 14, 2021 at 10:25 pm

    your videos are great



  50. Dhanush sivajaya on July 14, 2021 at 10:26 pm

    Thankyou sir