In this tutorial, we will show you how to search and print which files contain your specified text or string in Ubuntu and other Linux in command line, with examples.
Method 1: Use grep commandThis command comes useful when you want to filter the output.
The command options which can be used in grep are: -l or --files-with-matches means only print names of files containing matches, -r or --recursive, to search sub-folders for given directory.
You may print names of files, matched lines, as well as line numbers of files contain “GParamSpecClass”.
To show more file content, you may add -A NUM or --after-context=NUM to print NUM lines of trailing context after matching lines, and -B NUM or --before-context=NUM to print NUM lines of leading context before matching lines.
Sometimes, there may be multiple files or multiple lines match what you search.
grep supports regex expressions to make searching more flexible.
Method 2: Use ag command, ag for file searching.
It prints all the names of files, lines and line numbers that matches. ag command is super fast!
For example, search “gparamspecclass” under current directory, and only show file-names and how many matches.