How to Compare Text Files Using diff

If you need to compare two text files in Unix, you’re mostly likely to use the diff command.

Today I’ll talk about the simplest scenario: you want to compare two files and understand if there are any differences.

Suppose you have two files in /tmp directory:
/tmp/1.txt:

aaa bbb ccc ddd eee fff ggg

and /tmp/2.txt:

bbb
c c
ddd
eee
fff
ggg
hhh

I have deliberately created them so short and simple – this way it’s easier to explain how the comparison works. If there are no differences between the files, you will see no output, but if two text files are indeed different, all the text mismatches will be highlighted using the standard diff output:

$ diff /tmp/1.txt /tmp/2.txt
1d0
< aaa
3c2
< ccc
---
> c c
7a7
> hhh

Lines like “1d0 and “3c2 are the coordinates and types of the differences between the two compared files, while lines like “< aaa” and “> hhh” are the differences themselves.

Diff change notation includes 2 numbers and a character between them. Characters tell you what kind of change was discovered:

d – a line was deleted
c – a line was changed
a – a line was added

Number to the left of the character gives you the line number in the original (first) file, and the number to the right of the character tells you the line number in the second file used in comparison.

So, looking at the two text files and the diff output above, you can see what happened:

This means that 1 line was deleted. < aaa suggests that the aaa line is present only in the original file:

1d0
< aaa

And this means that the line number 3 has changed. You can see how this confirms that in the first file the line was “ccc”, and in the second it now is “c c”.

3c2
< ccc
---
> c c

Finally, this confirms that one new line appeared in the second file, it’s “hhh” in the line number 7:

7a7
> hhh

That’s all you need to know to start playing with text comparisons yourself. Stay tuned for more!

Useful command line options for diff

Here are just some of the options, I won’t demonstrate them but simply wanted you to know what’s possible:

-i option – allows you to ignore case when comparing lines (aaa will equal AaA, etc)
-r option – recursively compare directories (and any files found there)
-s option – report identical files (you can script around this if your task is to confirm which files are identical)

Related books

If you want to learn more, here’s a great book:

unix-power-tools
Unix Power Tools




How To Find Out Which Group a Unix User Belongs To

If you know the name of a particular user on your Unix system and just want to confirm the primary Unix group (gid) of this individual, just use the id command:

$ id -g greys
115

If Unix group id (gid) for the user doesn’t help you much and you’re interested in the Unix group name, use this:

$ id -gn greys
admins

If you want to get all the information about the user id and all the groups the user belongs to, it’s easier to use the default id command:

$ id greys
uid=1000(greys) gid=115(admins) groups=35(testgroup),115(admins)

See also:




How To Find Out User ID in Unix

There’s quite a few ways to confirm a user ID (uid) in Unix.

id command

This is probably one of the easiest ways to find out a uid of a particular user in your system:

# id -u greys
500

The most common way of using the id command is even simpler, and it gives you all the information about a user you may need:

# id greys
uid=500(greys) gid=500(greys) groups=500(greys)

This not only shows you the user id (uid), but also confirms user’s group id (gid) and all the rest Unix groups a user belongs to.

getent

Another way to get information about a user (including uid) is to use the getent command:

# getent passwd greys
greys:x:500:500:Gleb Reys:/home/greys:/bin/bash

In this example, greys is my username, 500 is the uid, and the second 500 in this line is my Unix group id (gid).

/etc/passwd file

If you know that the user you’re looking at is a local user (it is created on the local Unix system of yours), you can grep for it in /etc/passwd file directly:

$ grep greys /etc/passwd
greys:x:500:500:Gleb Reys:/home/greys:/bin/bash




How To Find Out RedHat Version And More

If you’re a really curious mind, you won’t be satisfied with simply knowing the current release of your RedHat Linux, that’s why there’s a few more commands you could use to satisfy your interest.

RedHat release

If you simply want to confirm whether you’re using a RHEL4, RHEL5 or any of the previous RedHat Linux releases, this is the first place to look:

bash-3.1$ cat /etc/redhat-release
Red Hat Enterprise Linux Client release 5 (Tikanga)

RedHat kernel version and type

