rnr 0.5.1

RnR is a command-line tool to rename multiple files and directories that supports regular expressions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<p align="center">
<img src="https://user-images.githubusercontent.com/8478202/107156909-59030580-6981-11eb-9374-95959b6ec067.png" width="350" height="350" alt="rnr">
</p>

<p align="center">
    <a href="https://github.com/ismaelgv/rnr/actions?query=workflow%3ARnR">
        <img src="https://github.com/ismaelgv/rnr/workflows/RnR/badge.svg" alt="Build Status"></a>
    <a href="https://crates.io/crates/rnr">
        <img src="https://img.shields.io/crates/v/rnr.svg" alt="Crates.io"></a>
    <a href="https://github.com/ismaelgv/rnr/blob/master/LICENSE">
        <img src="https://img.shields.io/crates/l/rnr.svg" alt="License"></a>
</p>

<p align="center">
    <b>RnR</b> is a command-line tool to <b>securely rename</b> multiple files
    and directories that supports regular expressions.
</p>

## Features
* Batch rename files and directories.
* Automated checks to avoid unwanted file collisions, removals or overwrites.
* Use regexp, including capture groups.
* Include directories recursively.
* Create backup files.
* Create and read operations from dump file.
* Undo operations from dump file.
* Exclude/include hidden files.
* Linux, Mac and Windows support, including terminal coloring.
* Extensive unit testing.
* Select limit of replacements.
* Apply text transformations to the replacements including capture groups.
* Convert UTF-8 file names to ASCII representation.

# Install

## Binaries

