foreach-cli: Execute Command Per File
raw JSON →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.
Common errors
error foreach: command not found ↓
npm install -g foreach-cli) or use npx foreach-cli ... to execute it without global installation. error Error: Command failed: <your command> ↓
-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. error No files found matching glob: <your glob> ↓
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. Warnings
breaking 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. ↓
gotcha 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. ↓
gotcha 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. ↓
Install
npm install foreach-cli yarn add foreach-cli pnpm add foreach-cli Imports
- foreachCli wrong
const foreachCli = require('foreach-cli');correctimport { Cli } from 'foreach-cli'; // for programmatic usage, typically not required
Quickstart
npm install foreach-cli
# Create some dummy files
echo "content A" > folder1/fileA.txt
echo "content B" > folder2/fileB.log
echo "content C" > folder1/fileC.js
# Example 1: Count lines in all .txt files
# This command will execute 'wc -l' for each .txt file in the current directory or subdirectories.
# The #{path} placeholder is replaced with the full path to the matched file.
foreach -g "**/*.txt" -x "echo 'Processing #{path}:'; wc -l #{path}"
# Example 2: Rename all .log files to .oldlog
# This demonstrates using multiple placeholders and shell commands for file manipulation.
foreach -g "**/*.log" -x "echo 'Renaming #{path} to #{dir}/#{name}.oldlog'; mv #{path} #{dir}/#{name}.oldlog"
# Verify the renaming
ls -R