Next step is to find out the exact Linux kernel version on your system, and also confirm whether it’s 64-bit or not:

bash-3.1$ uname -a
Linux rhserver123 2.6.18-8.el5 #1 SMP Fri Jan 26 14:15:14 EST 2007 x86_64 x86_64 x86_64 GNU/Linux

RedHat kernel build

For the most curious ones, here’s the last command. Use it to confirm who and when compiled the RedHat kernel you’re using, and what gcc compiler was used in the build process.

bash-3.1$ cat /proc/version
Linux version 2.6.18-8.el5 ([email protected]) (gcc version 4.1.1 20070105 (Red Hat 4.1.1-52)) #1 SMP Fri Jan 26 14:15:14 EST 2007

See also




How To Take A Screenshot in Unix (xwd)

Quite often there’s a need for you to take a screenshot of your Unix desktop, and as always there’s a number of ways to do it. Today I’m going to cover the command line approach to taking screenshots.

Taking a Screenshot with xwd

Most modern Unix desktop systems come with Gnome desktop environment by default, and use Xorg as their default X11 server. This means you are likely to have the xwd tool in your OS, which allows you to take screenshots.

Furthermore, many Unix distros come with hundreds of command-line tools bundled with the default OS install. Imagemagick is one of such bundled toolkits, and you can use the convert tool which is part of it for converting the xwd-generated screenshot into any graphics format of your preference.

Here’s how you use these two commands together:

bash-3.0$ xwd -root | convert - /tmp/screenshot.png

In this line, xwd command is invoked to take a full screenshot of your desktop. You then pipe its output to the convert tool and specify where you want this output saved to.

See Also




How To Mount An ISO image

Mounting an ISO image of a CD/DVD before burning it is one of the basic steps to verifying you’re going to get exactly the desired result. It’s also a neat trick to access files from a CD/DVD image when you only need a file or two and not a whole CD. Why burn it at all when you can access files much quicker and easier by simply mounting the ISO image?

Every Unix OS has a way to access ISO filesystem, and today I’ll only give you examples for Linux and Solaris. In both cases, the two things you need for the example to work are the ISO image itself and an available mount point (basically, an empty directory) on your filesystem to mount it under.

Here’s how to mount an ISO in Linux:

# mount -o loop /net/server/linux-bootcd.iso /mnt

Here’s how to mount an ISO in Solaris:

First, you need to associate your ISO image with a virtual device:

# lofiadm -a /net/server/linux-bootcd.iso

lofiadm approach allows you to have virtual devices associated with as many ISO images as you like, and you can view the list of current associations at any moment:

# lofiadm
Block Device File
/dev/lofi/1 /net/server/linux-bootcd.iso

To mount a virtual device, you use the following command:

# mount -F hsfs /dev/lofi/1 /mnt

See Also




How To: Use apt-get behind proxy

If you run your Ubuntu system behind a firewall and have to use proxy server for http and ftp access, then your apt-get on a newly installed Ubuntu system will probably not work.

To make it use proxy, simply set the http_proxy environment variable. Once you get it working (try something like apt-get update), you’ll probably want to add it to your .bashrc file.

Setting the http_proxy variable

Here’s how you set the variable:

bash$ export http_proxy=http://proxy:8080

If you’re required to use your username and password for your proxy, they can be specified this way. Obviously, port 8080 can and should be replaced with the valid port for your proxy server (sometimes it’s 3128):

bash$ export http_proxy=http://username:password@proxy:8080

See Also




How To: Find Out the Release Version of Your UNIX

Different UNIX-like operating systems store information about their release versions differently. If you know what OS you have, but not sure about the version, then here’s how you can find out:

RedHat Linux

bash-3.1$ cat /etc/redhat-release
Red Hat Enterprise Linux Client release 5 (Tikanga)

Ubuntu Linux

bash-3.1$ cat /etc/issue
Ubuntu 6.10 n l

SUSE Linux

~> cat /etc/SuSE-release
SUSE Linux Enterprise Desktop 10 (x86_64)
VERSION = 10

Sun Solaris

bash-2.03$ cat /etc/release Solaris 8 2/04 s28s_hw4wos_05a SPARC Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. Assembled 08 January 2004

See Also