8

I am trying to patch a file.

The patch file is in the directory in which the list of files to be patched.

When I run commend

patch < file.patch

It prompts me

File to patch:

How to avoid this? What should I add in the patch file to automatically detect the filename that it should patch

4
  • 1
    What format of patch file are you working with? (Including the first few lines of the file will help identify this.) Commented Jun 13, 2016 at 3:27
  • 1
    A patch file is normally a unified diff (i.e. the output of diff -u), so it puts some filenames at the top that patch will recognize. But, the filenames could be a/subdir/file1 and your current directory is just above subdir. You may need to use patch's -p option (e.g. patch -p1 < ... or patch -p2 < ...). Adjust to suit Commented Jun 13, 2016 at 3:27
  • Continuing on Craig's comment, the -pX option tells patch to strip X components of the path (slashes) from the left. If you get the error unable to find a/path/to/filename, File to patch: and you know the file is at path/to/filename on your system, you would use path -p1 to strip the a/ from the beginning of the path allowing patch to find the file to patch on your system. Commented Jun 13, 2016 at 5:11
  • reg. pair.com/support/kb/paircloud-diff-and-patch, maybe diff -ruN folder1/ folder2/ > patchfile.patch ? Commented Apr 18, 2019 at 6:42

2 Answers 2

4

GNU version of patch includes a --batch or -t option, which prevents all prompting.

-t or --batch
   Suppress  questions  like  -f, but make some different assumptions: skip patches
   whose headers do not contain file names (the same as -f); skip patches for which
   the file has the wrong version for the  Prereq: line in the patch; and assume
   that patches are reversed if they look like they are.
Sign up to request clarification or add additional context in comments.

Comments

1

When creating the patch with diff, the output format, by default, does not includes the filename of the original filename. You can change this with the -u option like this...

$ diff -u OriginalFile NewFile >NewFile.patch

Then, you can simply...

$ patch -u <NewFile.patch

For further information, read about the unified GNU diff format.

1 Comment

That doesn't avoid the question/necesity of interaction, no matter which -p is specified, at least if NewFile is removed. There's no patch manpage or tutorial which explains what happens if the second file is absent - which is the only logical usecase for patching in my opinion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.