This is a simple command-line utility written in Rust, designed to search for a specific pattern in files within a directory (and its subdirectories). It mimics the functionality of the grep
command, offering both case-sensitive and case-insensitive search modes.
- Recursive Search: The tool recursively searches through directories and subdirectories for the given pattern.
- Case Sensitivity Option: Supports case-sensitive and case-insensitive search modes.
- File and Directory Support: Handles both files and directories, printing matches found within the files.
- UTF-8 Validation: Skips files that are not valid UTF-8 encoded.
- Rust: Ensure you have the latest stable version of Rust installed. You can install Rust by following the official installation guide.
-
Clone the Repository
First, clone the repository to your local machine:
git clone https://github.com/thinkphp/grep.git cd grep
-
Build the Project
Use Cargo to build the project:
cargo build
This will compile the project and create a
rustgrep
binary inside thetarget/debug
directory.
pattern
: The text string or pattern to search for. (Required)path
: The directory to search in. (Optional, defaults to the current directory.
)--ignore-case
(-i
): A flag to perform a case-insensitive search. (Optional)
To search for the pattern error
in the current directory:
cargo run "error"
To search for the pattern create_folder
in the /home/user/projects
directory, ignoring case:
cargo run "create_folder" --path /home/user/projects --ignore-case
To search for the pattern fix
in the /home/user/logs
directory:
cargo run "fix" --path /home/user/logs
- Recursive Directory Search: The program searches through the specified directory and its subdirectories.
- UTF-8 Validation: The program reads files as raw bytes and attempts to convert them into valid UTF-8 strings. Files that cannot be decoded as UTF-8 are skipped with a warning.
- Search Modes: The search can be case-sensitive or case-insensitive based on the
--ignore-case
flag. - Pattern Matching: The program reads each file line by line and checks if the given pattern exists in any line. If a match is found, the file path, line number, and line content are displayed.
When a match is found, the output is displayed as follows:
Found in /path/to/file.txt: Line 12: This is a line containing the word error.
If no matches are found, there will be no output.
- If a file cannot be opened, an error message will be printed.
- Files that are not valid UTF-8 will be skipped with a warning.
- If an invalid directory path is provided, an error will be printed.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to fork this project and open issues or pull requests if you'd like to contribute. Contributions are welcome!
### Key Sections:
- **Features**: Highlights the key capabilities of the tool, such as recursive search and support for case-insensitive searches.
- **Requirements**: Specifies that Rust needs to be installed to run the tool.
- **Installation**: Describes how to clone the project and build it using Cargo.
- **Usage**: Provides examples for different ways to use the tool with different command-line arguments.
- **How It Works**: Explains the tool’s functionality and how it handles files, directories, and search modes.
- **Output**: Describes how the results are displayed.
- **Error Handling**: Explains how errors, such as invalid files or directories, are handled.
- **License**: States the licensing information for the project.
- **Contributions**: Encourages other developers to contribute to the project.