Nearly every Linux user has used the grep command to search for text with regular expressions, a precise text-searching language.But the grep command is over 50 years old.Here are some modern replacements you might want to consider.
ripgrep Search down through your directories ripgrep is one tool that intends to improve on the venerable grep.One way it does this is by searching recursively.When you call ripgrep with the rg command and a regular expression string, it will search the files in the current directory as well as any files in subdirectories until it gets to the bottom of the directory tree.
I'm interested in data science programming with Python, so suppose I wanted to find files that included the Seaborn library for data visualization.To do that, I would use this command: rg seaborn stats I can also search within specific file types.Suppose I wanted to find all the import statements for my Python scripts.
Here's how I would do it with ripgrep: rg -t py import The advantage of ripgrep's recursive searching is that I don't have to deal with the find command's ugly syntax or use xargs to feed files into it.ripgrep is surprisingly fast for a recursive search tool.ack A Perl-powered grep replacement ack is a grep replacement tool that's based on Perl.
While Perl has faded from the limelight as a scripting language, it's still a very good choice if you're into text processing and regular expressions, which offer precise text searching down to the character.Similar to ripgrep, ack will search recursively starting from the current directory: ack seaborn This will find all files containing the string "seaborn," starting in the current directory.ack is also designed for searching through source code rather than general text, and will filter out a lot of information that's irrelevant to programmers by default.
ast-grep A powerful search-and-replace tool for programmers Close ast-grep is a powerful tool that can not only search text, but it can also perform replacements based on searches, similar to the search-and-replace function in a word processor.It can also function as a "linter," a program that can find programming and stylistic errors to highlight them.The programmer can then fix them.
A simple use would be to find a pattern in a series of scripts.In this case, I'll use a Python example again.I'll look for import statements, since these are used to call libraries: ast-grep -p 'import' ast-grep knows about many programming language file formats, and Python is already a very popular language.
This will allow it to find the import statements in any Python files in the current directory and lower.Since it's possible to mess up your files using search and replace, the developers have included an online "playground" where you can test patterns before you unleash them on your local machine.There's a lot to include with ast-grep that won't fit in this small section.
git grep Yes, git can search git has become the premier version control tool for a reason.It has many powerful features, it's open-source, and, like Linux, Linus Torvalds created it.It also has a grep-like utility for searching source code.
It's rather imaginatively called "git grep." The advantage of git grep over standard grep is that it can search your entire project history and quickly parse tracked files while automatically skipping ignored build artifacts.It's powerful, but the catch is that the code you want to search has to be tracked by git by default.Fortunately, I happen to have a repository of my Jupyter notebooks on my machine.
To search for Seaborn imports, I can simply call git grep: git grep 'seaborn' It will then show me all the files where this pattern occurs.If I wanted to find the specific line numbers in the files, I would use the -n option.git grep -n 'seaborn' git grep patterns are case-sensitive by default, so if I wanted to find a pattern without paying attention to case, I would use the -i option: git grep -i 'seaborn' Find Files Interactively With fzf Fuzzy Search Makes File Hunting Faster One problem with the standard grep is that it's static.
All it can do is read standard input from the command line and filter it, returning more text.Wouldn't it be great if you could find files interactively? That's what fzf does.fzf stands for "fuzzy find." By default, fzf will open an interactive session that shows you matching files based on what you type.
This means that if you match part of the word, fzf will show you the whole line that matches.It also matches recursively, starting at the current working directory.You can specify exact matches by prefacing a pattern with a single quote (') character, like this: 'python You can also match by boundaries by enclosing a pattern in single quotes: 'python' You can find patterns at the beginning of a line with the caret (^) operator: ^python The dollar sign ($) You can also find files that don't match a certain pattern, such as files that aren't Python scripts: !.py$ Sometimes it's good to let go Linux is still the biggest successor to the original Unix, but some things are worth letting go when better alternatives come along, such as these modern alternatives to the grep command.
Read More