|
shell-cmd v1.2
Recursively find files matching a regex and execute a shell command for each match.
|
shell-cmd v1.2 — Recursively find files matching a regex and execute a shell command for each match. More...
#include "argz.hpp"#include <chrono>#include <cstdlib>#include <filesystem>#include <format>#include <grp.h>#include <iostream>#include <pwd.h>#include <regex>#include <signal.h>#include <span>#include <string>#include <sys/stat.h>#include <sys/wait.h>#include <unistd.h>#include <vector>Go to the source code of this file.
Classes | |
| struct | SizeFilter |
| Holds a parsed size filter with comparison operator and byte threshold. More... | |
| struct | TimeFilter |
| Holds a parsed modification-time filter with comparison operator and day count. More... | |
| struct | Options |
| Aggregates all runtime options parsed from the command line. More... | |
| struct | Stats |
| Tracks execution statistics printed in the summary. More... | |
Enumerations | |
| enum class | CmpOp { EQ , LT , GT } |
| Comparison operator for size and time filters. More... | |
Functions | |
| SizeFilter | parse_size_filter (const std::string &s) |
| Parse a size filter string into a SizeFilter. | |
| TimeFilter | parse_time_filter (const std::string &s) |
| Parse a time filter string into a TimeFilter. | |
| bool | matches_filters (const fs::directory_entry &entry) |
| Test a directory entry against all active metadata filters. | |
| bool | proc_cmd (const std::string &cmd, std::span< const std::string > text) |
| Substitute placeholders in a command template and execute the result. | |
| void | wait_for_slot () |
| Block until a parallel execution slot is available. | |
| void | wait_all () |
| Wait for all outstanding child processes to finish. | |
| std::string | replace_string (std::string orig, const std::string &with, const std::string &rep) |
| Replace all occurrences of a substring within a string. | |
| void | add_directory (const fs::path &path, const std::string &cmd, const std::string ®ex_str, std::vector< std::string > &args, int depth) |
| Recursively walk a directory, match entries against a regex and filters, and run commands. | |
| int | System (const std::string &command) |
| Execute a shell command using fork/exec with proper signal handling. | |
| void | print_help (const char *prog) |
| Print usage information to stdout. | |
| int | main (int argc, char **argv) |
| Program entry point. | |
Variables | |
| static Options | opts |
| Global runtime options. | |
| static Stats | stats |
| Global execution statistics. | |
| static std::vector< pid_t > | child_pids |
| PIDs of outstanding child processes (parallel mode). | |
| static bool | stop_requested = false |
| Set to true when stop-on-error triggers. | |
shell-cmd v1.2 — Recursively find files matching a regex and execute a shell command for each match.
Walks a directory tree using std::filesystem, applies metadata filters (size, time, permissions, ownership, type), substitutes placeholders in a command template, and executes the resulting command for every matched entry. Supports parallel execution, exclude patterns, confirm mode, and stop-on-error.
Definition in file cmd.cpp.
|
strong |
| SizeFilter parse_size_filter | ( | const std::string & | s | ) |
Parse a size filter string into a SizeFilter.
| s | Filter string, e.g. "+10M", "-1K", "4096". Prefix '+' means greater-than, '-' means less-than, no prefix means exact. Suffix K/M/G multiplies by 1024/1048576/1073741824. |
Definition at line 96 of file cmd.cpp.
References SizeFilter::active, SizeFilter::bytes, EQ, GT, LT, and SizeFilter::op.
Referenced by main().
| TimeFilter parse_time_filter | ( | const std::string & | s | ) |
Parse a time filter string into a TimeFilter.
| s | Filter string, e.g. "+7", "-1", "3". Prefix '+' means older-than, '-' means newer-than, no prefix means exact. |
Definition at line 131 of file cmd.cpp.
References TimeFilter::active, TimeFilter::days, EQ, GT, LT, and TimeFilter::op.
Referenced by main().
| bool matches_filters | ( | const fs::directory_entry & | entry | ) |
Test a directory entry against all active metadata filters.
| entry | The filesystem directory entry to check. |
Definition at line 153 of file cmd.cpp.
References EQ, GT, LT, and opts.
Referenced by add_directory().
| bool proc_cmd | ( | const std::string & | cmd, |
| std::span< const std::string > | text ) |
Substitute placeholders in a command template and execute the result.
Replaces %%0 (filename), %%1 (full path), %b (stem), %e (extension), and %%2+ (extra args). Supports confirm mode, dry-run, parallel forking, and stop-on-error.
| cmd | The command template string. |
| text | Span of strings: text[0] is the matched file path, text[1+] are extras. |
Definition at line 412 of file cmd.cpp.
References child_pids, opts, replace_string(), stats, stop_requested, System(), and wait_for_slot().
Referenced by add_directory().
| void wait_for_slot | ( | ) |
Block until a parallel execution slot is available.
Waits for any outstanding child process to finish, updates stats, and sets stop_requested if stop-on-error is enabled and a child failed.
Definition at line 370 of file cmd.cpp.
References child_pids, opts, stats, and stop_requested.
Referenced by proc_cmd().
| void wait_all | ( | ) |
Wait for all outstanding child processes to finish.
Called at the end of execution in parallel mode to drain the process pool.
Definition at line 390 of file cmd.cpp.
References child_pids, and stats.
Referenced by main().
| std::string replace_string | ( | std::string | orig, |
| const std::string & | with, | ||
| const std::string & | rep ) |
Replace all occurrences of a substring within a string.
| orig | The original string. |
| with | The substring to search for. |
| rep | The replacement text. |
Definition at line 232 of file cmd.cpp.
Referenced by proc_cmd().
| void add_directory | ( | const fs::path & | path, |
| const std::string & | cmd, | ||
| const std::string & | regex_str, | ||
| std::vector< std::string > & | args, | ||
| int | depth ) |
Recursively walk a directory, match entries against a regex and filters, and run commands.
| path | The directory to scan. |
| cmd | The command template containing placeholders. |
| regex_str | ECMAScript regex matched against each entry's full path. |
| args | Mutable argument vector; args[0] is overwritten with the matched path. |
| depth | Current recursion depth (0 at the root call). |
Definition at line 249 of file cmd.cpp.
References add_directory(), matches_filters(), opts, proc_cmd(), stats, and stop_requested.
Referenced by add_directory(), and main().
| int System | ( | const std::string & | command | ) |
Execute a shell command using fork/exec with proper signal handling.
Blocks SIGCHLD and ignores SIGINT/SIGQUIT in the parent process to prevent interrupted batch operations. Restores all signal masks after the child exits.
| command | The shell command string to execute via /bin/sh -c. |
Definition at line 314 of file cmd.cpp.
References System().
Referenced by proc_cmd(), and System().
| void print_help | ( | const char * | prog | ) |
Print usage information to stdout.
| prog | The program name (argv[0]). |
Definition at line 479 of file cmd.cpp.
Referenced by main().
| int main | ( | int | argc, |
| char ** | argv ) |
Program entry point.
Parses command-line arguments via argz, configures options, validates positional arguments and placeholder consistency, runs directory traversal, waits for parallel children, and prints a summary.
Definition at line 519 of file cmd.cpp.
References add_directory(), Argz< String >::addOptionSingle(), Argument< String >::arg_value, opts, parse_size_filter(), parse_time_filter(), print_help(), Argz< String >::proc(), stats, ArgException< String >::text(), and wait_all().
|
static |
Global runtime options.
Definition at line 66 of file cmd.cpp.
Referenced by add_directory(), main(), matches_filters(), proc_cmd(), and wait_for_slot().
|
static |
Global execution statistics.
Definition at line 75 of file cmd.cpp.
Referenced by add_directory(), main(), proc_cmd(), wait_all(), and wait_for_slot().
|
static |
PIDs of outstanding child processes (parallel mode).
Definition at line 76 of file cmd.cpp.
Referenced by proc_cmd(), wait_all(), and wait_for_slot().
|
static |
Set to true when stop-on-error triggers.
Definition at line 77 of file cmd.cpp.
Referenced by add_directory(), proc_cmd(), and wait_for_slot().