### GitHub Releases
You can download binaries from [latest release
page](https://github.com/ismaelgv/rnr/releases), choose the compressed
file corresponding to your platform. These compressed files contain the
executable and other additional content such as completion files (*Bash*, *Zsh*,
*fish* and *PowerShell*).

### Arch Linux
A package is available in the AUR
([`rnr`](https://aur.archlinux.org/packages/rnr/)) to install latest version of
*RnR* on Arch Linux.

### Homebrew
You can use [Homebrew package manager](https://brew.sh) to install this tool in macOS or Linux systems.
```sh
brew install rnr
```

## From Source
*RnR* is written in Rust. You can build it from source using Cargo.

### From git repository
```sh
git clone https://github.com/ismaelgv/rnr .
cargo install
```
### From Crates.io
```sh
cargo install rnr
```
# Usage

## Options
Check a detailed description of the application usage and all its options using: `rnr help`.

## Default behavior
* Checks all operations to avoid overwriting existing files.
* *Dry-run* by default.
* Only **UTF-8 valid** input arguments and filenames.
* Works on files and symlinks (ignores directories).
* Accepts multiple files as arguments.
* Accepts a **regex** to generate matches. These expressions have same
  limitations of `regex` crate. You can check regex syntax
  [here]https://docs.rs/regex/#syntax. It supports numbered and named *capture
  groups*.
* If max depth is not provided to recursive mode, it is assumed *infinite*.
* Does not generate backups.
* Output is *colored* (only ANSI terminals).
* Ignore hidden files and directories.
* Dump all operations into a file in force mode. This dump file can be used to
  undo these operations from `from-file` subcommand.
* Number of replacements set to one.

## Examples
* [Rename a list of files]#rename-a-list-of-files
    * [Include directories]#include-directories
    * [Multiple replacements]#multiple-replacements
    * [Combination with other UNIX tools]#combination-with-other-unix-tools
* [Recursive rename]#recursive-rename
    * [Recursive rename with max directory depth]#recursive-rename-with-max-directory-depth
    * [Recursive rename including directories and hidden files]#recursive-rename-including-directories-and-hidden-files
* [Undo/redo operations using dump file]#undoredo-operations-using-dump-file
* [Create backup files before renaming]#create-backup-files-before-renaming
* [Convert UTF-8 file names to ASCII]#convert-utf-8-file-names-to-ascii
* [Advanced regex examples]#advanced-regex-examples
    * [Replace extensions]#replace-extensions
    * [Replace numbers]#replace-numbers
    * [Capture groups]#capture-groups
    * [Capture several named groups and swap them]#capture-several-named-groups-and-swap-them
    * [Capture several groups and apply a transformation]#capture-several-groups-and-apply-a-transformation


__NOTE:__ If the regular expression `EXPRESSION` contains `-` as initial
character, the application with parse it as an argument. You need to use `--`
after flags and before the arguments, for example `rnr regex -f -- '-foo' '-bar'
[...]`.

__WINDOWS NOTE:__ In the examples that use `*`, you need to expand the wildcard
in PowerShell, for example: `rnr regex a b (Get-Item ./*)`. This is not supported in
`cmd.exe`.

### Rename a list of files
You can pass a list of files to be renamed as arguments:
```sh
rnr regex -f file renamed ./file-01.txt ./one/file-02.txt ./one/file-03.txt
```
*Original tree*
```
.
├── file-01.txt
├── file-02.txt
├── file-03.txt
└── one
    ├── file-01.txt
    ├── file-02.txt
    └── file-03.txt
```
*Renamed tree*
```
.
├── renamed-01.txt
├── file-02.txt
├── file-03.txt
└── one
    ├── file-01.txt
    ├── renamed-02.txt
    └── renamed-03.txt
```

#### Include directories
Directories are ignored by default but you can also include them to be renamed using the option `-D`.
```sh
rnr regex -f -D foo bar ./*
```
*Original tree*
```
.
├── foo
│   └── foo.txt
└── foo.txt
```
*Renamed tree*
```
.
├── bar
│   └── foo.txt
└── bar.txt
```

#### Multiple replacements
The replacement limit is set to 1 by default, but you can configure this limit
to replace multiple non-overlapping matches. All matches will be replaced if
this option is set to 0.

```sh
rnr regex -f -l 0 o u ./*
```
*Original tree*
```
.
├── foo.txt
├── foofoo.txt
├── foofoofoo.txt
└── foofoofoofoo.txt
```
*Renamed tree*
```
.
├── fuu.txt
├── fuufuu.txt
├── fuufuufuu.txt
└── fuufuufuufuu.txt
```

#### Combination with other UNIX tools
You can combine `rnr` with other UNIX tools using pipes to pass arguments.

##### Find files older than 1 day and rename them
```sh
find . -type f +mtime 1 | xargs rnr regex -f file renamed
```

##### Read list of files from a file
```sh
cat file_list.txt | xargs rnr regex -f file rename
```

`file_list.txt` content:
```
file-01.txt
one/file-02.txt
one/file-03.txt
```

### Recursive rename
If recursive (`-r`) option is passed, `rnr` will look for al files in the path recursively without depth limit.
```sh
rnr regex -f -r file renamed ./
```
*Original tree*
```
.
├── file-01.txt
├── file-02.txt
├── file-03.txt
└── one
    ├── file-01.txt
    ├── file-02.txt
    ├── file-03.txt
    └── two
        ├── file-01.txt
        ├── file-02.txt
        ├── file-03.txt
        └── three
            ├── file-01.txt
            ├── file-02.txt
            └── file-03.txt
```
*Renamed tree*
```
.
├── renamed-01.txt
├── renamed-02.txt
├── renamed-03.txt
└── one
    ├── renamed-01.txt
    ├── renamed-02.txt
    ├── renamed-03.txt
    └── two
        ├── renamed-01.txt
        ├── renamed-02.txt
        ├── renamed-03.txt
        └── three
            ├── renamed-01.txt
            ├── renamed-02.txt
            └── renamed-03.txt
```
#### Recursive rename with max directory depth
Similarly, you can set a maximum directory depth in combination with recursive operations.
```sh
rnr regex -f -r -d 2 file renamed ./
```
*Original tree*
```
.
├── file-01.txt
├── file-02.txt
├── file-03.txt
└── one
    ├── file-01.txt
    ├── file-02.txt
    ├── file-03.txt
    └── two
        ├── file-01.txt
        ├── file-02.txt
        ├── file-03.txt
        └── three
            ├── file-01.txt
            ├── file-02.txt
            └── file-03.txt
```
*Renamed tree*
```
.
├── renamed-01.txt
├── renamed-02.txt
├── renamed-03.txt
└── one
    ├── renamed-01.txt
    ├── renamed-02.txt
    ├── renamed-03.txt
    └── two
        ├── file-01.txt
        ├── file-02.txt
        ├── file-03.txt
        └── three
            ├── file-01.txt
            ├── file-02.txt
            └── file-03.txt
```

#### Recursive rename including directories and hidden files
`rnr` ignore hidden files by default to speed up the operations and avoid problems with some particular directories like `.git/` or `.local/`. You can include hidden files passing `-x` option. Also, you can use include directories `-D` option with `-r` too.
```sh
rnr regex -f -r -D -x foo bar ./
```
*Original tree*
```
.
├── .foo_hidden_file.txt
├── foo.txt
├── foo
│   ├── foo.txt
│   └── foo
│       └── foo.txt
└── .foo_hidden_dir
    └── foo.txt
```
*Renamed tree*
```
.
├── .bar_hidden_file.txt
├── bar.txt
├── bar
│   ├── bar.txt
│   └── bar
│       └── bar.txt
└── .bar_hidden_dir
    └── bar.txt
```

### Undo/redo operations using dump file
When you perform a renaming operation, `rnr` will create by default a dump file in the current directory you executed the command. This file can be used to easily revert the operations using `from-file` and `-u` option.

*Rename operation*
```sh
rnr regex -f foo bar ./*
```
*Undo previous operation*
```sh
rnr from-file -f -u rnr-[timestamp].json
```

If you want to redo the operation just pass the dump file without any additional argument:
```sh
rnr from-file -f rnr-[timestamp].json

```

### Create backup files before renaming
`rnr` can create backup files before renaming for any operation passing `-b` option. The backup files names are ensured to be unique and won't be overwritten if another backup is created. If you are working with many large files, take into account that files will be duplicated.

```sh
rnr regex -f -b file renamed ./*
```

*Original tree*
```
.
├── file-01.txt
├── file-02.txt
└── file-03.txt
```
*Renamed tree*
```
.
├── file-01.txt.bk
├── file-02.txt.bk
├── file-03.txt.bk
├── renamed-01.txt
├── renamed-02.txt
└── renamed-03.txt
```

### Convert UTF-8 file names to ASCII
`rnr`can convert UTF-8 file names to their ASCII representation. This feature uses
[AnyAscii library](https://github.com/anyascii/anyascii) to perform the
transliteration. To avoid conflicts with paths, the characters that would be translated
to `/` are changed to `_` instead.

You can run:
```sh
rnr to-ascii ./*
```
Or:
```sh
rnr to-ascii -r .
```

*Original tree*
```
.
├── fïlé-01.txt
├── FïĹÊ-02.txt
└── file-03.txt
```
*Renamed tree*
```
.
├── file-01.txt
├── FILE-02.txt
└── file-03.txt
```

### Advanced regex examples
More info about regex used [in the `regex` package](https://docs.rs/regex).
#### Replace extensions
```
rnr regex -f '\..*$' '.txt' ./*
```
*Original tree*
```
.
├── file-01.ext1
├── file-02.ext2
└── file-03.ext3
```
*Renamed tree*
```
.
├── file-01.txt
├── file-02.txt
└── file-03.txt
```

#### Replace numbers
```
rnr regex -f '\d' '1' ./*
```
*Original tree*
```
.
├── file-01.txt
├── file-02.txt
└── file-03.txt
```
*Renamed tree*
```
.
├── file-11.txt
├── file-12.txt
└── file-13.txt
```
#### Capture groups
1. Capture three unnamed groups [`name(1)-number(2).extension(3)`].
2. Swap group 1 (name) and group 2 (number).
```sh
rnr regex -f '(\w+)-(\d+).(\w+)' '${2}-${1}.${3}' ./*
```
*Original tree*
```
.
├── file-01.txt
├── file-02.txt
└── file-03.txt
```
*Renamed tree*
```
.
├── 01-file.txt
├── 02-file.txt
└── 03-file.txt
```
__SHELL NOTE:__ In shells like Bash and zsh, make sure to wrap the `REPLACEMENT`
pattern in single quotes. Otherwise, capture group indices will be replaced by
expanded shell variables.

#### Capture several named groups and swap them
1. Capture two digits as `number`.
2. Capture extension as `ext`.
3. Swap groups.
```sh
rnr regex -f '(?P<number>\d{2})\.(?P<ext>\w{3})' '${ext}.${number}' ./*
```
*Original tree*
```
.
├── file-01.txt
├── file-02.txt
└── file-03.txt
```
*Renamed tree*
```
.
├── file-txt.01
├── file-txt.02
└── file-txt.03
```

#### Capture several groups and apply a transformation

1. Capture three unnamed groups [`name(1)-number(2).extension(3)`].
2. Swap group 1 (name) and group 2 (number).
3. Transform replacement to uppercase.

```sh
rnr regex -f -t upper '(\w+)-(\d+)' '${2}-${1}' ./*
```
*Original tree*
```
.
├── file-01.txt
├── file-02.txt
└── file-03.txt
```
*Renamed tree*
```
.
├── 01-FILE.txt
├── 02-FILE.txt
└── 03-FILE.txt
```