How to Patch (and Create Patches) a File on Linux

The Linux patch This command allows you to quickly and securely transfer changes from one set of files to another. Learn how to use it patch the easy way.

The patch and diff commands

Imagine you have a text file on your computer. You will receive a modified version of this text file from someone else. How do you quickly transfer all changes from the modified file to your original file? That’s where patch and diff come into the game. patch and diff can be found in Linux and other Unix-like operating systems such as macOS.

the diff Command investigated two different versions of a file and lists the differences between them. The differences can be saved in a file called a patch file.

the patch Command can read a patch file and use its contents as a series of instructions. If you follow these instructions, the changes will be in the modified file reproduced in the original File.

Now imagine that this process happens to a whole directory of text files. Everything in one fell swoop. That’s the power of patch.


Sometimes the changed files are not sent to you. You will only receive the patch file. Why send dozens of files when you can send a file or post a file for easy download?

What do you do with the patch file to actually patch your files? Aside from being almost a tongue twister, that’s a good question too. We’ll walk you through in this article.

the patch The command is most commonly used by people who work with software source code files, but it works equally well with any set of text files, regardless of their purpose, source code or not.

How to compare two text files in the Linux terminal

Our example scenario

In this scenario, we are in a directory called work, which contains two other directories. One is called work and the other is called late. The working directory contains a number of source code files. The latest directory contains the latest version of these source code files, some of which have been modified.

To be on the safe side, the working directory is a copy of the current version of the text files. It’s not the only copy of them.

Find the differences between two versions of a file

the diff Command finds the differences between two files. The default action is to list the changed lines in the terminal window.


A file is called slang.c. We compare the version in the working directory with the version in the newest directory.

the -u (unified) option says diff to also list some of the unchanged lines of text before and after each of the changed sections. These lines are called context lines. You help that patch Command pinpoints exactly where a change needs to be made in the original file.

We specify the names of the files so that diff knows which files to compare. The original file is listed first, then the modified file. This is the order we give diff:

diff -u working/slang.c latest/slang.c

diff generates an output list with the differences between the files. If the files were the same, no output at all would be listed. This kind of output from see diff confirms that there are differences between the two versions of the file and that the original file needs to be patched.

Create a patch file

Use the following command to capture these differences in a patch file. It’s the same command as above, with the output of diff redirected to a file called slang.patch.

diff -u working/slang.c latest/slang.c > slang.patch

The name of the patch file is arbitrary. You can call it what you want. It’s a good idea to give it the “.patch” extension. however, as it makes it clear what type of file it is.


close patch To respond to the patch file and change the working / slang.c file, use the following command. the -u (uniform) option leaves patch know that the patch file contains consistent lines of context. In other words, we used the -u option with diff, so we are using that -u Option with patch.

patch -u working.slang.c -i slang.patch

If all goes well, there is a single line of output telling you patch patches the file.

Make a backup copy of the original file

We can instruct patch to back up the patched files before modifying them -b (Backup) option. the -i (input) option tells patch the name of the patch file to use:

patch -u -b working.slang.c -i slang.patch

The file is patched as before with no noticeable difference in output. However, if you look in the working folder, you will see that the slang.c.orig file has been created. The date and time stamps of the files show that slang.c.orig is the original file and slang.c is a new file created by. was created patch.

Using diff with directories

We can use diff to create a patch file that contains any differences between files in two directories. We can then use this patch file as well patch to apply these differences to the files in the working folder with a single command.

The options we’re going to use diff are the -u (unified context) option we previously used the -r to make the (recursive) option diff look in any subdirectories and the -N (new file) option.


the -N Option says diff how to deal with files in the newest directory that are not in the working directory. It forces diff Include instructions in the patch file so thatpatch creates files that exist in the newest directory but are missing from the working directory.

You can group the options together so that they have a single hyphen (-).

Note that we are only providing the directory names, not revealing them diff to look at certain files:

diff -ruN working/ latest/ > slang.patch

slang.patch” width =”646″ height =”77″ src =”/pagespeed_static/1.JiBnMqyl6S.gif” onload =”pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);” onerror =”this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);”>

A look at the patch file

Let’s take a quick look at the patch file. We use less to look at its contents.

The beginning of the file shows the differences between the two versions of slang.c.

If we scroll further down through the patch file, we see that it then describes the changes in another file called structs.h. This will check that the patch file definitely contains the differences between different versions of several files.

Look before you jump

Patching a large collection of files can be a little annoying, so we’ll be using the --dry-run Opportunity to check that everything is okay before we take the plunge and commit to making the changes.


the --dry-run Option says patch doing anything but actually changing the files. patch performs all pre-flight checks on files and reports them if problems arise. In any case, no files are changed.

If no problems are reported, we can retry the command without that --dry-run Option and patch our files with confidence.

the -d (Directory) Tell option patch which directory is to be edited.

Note that we are not using this -i (Input) option to tell a story patch which patch file contains the instructions from diff. Instead, we will route the patch file in patch with <.

patch --dry-run -ruN -d working < slang.patch

From the entire directory, diff found two files to patch. The change instructions for these two files have been reviewed by patch , and no problems were reported.

Pre-flight checks are okay; we are ready to go.

Patch a directory

To really apply the patches to the files, let’s use the previous command without that --dry-run Opportunity.

patch -ruN -d working < slang.patch


This time, not every output line begins with “Check”, but every line begins with “Patch”.

And no problems are reported. We can compile our source code and we will be on the latest version of the software.

Settle your differences

This is by far the easiest and safest way to use patch. Copy your target files into a folder and patch that folder. Copy them back when you are satisfied that the patch completed without errors.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts