# replace-in-files-cli

> Replace matching strings and regexes in files

Latest version **4.0.0** (published 2025-09-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install replace-in-files-cli
pnpm add replace-in-files-cli
yarn add replace-in-files-cli
bun add replace-in-files-cli
```

Provides the command `replace-in-files`.

## Health

**Score 40/100 (D)** — status: stable.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2025-09-11 |
| First published | 2019-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 5 |
| Unpacked size | 12.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 180 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | cli-app, cli, replace, matching, match, matches, find, search, string, regex, regexp, pattern, files, file, text, contents |

## Links

- npm: https://www.npmjs.com/package/replace-in-files-cli
- Repository: https://github.com/sindresorhus/replace-in-files-cli
- Homepage: https://github.com/sindresorhus/replace-in-files-cli#readme
- Issues: https://github.com/sindresorhus/replace-in-files-cli/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/replace-in-files-cli

## Dependencies (5)

- [meow](https://npm.io/package/meow.md) ^13.2.0
- [globby](https://npm.io/package/globby.md) ^14.1.0
- [normalize-path](https://npm.io/package/normalize-path.md) ^3.0.0
- [write-file-atomic](https://npm.io/package/write-file-atomic.md) ^6.0.0
- [escape-string-regexp](https://npm.io/package/escape-string-regexp.md) ^5.0.0

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 4.0.0 (latest) — 2025-09-11
- 3.0.0 — 2024-06-26
- 2.2.0 — 2023-05-14
- 2.1.0 — 2023-05-08
- 2.0.0 — 2021-11-09
- 1.0.0 — 2020-09-29
- 0.3.1 — 2020-01-19
- 0.3.0 — 2019-09-05
- 0.2.0 — 2019-07-29
- 0.1.0 — 2019-06-12

## README

# replace-in-files-cli

> Replace matching strings and regexes in files

## Install

```sh
npm install --global replace-in-files-cli
```

## Usage

```
$ replace-in-files --help

  Usage
    $ replace-in-files <files…>

  Options
    --regex           Regex pattern to find  (Can be set multiple times)
    --string          String to find  (Can be set multiple times)
    --replacement     Replacement string  (Required)
    --ignore-case     Search case-insensitively
    --no-glob         Disable globbing
    --dry-run         Show what would be replaced without making changes

  Examples
    $ replace-in-files --string='horse' --regex='unicorn|rainbow' --replacement='🦄' foo.md
    $ replace-in-files --regex='v\d+\.\d+\.\d+' --replacement=v$npm_package_version foo.css
    $ replace-in-files --string='blob' --replacement='blog' 'some/**/[gb]lob/*' '!some/glob/foo'
    $ replace-in-files --dry-run --string='old' --replacement='new' file.txt

  You can use the same replacement patterns as with `String#replace()`, like `$&`.

  When working with quotes in shell commands, escape them with backslashes:
    $ replace-in-files --string='\"use strict\";' --replacement='\"use strict\";\nrequire(\"module\");' file.js
```

Real-world use-case: [Bumping version number in a file when publishing to npm](https://github.com/sindresorhus/modern-normalize/commit/c1d65e3f7daba2b695ccf837d2aef19d586d1ca6)

The regex should be [JavaScript flavor](https://www.regular-expressions.info/javascript.html).

## API

You can also use this package programmatically:

```js
import replaceInFiles from 'replace-in-files-cli';

// Find and replace
await replaceInFiles('*.js', {
	find: ['old', /version \d+/],
	replacement: 'new'
});

// Transform entire file content
await replaceInFiles('*.js', {
	transform: (content, filePath) => `/* Banner */\n${content}`
});

// Combine find/replace with transform (transform runs after find/replace)
await replaceInFiles('*.js', {
	find: ['old'],
	replacement: 'new', 
	transform: (content, filePath) => `/* ${filePath} */\n${content}`
});
```

The `transform` option provides full control over file content:
- **Standalone**: Use alone for prepend, append, or complex transformations
- **Combined**: Use with `find`/`replacement` - transform runs after find/replace operations
- **Parameters**: Receives `(content, filePath)` for context-aware transformations

---
_Source: https://npm.io/package/replace-in-files-cli · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
