rm-by-pattern is a powerful tool designed for managing files by identifying and organizing them based on filename patterns. It scans directories recursively, extracts patterns from filenames using regular expressions, and allows users to selectively remove files matching specific criteria. This document aims to guide users through the process of utilizing rm-by-pattern effectively.
To begin, rm-by-pattern can analyze all files within a directory (and its subdirectories) to identify and group filename patterns. This is particularly useful for handling files in a structured manner, such as images or documents with size dimensions or date stamps in their names.
For instance, running the following command:
rm-by-pattern . -r '\d+x\d+'
will examine each file in the current directory (and all subdirectories) to find matches for the regular expression \d+x\d+
, which targets patterns resembling dimensions (e.g., "600x400").
Given a set of files like:
- "0001-name-1-608x434-addition-text.jpg"
- "0001-name-2-400x434-addition-text.jpg"
- "0001-name-3-200x434-addition-text.jpg"
- "0001-name-4-608x434-addition-text.jpg"
- "0001-name-5-608x434-addition-text.png"
rm-by-pattern generates a YAML configuration file reflecting the identified patterns and file extensions, as shown below:
patterns:
- 608x434
- 400x434
- 200x434
extentions:
- jpg
- png
rmPatterns:
# User specifies patterns to remove here
rmExtentions:
# User specifies extensions to remove here
After generating the initial configuration, users can specify which patterns and file extensions to target for removal. This step involves manually editing the YAML file to add desired patterns and extensions under rmPatterns
and rmExtentions
.
For example, to remove all ".jpg" files with the "200x434" pattern, update the YAML configuration as follows:
patterns:
- 608x434
- 400x434
- 200x434
extentions:
- jpg
- png
rmPatterns:
- 200x434
rmExtentions:
- jpg
Finally, to execute the file removal process based on the specified criteria in the YAML configuration file (e.g., pattern.yml
), run:
rm-by-pattern . -c pattern.yml
This command will remove all files matching the user-defined patterns and extensions from the directory recursively, effectively cleaning up the file system as per user preferences.
- Go 1.20 or later.
- Access to both local and remote servers.
- SSH access to the remote server.
Using go install
:
go install github.com/asolopovas/rm-by-pattern@latest
Clone the repo and build the app:
git clone https://github.com/asolopovas/rm-by-pattern.git go build -o $ABSOLUTE_PATH_TO_RMBYPATTERN main.go
Add it to your system path or copy executable sudo cp rm-by-pattern /usr/local/bin/
.
rm-by-pattern operates on a config file (pattern.yaml
). Generate a default config file using -g
flag:
rm-by-pattern -g
rm-by-pattern -v
Open issues, submit pull requests, and share feedback.
MIT License.