Command-line Utility (Groovy)
Author: RamJam
Directory Search and Replace is a Groovy-based command-line utility that searches and replaces text in .txt files within a specified directory and its subdirectories.
The tool supports:
- Case-sensitive and case-insensitive matching
- Recursive or shallow directory scanning
- Automatic file backups
- Detailed logging
- Interactive mode for user-friendly input
β±οΈ This project was completed in 1 day, including learning the Groovy language.
- π Exercise Link:
https://drive.google.com/file/d/1pE6Sx5q1eplGaQpd8HZqyxX707qh7PC3/view?usp=sharing
- Groovy (version
5.0.3) - Java Runtime Environment (JRE) or Java Development Kit (JDK)
Verify Groovy installation:
groovy -versionNo installation required.
- Clone or download the project.
- Ensure
groovyis available in your systemPATH.
groovy App.groovy <target_directory> <pattern> <replacement> <log_path>| Argument | Description |
|---|---|
<target_directory> |
Absolute or relative path to the directory to scan |
<pattern> |
String pattern to search for |
<replacement> |
String to replace the matched pattern |
<log_path> (optional) |
Directory where logs will be saved (defaults to <target_directory>/.log) |
| Flag | Description |
|---|---|
--shallow |
Scan only the top-level directory |
--ignore-case |
Perform case-insensitive matching |
--form |
Interactive mode |
groovy App.groovy /path/to/directory "old_text" "new_text"groovy App.groovy --form --ignore-case --shallow- Recursive scanning (default)
- Optional shallow scanning
- Case-sensitive and insensitive matching
- Automatic backups
- Detailed logging
- Robust error handling
- Modified files are overwritten
- Backups stored in:
<target_directory>/.backup/<process_id>/<filename> - Log file includes:
- Start and end times
- Modified files count
- Line and column changes
- Before/after content
- Processes only
.txtfiles - Hidden directories are skipped
- Each run uses a unique process ID
- Ensure directory exists and is accessible
- Verify Groovy installation
- Review log file for errors