How to Color diff Output? – Its Linux FOSS |
您所在的位置:网站首页 › linux的diff命令 › How to Color diff Output? – Its Linux FOSS |
In Linux, the “diff(difference)” command compares the files line by line and provides instructions like add, delete, and change to make them identical. However, the simple “diff” command does not provide a syntax highlighting feature that improves the readability of the output. For this purpose, the “–color” flag of the “diff” utility is beneficial in the form of the “diff –color” command. It displays the colorized output for better visual repression as compared to the “diff” command. This guide lists possible ways to get the output of the diff command: Use the “diff –color” Command Use the “colordiff” Command Sample Files:A sample file named “File.txt” is taken whose content is displayed in the terminal with the “cat” command followed by “-n(numbering)” flag: $ cat -n File1.txtAnother file “File2.txt” is displayed in the terminal that also contains 5 number of lines: $ cat -n File2.txtThis section carries out the various practical examples that demonstrate the working of “diff –color” command for colorizing the “diff” output. Example 1: Highlight Different ContextThe “diff –color” command without any argument scans the difference between two files. It also gives suggestions to add, remove, or change the lines that are different: $ diff --color File1.txt File2.txtThe above command has highlighted the different lines of “File1” and “File2”. The “4c4” instructs to change(c) the fourth line of File1 with the fourth line of File2. Example 2: Print Output in Unified FormatThe “diff –color” command is also compatible with the “-u(unified format)” flag. It displays the output in a unified format and prevents unnecessary lines i.e the same lines of compared files: $ diff --color -u File1.txt File2.txtIn the above output: The “File1.txt” name along with its timestamp is marked with “—” characters. Whereas the “+++” characters are used for “File2.txt”. The “@@” denotes the range of both files i.e “-1,5” of File1.txt and “+1,5” of File2.txt. The “–” symbol tells the user to delete the “red” line of File1.txt and “+” instructs that add the “green” line of File2.txt into FIle1. Example 3: Display Colorized OutputApart from the unified format the “diff –color” command can also colorize the complete output with the “-c” flag in this way: $ diff --color -c File1.txt File2.txtThe output is a little bit different from the unified format: The ******* acts as a separator between files content and the timestamp. The ***1,5**** shows the “File1.txt” range and the —1,5 —- represents the “File2.txt”. Red lines show the “File1.txt” content and green lines display “File2.txt” data. The “!” symbol denotes the different lines of File1 and File2 Example 4: Change the Default Colour SchemeBy default, the “diff –color” highlights the output in the “RGB” color pattern. In order to change the default color selection, the “palette” flag is used with the ANSI color code: $ diff --color --palette=':ad=36:de=33:ln=31' File1.txt File2.txtIn the above command, the “ad(add lines)” ANSI color code is “36(for Cyan)”. The “de(delete lines)” uses “33(yellow)” color code, and “ln(line number)” uses “31(red)”: The output shows the specified new color scheme. Example 5: Create Alias for “diff –color” TemporarilyIf the user wants to always display the colorized output of the “diff” command, then create an alias for it: $ alias diff='diff --color'The basic syntax follows for creating an alias is “alias [name]=[command]”. For instance, the “diff” alias is set to run the single-quoted “diff –color” command: The alias has been created successfully. Now, run the alias “diff” instead of the “diff –color” command in this format: $ diff File1.txt File2.txtThe above command has displayed the colorized output in the terminal. Example 6: Create Alias for “diff –color” PermanentlyThe above “alias” is created for temporarily basic that automatically deletes after the system shuts down. It can be created permanently by following this set of instructions. Open the “.bashrc” FileTo make the permanent alias open the “.bashrc” file in the “nano” text editor: $ nano .bashrcScroll down the opened “.bashrc” file and add the following lines to the end of it: $ alias diff='diff --color'Press “Ctrl+S” to save the file and “Ctrl+X” to exit the text editor. Update the “.bashrc” FileUpdate/reload the file to make the changes effective by using the “source” command: $ source ~/.bashrcAll the new changes have been made in the “.bashrc” file. Run the “diff” aliasNow execute the “diff” alias with the “-c(colorized)” flag to display the colorized output of compared files: $ diff -c File1.txt File2.txtThe newly created permanent “diff” alias has done its job successfully. Alternative: Use the “colordiff” CommandThe “–color” option comes in the “3.4” version of the “diff” command. If you are using an older version then the “diff –color” command may generate an error. In such situations use an external “colordiff” utility that performs the same as the “diff –color” command. Install the “colordiff” ToolFirst install the “colordiff” command line tool using the default package managers as per Linux distributions: $ sudo yum install colordiff # For RHEL/CentOS $ sudo dnf install colordiff # For Fedora $ sudo apt install colordiff # For Ubuntu/DebianThe installation of “colordiff” tool has been completed. Use the “colordiff” CommandThe “colordiff” command without any argument displays the same output as “diff –color”. Let’s see its practical implementation: $ colordiff File1.txt File2.txtThe “colordiff” command has scanned the different lines of File1 and File2. For getting the options list of the “colordiff” utility executes its “–help” command in the terminal: $ colordiff --helpScroll down the page for more flags/arguments. ConclusionIn Linux, the “diff” color command line tool offers the “–color” option that scans the difference between compared files and prints the colorized output. The user can also use the “colordiff” command line tool for the older versions of the “diff” command to colorize its output. This guide has described the complete procedure to color diff output. |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |