RipGrep | Searching fast using RipGrep
1. Introduction
Ripgrep a recursive search utility, to search in the directory you specified for your given pattern. This tool is specialized to code search having recursive search and automatic filtering in default. For example: I have to find out where the pdf-export()
function is inside the web root directory then i will use this tool to search in that entire web root directory passing the pdf-export()
as pattern. You obviously have the query why not grep and why ripgrep. The answer to this question is explained in upcoming sections in this page.
2. Installation
In ubuntu system, we can install ripgrep
from apt repository.
sudo apt install ripgrep
In other linux distributions we can either find it on their package repository or we can simply add ripgrep in sources.
3. Basic Usage
Syntax:
The basic syntax of ripgrep is:
#Basic Syntax
rg [options] [pattern] [path]
Options:
- -l: List the file names matching the specified pattern
- Example: Find out file names containing links.
rg -l "https://" airline/
- Here in the figure below we can see that the file names that contains
https
links.
- Example: Find out file names containing links.
- —files-without-match: List the file names that do not contain specified pattern.
- Example: Find out file names that donot contain links.
rg --files-without-match 'https://'
- Here in the figure below we can see the file names that donot contain
https
links.
- Example: Find out file names that donot contain links.
- -t: Specify file type
- Example: Find out print statements inside scala.
rg -t scala "println" airline/
- Here in the figure below we can see the matches inside the scala source code in
airline/
directory.
- Example: Find out print statements inside scala.
- -z: Search for compressed zip file.
- Example:
Difference betwee RipGrep and Grep:
The primary difference between ripgrep and traditional GNU grep is that ripgrep is fast. The reason why it’s fast is that ripgrep leverages multiple cores for searching whereas grep doesn’t.
In the below shown figure we can see that when i search for pattern hi
in the entire /
directory the CPU used by two cores is parallel where no any over consumption of CPU occurs during the search.
The below shown figure is the CPU usage while doing the same task using grep. Here you can see that one core is in 100% use leaving another processor not in use.
RipGrep Examples
Recursive Search
When no file or directory is specified ripgrep will search recursively starting from the current directory. Here is the command to do recursive search.
rg [pattern] [directory]
rg "mytest()" scripts/
Filtering based on file types
To search for specific file type within a specified directory, we can use following command:
#find the specified pattern from python source file.
rg "Hello" --type py
#the recommended method to limit your search in python file is
rg -tpy [pattern]
Nice blog here Also your site loads up very fast What host are you using Can I get your affiliate link to your host I wish my site loaded up as quickly as yours lol
Stumbling upon this website was such a delightful find. The layout is clean and inviting, making it a pleasure to explore the terrific content. I’m incredibly impressed by the level of effort and passion that clearly goes into maintaining such a valuable online space.
My brother recommended I might like this web site He was totally right This post actually made my day You cannt imagine just how much time I had spent for this information Thanks
Wow superb blog layout How long have you been blogging for you make blogging look easy The overall look of your site is magnificent as well as the content