{"id":17654,"library":"foreach-cli","title":"foreach-cli: Execute Command Per File","description":"foreach-cli is a command-line utility designed to execute a specified shell command for each file that matches a given glob pattern. Currently at version 1.8.1, the package provides a stable and reliable method for batch processing files. It differentiates itself from its predecessor, `each-cli`, by consistently setting the current working directory (CWD) to where `foreach-cli` was invoked, rather than changing it to the matched file's directory. This design choice simplifies command construction and prevents unexpected path issues. Additionally, it accepts command arguments as direct strings, enabling complex shell commands, including piping and redirects, that might be problematic with other tools. The utility supports various placeholders like `#{path}`, `#{dir}`, and `#{name}` to dynamically inject file-specific information into the executed commands.","status":"active","version":"1.8.1","language":"javascript","source_language":"en","source_url":"https://github.com/danielkalen/foreach-cli","tags":["javascript","cli","foreach","each","command","foreach-command","run","line"],"install":[{"cmd":"npm install foreach-cli","lang":"bash","label":"npm"},{"cmd":"yarn add foreach-cli","lang":"bash","label":"yarn"},{"cmd":"pnpm add foreach-cli","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While the `index.js` exports a `Cli` class, `foreach-cli` is primarily a command-line utility. Direct programmatic import and usage are uncommon and undocumented; most users interact with it via the shell. The internal API is not stable for general consumption.","wrong":"const foreachCli = require('foreach-cli');","symbol":"foreachCli","correct":"import { Cli } from 'foreach-cli'; // for programmatic usage, typically not required"}],"quickstart":{"code":"npm install foreach-cli\n\n# Create some dummy files\necho \"content A\" > folder1/fileA.txt\necho \"content B\" > folder2/fileB.log\necho \"content C\" > folder1/fileC.js\n\n# Example 1: Count lines in all .txt files\n# This command will execute 'wc -l' for each .txt file in the current directory or subdirectories.\n# The #{path} placeholder is replaced with the full path to the matched file.\nforeach -g \"**/*.txt\" -x \"echo 'Processing #{path}:'; wc -l #{path}\"\n\n# Example 2: Rename all .log files to .oldlog\n# This demonstrates using multiple placeholders and shell commands for file manipulation.\nforeach -g \"**/*.log\" -x \"echo 'Renaming #{path} to #{dir}/#{name}.oldlog'; mv #{path} #{dir}/#{name}.oldlog\"\n\n# Verify the renaming\nls -R","lang":"bash","description":"Demonstrates installation and basic command-line usage to process files matching glob patterns."},"warnings":[{"fix":"Review all commands, especially those using relative paths, to ensure they correctly reference files given the consistent CWD. Use explicit placeholders like `#{path}` or `#{dir}` where ambiguity might arise.","message":"This package is a complete rewrite of `each-cli`. The most significant breaking change is how the current working directory (CWD) is handled. `foreach-cli` maintains the CWD of the original invocation, whereas `each-cli` changed the CWD to the directory of each matched file. This requires updating commands that rely on relative paths.","severity":"breaking","affected_versions":">=1.0.0 (compared to `each-cli`)"},{"fix":"Always enclose the command argument for `-x` within double quotes (e.g., `-x \"command | another_command\"`) to ensure it's treated as a single string.","message":"When executing complex commands with pipes or special characters, ensure the entire command passed to `-x` is properly quoted. Without quoting, the shell might interpret parts of your command as arguments to `foreach-cli` itself.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Prefer using `foreach-cli` directly from the shell or via `child_process.exec`/`child_process.spawn` if integrating into a Node.js script. Do not directly import the `Cli` class for application logic unless you are prepared for potential internal API changes.","message":"The primary use case for `foreach-cli` is command-line execution. While there's a programmatic `Cli` class export, its API is undocumented and not intended for general programmatic consumption. Relying on it may lead to unstable behavior across versions.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Install the package globally (`npm install -g foreach-cli`) or use `npx foreach-cli ...` to execute it without global installation.","cause":"The `foreach` executable is not in your system's PATH.","error":"foreach: command not found"},{"fix":"Check the command you passed to `-x` (e.g., `tar xvf #{path}`). Run it manually on a single file to debug syntax errors, incorrect paths, or missing binaries. Ensure necessary tools are installed and in the PATH for `foreach-cli`'s environment.","cause":"The command executed by `foreach-cli` (specified with `-x`) failed. This error is typically a pass-through from the underlying shell command.","error":"Error: Command failed: <your command>"},{"fix":"Verify your glob pattern syntax. Ensure you are running `foreach-cli` from the correct directory where the files reside. Use an absolute glob path if needed, or check for typos in file extensions/names.","cause":"The provided glob pattern (`-g`) did not match any files in the current working directory or its subdirectories.","error":"No files found matching glob: <your glob>"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}