Skip to main content

A recursive file searcher with boolean expressions.

Project description

Recursive File Searcher with Boolean Expressions

This Python script allows you to recursively search for files within a directory based on a boolean expression of keywords. It supports AND (&), OR (|), and parentheses for grouping, providing a flexible way to define search criteria. Matching file content can optionally be copied to the clipboard.

Features

  • Recursive Search: Traverses subdirectories within the specified directory.
  • Boolean Expressions: Uses & (AND), | (OR), and parentheses for complex search logic.
  • File Pattern Inclusion/Exclusion: Filter files based on include and exclude patterns (e.g., *.txt, *.log).
  • Clipboard Copying: Copy the content of all matching files to the clipboard for easy access.
  • Error Handling: Gracefully handles file read errors and invalid directory specifications.
  • Colored Output: Uses color-coded output for improved readability (requires colorama).
  • File Grouping by Extension: Groups found files by their extensions for clearer presentation.
  • UTF-8 Support: Handles files encoded in UTF-8.

Usage

You can install this package using pip:

pip install smsearch

If you installed via pip, you can use the command:

smsearch <directory> <expression> [--include <pattern>] [--exclude <pattern>] [-c]smsearch <directory> <expression> [--include <pattern>] [--exclude <pattern>] [-c]

Installation

  1. Clone the repository:

    git clone https://github.com/celalertug/smart-search.git
    
  2. Install required libraries:

    pip install pyparsing colorama pyperclip
    

Usage

python main.py <directory> <expression> [--include <pattern>] [--exclude <pattern>] [-c]

Arguments:

  • <directory>: The directory to search in.
  • <expression>: The boolean expression to search for. Use & for AND, | for OR, and parentheses for grouping.
  • --include <pattern> (optional): Include only files matching this pattern. Uses fnmatch syntax.
  • --exclude <pattern> (optional): Exclude files matching this pattern. Uses fnmatch syntax.
  • -c, --copy (optional): Copy the content of matching files to the clipboard.

Examples:

  • Search for files containing both "word1" and "word2":

    python main.py /path/to/search "word1 & word2"
    
  • Search for files containing either "python" or "java", and also containing "code":

    python main.py /path/to/search "(python | java) & code"
    
  • Search for files containing "important" and either "urgent" or "critical":

    python main.py /path/to/search "important & (urgent | critical)"
    
  • Search for files containing either "error" or "exception" in .log files, excluding .gz files:

    python main.py /path/to/search "error | exception" --include "*.log" --exclude "*.gz"
    
  • Search for files containing "confidential" and either "password" or "secret", and copy the content to the clipboard:

    python main.py /path/to/search "confidential & (password | secret)" -c
    

How it Works

The script uses the pyparsing library to parse the boolean expression and os.walk to recursively search the directory. It opens each file, reads its content, and evaluates the boolean expression against the content. Matching files are printed to the console, grouped by extension. If the -c or --copy flag is set, the content of matching files is concatenated and copied to the clipboard.

Contributing

Contributions are welcome! Please feel free to submit pull requests for bug fixes, new features, or improvements to the documentation.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smsearch-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

smsearch-0.1.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file smsearch-0.1.1.tar.gz.

File metadata

  • Download URL: smsearch-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for smsearch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fd5a3bfd62aa6275c187dd8be5fe82854df98a57f8aa79b3c76428446ce7ae7a
MD5 887e4a056c6d3f11dfdf08238e5dd1b3
BLAKE2b-256 d15f3f29ed89e03959a21a22b4d998544e66ccce27ba17f4d529762c067feef1

See more details on using hashes here.

File details

Details for the file smsearch-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: smsearch-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for smsearch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e6a0e2b1a5dbba4c337f8fa624c6dfd30300f723e9976d7055bc2115f551bb2
MD5 51fe872a8cf1773237487344f51d69c5
BLAKE2b-256 a3e5e26e450d8242c4669782e6725f1dfc740eecc5cfc520c484452b01199809

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page