SoFunction
Updated on 2025-03-02

Common commands and usages for querying content in Linux shell command line

There are several commands to find file content in a Linux shell. The following are several commonly used commands and their usage:

1. grep command

The grep command is one of the most commonly used text search tools, which searches for a specified string in a file and outputs lines containing that string.

Basic usage

grep "pattern" filename

• pattern: The string or pattern to search for.

• filename: The file name to search for.

Options

• -i: Ignore case.

• -v: Invert the match, that is, the output does not contain the specified pattern.

• -r or --recursive: Recursively searches all files in the directory.

• -l: Only output file names containing matches, and not specific content.

• -n: Output line number.

• -C num: Outputs a num row matching the line context.

Example

Search for lines containing "hello" in the file:

grep "hello" 

Recursively search the lines containing "world" in all files under directory ./dir/:

grep -r "world" ./dir/

2. find command

The find command can be used to search files in the file system, and in combination with grep, it can be used to search file content.

Basic usage

find path -name pattern -exec grep search_pattern {} \;

• path: Start search path.

• pattern: file name mode.

• search_pattern: Content to search.

Example

Search for lines containing "example" in all .txt files in the current directory and its subdirectories:

find . -name "*.txt" -exec grep "example" {} \;

3. ack command

ack is a more modern and more powerful search tool that supports Perl regular expressions and can automatically ignore files in version control systems.

Install

In some systems, ack may need to be installed first:

sudo apt-get install ack-grep

usage

ack "pattern" filename

• pattern: The string or pattern to search for.

• filename: The file name to search for.

Example

Search for lines containing "hello" in the file:

ack "hello" 

4. ripgrep command

ripgrep (rg) is a fast text search tool that supports regular expressions.

Install

You may need to install ripgrep first:

curl -L /BurntSushi/ripgrep/releases/download/13.0.0/ripgrep_13.0.0_amd64.deb -o 
sudo dpkg -i 

usage

rg "pattern" filename

• pattern: The string or pattern to search for.

• filename: The file name to search for.

Example

Search for lines containing "hello" in the file:

rg "hello" 

Summarize

The above are some common commands and their usages for finding file content. You can choose the tool that suits you best as you want. If you need more advanced features or better performance, consider ack or ripgrep.

You can also query the contents of all files in the folder.

 grep -r "load_unity_model" ./src/seamless_communication/

This is the article about the commonly used commands and usages of Linux shell command line query files. For more relevant shell command line query files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!