You often may need to compare one version of a file to an earlier one or check one file against a reference file. Linux provides several tools for doing this, depending on how deep a comparison you need to make.
The most common task involves comparing two text files. The tool of choice for this task is diff. With diff, you can compare two files line by line. By default, diff notices any differences between the two text files, no matter how small. This could be as simple as a space character being changed into a tab character from one file to the next. The file will look the same to a user, but diff will find that difference. The real power of diff comes from the options available to ignore certain kinds of differences between files. In the above example, you could ignore that change from a space character to a tab character by using the -b or --ignore-space-change options, which tell diff to ignore any differences in the amount of whitespace from one file to the next.
What about blank lines? The -B or --ignore-blank-lines options tell diff to ignore any changes in the number of blank lines from one file to the next. In this way, diff effectively looks only at the actual characters when comparing the files, narrowing diff’s focus to the actual content.
What if that’s not good enough for your situation? You may need to compare files where one was entered with Caps Lock turned on for some reason, or maybe the terminal being used was misconfigured. You may not want diff to report simple differences in case as “real” differences. In this situation, use the -i or --ignore-case options.
What if you’re working with files from a Windows box? Everyone who works on both Linux and Windows has run into the issue with line endings on text files. Linux expects only a single newline character, while Windows uses a carriage return and a newline character. diff can ignore this with the --strip-trailing-cr option.
diff’s output can take a few different formats. The default contains the line that is different, along with a number of lines right before and after the line in question. These extra lines are called context and can be set with the “-c”, “-C” or “--context=” options and the number of lines to use for context. This default output can be used by the patch program to change one file into the other. In this way, you can create source code patches to upgrade code from one version to the next. diff also can output differences between files that can be used by ed as a script with the -e or --ed options. diff also will output an RCS-format diff with the option -n or --rcs. Another option is to print out the differences in two columns, side by side, with the -y or --side-by-side options.
The diff utility compares only two files. What if you need to compare three files? diff3 comes to the rescue. This utility compares three files and prints out the diff statements. Again, you can use the -e option to print out a script suitable for the ed editor.
What if you simply want to see two files and how they differ? Another utility might be just what you are looking for, comm. With no other options, comm takes two files and prints out three columns. The first column contains lines unique to the first file, the second column contains lines unique to the second file, and the third column contains lines common to both files. You can suppress each of these columns selectively with the options -1, -2 and -3. They suppress columns 1, 2 or 3, respectively.
Although this works great for text files, what if you need to compare two binary files? You need some way to compare each and every byte in each file. Use the cmp utility, which does a byte-by-byte comparison of two files. The default output is a printout of which byte and line contains the difference. If you want to see what the byte values are, use the -b option. The -l option gives even more detail, printing out the byte count and the byte value from the two files.
With these utilities, you can start to get a better handle on how your files are changing. Here’s hoping you keep control of your files!
Joey Bernard has a background in both physics and computer science. This serves him well in his day job as a computational research consultant at the University of New Brunswick. He also teaches computational physics and parallel programming.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- My +1 Sword of Productivity
- Managing Linux Using Puppet
- Non-Linux FOSS: Caffeine!
- Doing for User Space What We Did for Kernel Space
- SuperTuxKart 0.9.2 Released
- Google's SwiftShader Released
- Parsing an RSS News Feed with a Bash Script
- Rogue Wave Software's Zend Server
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide