Is it possible to tell patch not to generate .orig and .rej files?
I find it extremely annoying that patch creates these.
- 51,578
- 10
- 158
- 140
- 241
- 2
- 3
- 4
7 Answers
If you're not giving any option to patch other than -pN, it only creates those files when a patch fails to apply cleanly.
So, one option is to stop creating (or accepting) bad patches. :)
Back in the real world, this is a feature. When patch(1) fails to apply a patch segment to the original file, it saves the temporary original file copy out durably as *.orig, dumps the rejected segment to *.rej, and continues trying to apply patch segments. The idea is that you can open the *.rej file and complete the patch process manually by copying bits and pieces over to the patched file. The *.orig file can also be useful when the patch process accidentally wrecks something, and you need to refer to the original version to fix it.
I don't always fix a bad patch with text from the *.rej and *.orig files, but it's nice to have them in case I need them.
Once I've fixed up a bad patch, I run the following script at the project root to quickly clean things up:
#!/bin/bash
find . '(' \
-name \*-baseline -o \
-name \*-merge -o \
-name \*-original -o \
-name \*.orig -o \
-name \*.rej \
')' -delete
I call it cleanup-after-bad-patch because the long name partially insures against running this accidentally, since it could remove files you still need. To be honest, though, I normally run it by typing cleanTabEnter, that being sufficient to find this script in the PATH on my development machines.
The additional patterns it checks for are for the files output by my version control system of choice when it encounters the same problem during a merge operation. You may wish to adjust it for your VCS/SCM tools.
- 71,107
- 16
- 178
- 168
-
Easier is to not create the backup files with option `--no-backup-if-mismatch`. This can be combined with `--merge` to create inline merge info instead of .rej file. – mihca Aug 31 '21 at 12:28
-
@mihca: That option was added in [`patch` 2.4](https://git.savannah.gnu.org/cgit/patch.git/tree/NEWS), prior to the software's adoption as a GNU project, so it's probably "everywhere" by now, but only within the world that uses GNU `patch`, rather than some other version. Per that same reference, another way around this issue is to set the `POSIXLY_CORRECT` environment variable, but beware that this affects many tools other than GNU `patch`: `bash`, `grep`, and more. – Warren Young Aug 31 '21 at 13:57
The --no-backup-if-mismatch option will avoid the ".orig" files.
You might also want to try the --merge option, which creates an in-file conflict.
In all cases you should have some way to get back to a good state quickly if the merge becomes overwhelming.
- 281
- 3
- 5
To tell patch not to produce backups just omit the -b and any --backup-... options.
To instruct it not to create .rej files add -r - option to the command.
- 8,371
- 2
- 23
- 28
-
6This doesn't work for me – it just puts the rejects in a file called "-", which is a very very annoying filename to have around. I'm using version 2.5.8 on a Mac. – rjmunro Jan 28 '15 at 11:29
-
2working ok on `GNU patch 2.6` on mac perhaps try `-r /dev/null` – Stuart Cardall Mar 13 '19 at 16:50
-
`-r /dev/null` doesn't work. `saving rejects to file /dev/nullpatch: **** Can't remove file /dev/null : Operation not permitted` – Dan Burton Jan 07 '22 at 16:00
-
Since I see no way to get it to not output a rej file (on the default Mac version), I have simply resorted to dumping the file to /tmp. `--reject-file=/tmp/rej` – Dan Burton Jan 07 '22 at 16:02
I'm stuck with patch v2.5.4 where -r - causes it to create reject files named -.
I found that --reject-file= i.e. empty value causes patch to fail with exit code 2 IF it tries to write a reject file. If there are no rejects it works as expected. While not a complete solution for older version of patch, under some circumstances this may be acceptable or desired.
- 522,931
- 91
- 1,010
- 1,501
- 719
- 8
- 19
The best I could come up with (admittedly a way to sweep the dirt under the rug) is using -r <tmpfile>, i.e.:
# patch -r /tmp/deleteme.rej -i patchfile filetobepatched
since in v2.5.8, -r - actually creates the - file.
- 121
- 2
-
Unsatisfying, but this is the best solution out of all the answers given. – Dan Burton Jan 07 '22 at 16:04
Consider using the undocumented --version-control none option. That option is documented for the FreeBSD variant of patch.
- 111
- 3
patch -p1 -B /dev/null -r - < file.patch
- 1
-
-1: This is very bad advice. First, the `-B` flag doesn't send the `*.orig` output to `/dev/null`, as it appears to from your command. It just happens that normal users can't write to files called things like `/dev/nullfoo.cpp`. If you do this as root, you'll get junk in your `/dev` tree instead. Second, `-r -` doesn't suppress the `*.rej` file. It just appears to do that because the error due to the bogus `-B` flag stops it from showing you what it *really* would do without the `-B`, which is create a file called `-` in the current directory. – Warren Young Mar 19 '15 at 08:18
-
1@WarrenYoung according to `man patch`: "-r Put rejects into rejectfile instead of the default .rej file. When rejectfile is -, discard rejects." – Limbo Peng May 12 '15 at 09:02