[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ A ] [ B ] [ C ] [ D ] [ next ]

User's Guide
Chapter 8 - Searching for Files and Text


Debian includes many tools for locating files. This chapter describes the tools for:

From the desktop, you can search with most common options and view files. However, you need to use the command line for:

First, however, you need to know something about how the Linux directory structure.


8.1 Understanding Linux Files

Linux treats everything on your hard drives and disks as a file. In practice, however, Linux uses three main types of object to store information:

[Note]

Strictly speaking, links and directories are specialized files. You can copy them or change their permissions exactly as you would with any other file.


8.1.1 Understanding Linux Names

The names of Linux files, links and directories follow certain rules:

[Note]

A forward slash by itself is shorthand for the root directory. You can use this shorthand on both the command line and the desktop.


8.1.2 Understanding File Extensions in Linux

Extensions are file name suffixes that start with a period. Usually, they are two or three letters long.

Linux can read many file extensions used by other platforms, such as

them for mime types. However, Linux does not always use extensions to identify file types.

Often, the directory where a file is placed is a better indication of what type of file it is. See Using Directories to Find Files, Section 8.2.1.

However, Linux does use a few regular extensions.

                           Extensions Used in Linux                       
     +-------------------------------------------------------------------+
     |Extension|                        File Type                        |
     |---------+---------------------------------------------------------|
     |.bz2     |A file compressed using bzip2 .                          |
     |---------+---------------------------------------------------------|
     |.c       |A file written in the C programming language.            |
     |---------+---------------------------------------------------------|
     |.conf    |A configuration file. In some cases, "conf" is used in a |
     |         |file name, but not as an extension.                      |
     |---------+---------------------------------------------------------|
     |.deb     |A Debian package for installing software.                |
     |---------+---------------------------------------------------------|
     |.lock    |A lock file that prevents the use of another file.       |
     |---------+---------------------------------------------------------|
     |         |A Red Hat package file. Although you can sometimes use   |
     |.rpm     |.rpm s in Debian, doing so can have unexpected   |
     |         |results and is not generally recommended. You can also   |
     |         |convert packages using the alien utility .               |
     |---------+---------------------------------------------------------|
     |.so      |A shared object (a library or module).                   |
     |---------+---------------------------------------------------------|
     |.src     |A source code file. Written in plain text, a source file |
     |         |must be compiled to be used.                             |
     |---------+---------------------------------------------------------|
     |.tar     |A single file made of a collection of files archived     |
     |         |using the tar command.                                   |
     |---------+---------------------------------------------------------|
     |         |A single file made of a collection of files archived     |
     |.tar.gz  |using the tar command, then compressed used the gzip     |
     |         |command.                                                 |
     |---------+---------------------------------------------------------|
     |         |A single file made of a collection of files archived     |
     |.tgz     |using the tar command, then compressed used the gzip     |
     |         |command.                                                 |
     |---------+---------------------------------------------------------|
     |.gz      |A file compressed using the gzip command.                |
     +-------------------------------------------------------------------+

8.2 Introducing the Linux Directory Structure

Linux's directory structure is based on unix 's. Like other operating systems, Linux arranges directories in a tree-like hierarchy, in which any directory may contain other-directories or files.

However, Linux's directory structure has several differences from those in other popular operating systems:

[Note]

Do not confuse the root directory (/) with the root user, the root user's home directory(/root), or the root partition (the hard drive partition which the root directory is on).

[Note]

To see which directory a partition is mounted on, view /etc/fstab in a text editor. On each line, the first column is the partition, and the second column is its mount point.

The advantage of this system is that you can position a partition almost anywhere on the system.


8.2.1 Using Directories to Find Files

Linux directories are consistent in their contents. The result is that a file's directory is usually a clue to what type of file it is.

For example, if you are looking for an executable program, it is probably in a /bin (short for "binary)" if it is a general or third party application or /usr/bin ("system binary") if it is a program that is an essential part of the operating system, or a maintenance utility.

The following table shows the directories where certain types of files are likely to be located. This structure is very similar in all Linux distributions, although small differences exist.

                         The Linux Directory Structure                    
     +-------------------------------------------------------------------+
     |Directory |                      What's In It                      |
     |----------+--------------------------------------------------------|
     |          |The root directory. Contains all other files and        |
     |          |directories.                                            |
     |/         |                                                        |
     |          |The complete or absolute path to other directories      |
     |          |starts from the root directory.                         |
     |----------+--------------------------------------------------------|
     |          |Binary or executable files that can be used by both the |
     |          |root user and normal user.                              |
     |/bin      |                                                        |
     |          |Look in other /bin directories for other executable     |
     |          |files.                                                  |
     |----------+--------------------------------------------------------|
     |          |Files needed to start Linux, including the kernel. Most |
     |/boot     |files in /boot generally cannot be configured using a   |
     |          |text editor.                                            |
     |----------+--------------------------------------------------------|
     |          |The usual mount point for the cd drive. If the system   |
     |/cdrom    |has a second cd drive, its mount point is usually /     |
     |          |cdrom2.                                                 |
     |----------+--------------------------------------------------------|
     |          |Definitions for drives, partitions, and other devices. /|
     |/dev      |dev also includes the makedev tool to create new        |
     |          |devices.                                                |
     |----------+--------------------------------------------------------|
     |          |Most of the system's configuration files. Including /etc|
     |/etc      |/X11 for the X Window system and /etc/opt for third     |
     |          |party applications.                                     |
     |----------+--------------------------------------------------------|
     |          |The usual mount point for the first floppy drive. If the|
     |/floppy   |system has a second floppy drive, its mount point is    |
     |          |usually /floppy2.                                       |
     |----------+--------------------------------------------------------|
     |          |Home directories for all regular users. The root user's |
     |          |home directory is /root.                                |
     |          |                                                        |
     |          |Subdirectories of /home are named for the user name of  |
     |/home     |each account.                                           |
     |          |                                                        |
     |          |Personal configuration files that start with a . (a     |
     |          |period) are in each home directory. These configuration |
     |          |files are not displayed by many commands unless you     |
     |          |select an option to display them.                       |
     |----------+--------------------------------------------------------|
     |/initrd   |Files for Linux's ram disk.                             |
     |----------+--------------------------------------------------------|
     |          |Programming libraries needed to start the system and run|
     |          |basic commands.                                         |
     |/lib      |                                                        |
     |          |Look for other lib directories scattered throughout the |
     |          |system.                                                 |
     |----------+--------------------------------------------------------|
     |/         |Files recovered when a check is run on a damaged file   |
     |lost+found|system.                                                 |
     |----------+--------------------------------------------------------|
     |          |A folder for other mount points, usually other          |
     |          |partitions not used directly by Linux, including those  |
     |          |for other operating systems.                            |
     |/mnt      |                                                        |
     |          |On some Linux distributions, /mnt includes /cdrom and / |
     |          |floppy directories, but this is not standard Debian     |
     |          |practice.                                               |
     |----------+--------------------------------------------------------|
     |          |Process and system information.                         |
     |          |                                                        |
     |/proc     |Be extremely careful of changes to /proc while you are  |
     |          |logged in as the root user, because they can easily     |
     |          |bring the system down.                                  |
     |----------+--------------------------------------------------------|
     |          |The home directory for the root user.                   |
     |/root     |                                                        |
     |          |Home directories for other users are generally          |
     |          |sub-directories of /home.                               |
     |----------+--------------------------------------------------------|
     |          |Executable files that are used only by the root user,   |
     |/sbin     |including general management and networking commands.   |
     |          |                                                        |
     |          |Other executables are in /bin.                          |
     |----------+--------------------------------------------------------|
     |          |Programs and system files that are shared by all users. |
     |          |                                                        |
     |/usr      |A common location for free software or for third-party  |
     |          |software. Important sub-directories include /usr/local  |
     |          |and /usr/share.                                         |
     |----------+--------------------------------------------------------|
     |/var      |Data files whose content changes frequently, such as    |
     |          |caches, spools, log files and user mailbox files.       |
     |----------+--------------------------------------------------------|
     |/var/lock |Lock files that prevent another user from opening or    |
     |          |saving particular files.                                |
     |----------+--------------------------------------------------------|
     |/var/log  |Log files for different programs. These logs are        |
     |          |invaluable for solving problems.                        |
     +-------------------------------------------------------------------+

8.2.2 Understanding Absolute and Relative File Paths

A file path is a list of the directories above the file, ending with the file name.

Linux uses two types of paths: absolute and relative paths:

In addition, ./ indicates the present working directory and the path to your home directory.


8.3 Searching from the Desktop

You can search for files or text from the desktop using:

If you want a more elaborate search, See Searching From the Command Line, Section 8.4.


8.3.1 Using the find Command From the Desktop

The find command is a tool for locating files. It can also do basic searches using strings and regular expressions.

img/Finding-2.png

The default choice is the File name. Select the option. When it displays, select the Add button to display the option in the window. You can disable an option temporarily by de-selecting the Enable button beside it or remove it from the display by selecting the Remove button.

[Note]

This window stays open so that the results of other searches can be added to it.

img/Finding-4.png


8.3.2 Using the locate Command From the Desktop

The locate command searches a database of file names and prints results that match the string that is entered for a command.

img/Finding-5.png

img/Finding-6.png


8.3.3 Searching From the GNOME Midnight Commander

img/Finding-8.png

[Note]

You can only search for results in directories in which you have read permission.

Using the buttons in the results window, you can:


8.4 Searching From the Command Line

The Linux command line includes many tools for locating files or information in them. These tools can be divided into two main categories:

Some of these commands, including grep , egrep and find can use variable text patterns (metacharacters or regular expressions) to extend their search abilities. See Using Metacharacters or Wild Cards, Section 5.3.6.


8.4.1 Searching for Files: Using the find Command

The basic tool for finding files is find . The find command is useful for locating files by their attributes or by their position in the directory tree.

You can search by a wide variety of criteria, including:

Usually, find starts with the current directory, then searches its sub-directories. However, you can specify the starting directory.

You can also specify how many levels of sub-directories down to search.

Type: find directory options argument .

                               The find Command                           
     +-------------------------------------------------------------------+
     |  Option   |        Action        |            Comments            |
     |-----------+----------------------+--------------------------------|
     |           |                      |Arguments include:              |
     |           |                      |                                |
     |           |Searches by minutes   |  * +number: More than the given|
     |-amin      |since files were      |    number of minutes ago.      |
     |argument   |accessed.             |  * -number: Less than the given|
     |           |                      |    number of minutes ago.      |
     |           |                      |  * number: Exactly the given   |
     |           |                      |    number of minutes ago.      |
     |-----------+----------------------+--------------------------------|
     |           |                      |Arguments are:                  |
     |           |                      |                                |
     |           |                      |  * +number: More than the given|
     |-atime     |Searches by days since|    number of days ago.         |
     |argument   |files were accessed.  |  * - number: Less than the     |
     |           |                      |    given number of days ago.   |
     |           |                      |  * number: Exactly the number  |
     |           |                      |    of days ago.                |
     |-----------+----------------------+--------------------------------|
     |           |                      |Arguments include:              |
     |           |                      |                                |
     |           |Searches by minutes   |  * +number: More than the given|
     |-cmin      |since files were      |    number of minutes ago,      |
     |argument   |changed.              |  * -number: Less than the given|
     |           |                      |    number of minutes ago.      |
     |           |                      |  * number: Exactly the given   |
     |           |                      |    number of minutes ago.      |
     |-----------+----------------------+--------------------------------|
     |           |                      |Arguments are:                  |
     |           |                      |                                |
     |           |                      |  * +number: More than the given|
     |           |Searches by the number|    number of days ago.         |
     |-ctime     |of days since file    |  * -number: Less than the given|
     |argument   |attributes were       |    number of days ago.         |
     |           |changed.              |  * number: Exactly the number  |
     |           |                      |    of days ago.                |
     |           |                      |                                |
     |           |                      |                                |
     |-----------+----------------------+--------------------------------|
     |-follow    |Traces symbolic links |                                |
     |           |to original files.    |                                |
     |-----------+----------------------+--------------------------------|
     |           |                      |The group can be identified by  |
     |           |Searches for files    |name or by group id .           |
     |-group     |that belong to a      |                                |
     |group      |particular group of   |This option is best used as root|
     |           |users.                |user. Otherwise, you can only   |
     |           |                      |find files to which you have    |
     |           |                      |read permission.                |
     |-----------+----------------------+--------------------------------|
     |-maxdepth  |Sets the maximum level|                                |
     |number     |of sub-directories to |                                |
     |           |search.               |                                |
     |-----------+----------------------+--------------------------------|
     |-mindepth  |Sets the minimum level|                                |
     |number     |of sub-directories to |                                |
     |           |search.               |                                |
     |-----------+----------------------+--------------------------------|
     |-name      |Searches for files    |Put a string in quotation marks |
     |string     |that match the string.|to use shell metacharacters.    |
     |-----------+----------------------+--------------------------------|
     |           |Finds files newer than|                                |
     |-newer file|one included in the   |.                               |
     |           |command.              |                                |
     |-----------+----------------------+--------------------------------|
     |-perm      |Searches with         |                                |
     |permissions|permissions in        |                                |
     |           |absolute mode.        |                                |
     |-----------+----------------------+--------------------------------|
     |           |                      |Arguments include:              |
     |-type      |Searches for types of |                                |
     |argument   |files.                |  * d: Directory.               |
     |           |                      |  * l: Symbolic link.           |
     |           |                      |  * f: Regular file.            |
     |-----------+----------------------+--------------------------------|
     |           |                      |Users can be identified by      |
     |           |Searches for files    |account name or by user id.     |
     |           |that belong to the    |                                |
     |-user user |user included in the  |This option is best used as root|
     |           |command.              |user. Otherwise, you can only   |
     |           |                      |find files for which you have   |
     |           |                      |read permission.                |
     +-------------------------------------------------------------------+

8.4.2 Searching for Files From the Command Line Using the locate Command

The locate command searches a database for files. It is often the quickest way to find a file.

Type: locate string . If you want to specify the database, add the -d path or --database = path option.

[Note]

You may want to install slocate ("secure locate"), a replacement for locate .


8.4.3 Searching for Text From the Command Line: Using grep Commands

Linux uses the grep command line tools to search for text in files. This family of tools contains three commands:

All three commands use a similar format. Type:

[Note]

To use grep or egrep as fully as possible, you should familiarize yourself with metacharacters. See Using Metacharacters With grep Commands, Section 8.4.4.

[Note]

To save or print search results, use redirection operators. See Redirecting Input and Output for Commands, Section 5.5.6.

                The grep, egrep, and fgrep Commands (Find Text)           
     +-------------------------------------------------------------------+
     |         Option          |      Action      |       Comment        |
     |-------------------------+------------------+----------------------|
     |                         |Treats the string |Used only with grep . |
     |-E                       |as an extended    |Similar to using egrep|
     |                         |regular           |.                     |
     |                         |expression.       |                      |
     |-------------------------+------------------+----------------------|
     |                         |Treats strings as |Used only with grep . |
     |-F                       |regular text.     |Similar to using fgrep|
     |                         |                  |.                     |
     |-------------------------+------------------+----------------------|
     |                         |Searches using a  |                      |
     |  * -f file              |text file         |Strings must be listed|
     |  * --file= file         |containing a list |one per line in the   |
     |                         |of patterns to    |file.                 |
     |                         |match.            |                      |
     |-------------------------+------------------+----------------------|
     |                         |Ignores the       |If this option is not |
     |  * -i                   |distinction       |used, lower case      |
     |  * --ignore-case        |between upper and |letters are           |
     |                         |lower case        |distinguished from    |
     |                         |letters.          |upper case ones.      |
     |-------------------------+------------------+----------------------|
     |  * -l                   |Lists files with  |Individual lines are  |
     |  * --files-with-matches |matches.          |not listed.           |
     |-------------------------+------------------+----------------------|
     |  * -L                   |Searches for files|                      |
     |  * --files-without-match|that do not match |                      |
     |                         |the string.       |                      |
     |-------------------------+------------------+----------------------|
     |  * -n                   |Displays lines    |                      |
     |  * --line-number        |numbered.         |                      |
     |-------------------------+------------------+----------------------|
     |                         |Sets the number of|                      |
     |-number                  |lines to display  |Sometimes referred to |
     |                         |before and after  |as "context."         |
     |                         |each match.       |                      |
     |-------------------------+------------------+----------------------|
     |  * -q                   |Stops at the first|Whether other matches |
     |  * --quiet              |match.            |are available is not  |
     |                         |                  |listed.               |
     |-------------------------+------------------+----------------------|
     |  * -r                   |Searches          |                      |
     |  * --recursive          |sub-directories.  |                      |
     |-------------------------+------------------+----------------------|
     |  * -v                   |Searches for lines|                      |
     |  * --invert-match       |that do not match |                      |
     |                         |the string.       |                      |
     |-------------------------+------------------+----------------------|
     |  * -x                   |Displays lines    |                      |
     |  * --line-regexp        |that match the    |                      |
     |                         |entire string.    |                      |
     +-------------------------------------------------------------------+

8.4.4 Using Metacharacters With grep Commands

Metacharacters are characters that can be used to set special conditions for searching. They are also known as wild cards. See Using Special Symbols, Section 5.5.5.1.

Metacharacters are added to a text string. A text string that includes metacharacters is called a regular expression. Regular expressions are useful for finding variations in wording or in the positioning of the string on a line.

Metacharacters can be used with both grep and egrep . However, grep can use fewer metacharacters than egrep . Whenever you want to use metacharacters, egrep is usually the tool to use.

If you do not want to use any metacharacters at all, or do not have the characters memorized for use, use fgrep .

Metacharacters can be extremely powerful, especially when used in combination. However, when you are using search tools, little is likely to go wrong unless you combine a grep command with another one that makes changes to files.

[Note]

If you want to search for a character that is usually used as a metacharacter, place a backward slash ( ) in front of it. For example, ? searches for a question mark, instead of any character.

Metacharacters are especially powerful when used together. For example:


8.5 Viewing Files From the Desktop

The desktop offers only limited choices for viewing files. For selective viewing of parts of files, use the command line instead. See Viewing Files From the Command Line, Section 8.6.

To view a file from the desktop:

img/Finding-11.png

img/Finding-12.png


8.6 Viewing Files From the Command Line

Linux includes so many tools for viewing files from the command line that some are widely considered obsolete.

For example, cat is less commonly used than it once was, probably because it has few options and does not allow scrolling. However, cat is still useful for a quick, no-frills view of a short file.

Similarly, although more is still a functional tool, for most people it has been replaced by less , whose name is a play on the saying that "less is more."

In general, less is the standard tool for viewing files. However, specialized commands also exist:


8.6.1 Viewing Short Files: Using the cat Command

The cat command is useful for viewing files that are less than a screen in length. For longer files that you want to scroll back and forth in, use less instead.

Type: cat options file.

                            The cat Command (View)                        
     +-------------------------------------------------------------------+
     |        Option        |        Action         |      Comments      |
     |----------------------+-----------------------+--------------------|
     |  * -E                |Displays line ends.    |Line ends are marked|
     |  * --show-ends       |                       |with $.             |
     |----------------------+-----------------------+--------------------|
     |  * -b                |Displays line numbers. |                    |
     |  * --number-nonblank |                       |                    |
     |----------------------+-----------------------+--------------------|
     |  * -T                |Displays tabs.         |Tabs are marked with|
     |  * --show-tabs       |                       |^I.                 |
     |----------------------+-----------------------+--------------------|
     |  * -v                |Displays non-printing  |                    |
     |  * --show-nonprinting|characters.            |                    |
     +-------------------------------------------------------------------+

8.6.2 Viewing Files with Scrolling: Using the less Command

The less command is the most common tool for viewing files from the command line. In this category, it replaces cat and more , offering more options than either.

The less command displays the designated file one screen at a time. In addition to displaying files, less can also be used for simple searches.

However, for most searches, you probably want to use one of the search commands. See Searching from the Desktop, Section 8.3 See Searching From the Command Line.

Type: less options file .

                            The less Command (View)                       
     +-------------------------------------------------------------------+
     |Option|            Topic            |           Comments           |
     |------+-----------------------------+------------------------------|
     |-h    |Sets maximum number of lines |                              |
     |number|to scroll back at one time.  |                              |
     |------+-----------------------------+------------------------------|
     |      |Ignores the distinction      |If this command is not used,  |
     |-i    |between upper and lower case |lower case letters are        |
     |      |letters when a string is     |distinguished from upper case |
     |      |used.                        |ones.                         |
     |------+-----------------------------+------------------------------|
     |-p    |Displays file starting with  |                              |
     |string|the first occurrence of the  |                              |
     |      |string.                      |                              |
     |------+-----------------------------+------------------------------|
     |-s    |Prints multiple blank lines  |                              |
     |      |as a single blanks line.     |                              |
     |------+-----------------------------+------------------------------|
     |-y    |Sets the maximum number of   |                              |
     |number|lines to scroll forward at   |                              |
     |      |one time.                    |                              |
     +-------------------------------------------------------------------+

8.6.3 Viewing Files Backwards: Using the tac Command

The tac command's name comes from the fact that it was intended as the opposite of cat . However, tac remains useful after less has replaced cat .

Type: tac file.

Although a few options exist, they are not often used.


8.6.4 Viewing the Start of Files: Using the head Command

head has a companion tool called tail , which views the end of files. The options for both tools are similar, but have some important differences.

For example, head ordinarily displays the name of the file, while tail has more options.

Type: head options file .

                            The head Command (View)                       
     +-------------------------------------------------------------------+
     |   Option   |                Action                 |   Comments   |
     |------------+---------------------------------------+--------------|
     |            |                                       |Units are:    |
     |            |Sets the size of the display, and,     |              |
     |            |optionally, the units.                 |  * b :       |
     |-c number   |                                       |    Blocks.   |
     |units       |If no unit is specified, the size is in|  * k :       |
     |            |bytes.                                 |    Kilobyes. |
     |            |                                       |  * m :       |
     |            |                                       |    Megabytes.|
     |------------+---------------------------------------+--------------|
     |  * -n      |                                       |              |
     |    number  |                                       |              |
     |  * --lines |Sets the number of lines to display.   |The default is|
     |    number  |                                       |10.           |
     |            |                                       |              |
     |            |                                       |              |
     |------------+---------------------------------------+--------------|
     |  * -q      |Prevents display of the file name      |              |
     |  * --quiet |header.                                |              |
     +-------------------------------------------------------------------+

8.6.5 Viewing the End of Files: Using the tail Command

tail has a companion tool called head , which views the start of files. The options for both tools are similar, but have some important differences.

For example, tail has more options, while head ordinarily displays the name of the file.

Type: tail options file .

                            The tail Command (View)                       
     +-------------------------------------------------------------------+
     |  Option   |        Action         |           Comments            |
     |-----------+-----------------------+-------------------------------|
     |           |Sets the size of the   |                               |
     |           |display, and,          |Units are:                     |
     |-c number  |optionally, the units. |                               |
     |unit       |                       |  * b : blocks                 |
     |           |If no unit is          |  * k: Kilobyes                |
     |           |specified, the size is |  * m: megabytes               |
     |           |shown in bytes.        |                               |
     |-----------+-----------------------+-------------------------------|
     |           |                       |Press Ctrl-C to end.           |
     |           |Continues to display   |                               |
     |-f         |the last lines of a    |This option is especially      |
     |           |file as it grows.      |useful if you have a log file  |
     |           |                       |open as you try to debug.      |
     |-----------+-----------------------+-------------------------------|
     |  * -n     |Sets lines to display, |                               |
     |    number |measured from the end  |The default is 10.             |
     |  * --lines|of the file.           |                               |
     |    number |                       |                               |
     |-----------+-----------------------+-------------------------------|
     |  * -q     |Prevents the display of|                               |
     |  * --quiet|the file name header.  |                               |
     |-----------+-----------------------+-------------------------------|
     |           |                       |Arguments are:                 |
     |-number    |Sets how much displays,|                               |
     |argument   |measured from the end  |  * l: Lines.                  |
     |           |of the file.           |  * b: Blocks.                 |
     |           |                       |  * c: Characters.             |
     |-----------+-----------------------+-------------------------------|
     |           |                       |Arguments are:                 |
     |+number    |Sets how much displays,|                               |
     |argument   |measured from the start|  * l: Lines.                  |
     |           |of the file.           |  * b: Blocks.                 |
     |           |                       |  * c: Characters.             |
     +-------------------------------------------------------------------+

8.7 Comparing Files from the Command Line

Linux includes three tools for comparing files automatically:

[Note]

To save the results of a comparison to a file, or to print a copy, use redirection pipes. See Redirecting Input and Output for Commands, Section 5.5.6


8.7.1 Making Simple Comparisons of Files: Using the comm Command

Use the comm command for a basic comparison of text files. For more advanced comparisons, use diff .

Type: comm options 1st_file 2nd_file .

The results are printed in three columns. From left to right, they are:

Use the - number option to keep a column from displaying. Arguments are 1 , 2 or 3 .

If you want to block two columns, enter them without any comma or space between them. For example, if you want to display only lines common to both files, enter -12 .

The comm command has no other options.


8.7.2 Making Complex Comparisons: Using the diff Command

Use the diff command if you want to:

Type: diff options 1st_ file 2nd_file .

In the results, text from the first file is marked with a lesser-than symbol ( < ), while text from the second file is marked with a greater-than symbol ( > ).

                         The diff Command (Comparing)                     
     +-------------------------------------------------------------------+
     |          Option           |       Action       |     Comments     |
     |---------------------------+--------------------+------------------|
     |                           |Ignores changes in  |                  |
     |  * -b                     |the amount of empty |                  |
     |                           |space.              |                  |
     |---------------------------+--------------------+------------------|
     |  * -B                     |Ignores blank lines.|                  |
     |  * --ignore-blank-lines   |                    |                  |
     |---------------------------+--------------------+------------------|
     |                           |Ignores the         |If this command is|
     |                           |distinction between |not used, lower   |
     |-i                         |upper and lower case|case letters are  |
     |                           |letters when a      |distinguished from|
     |                           |string is used.     |upper case ones.  |
     |---------------------------+--------------------+------------------|
     |                           |When comparing      |                  |
     |  * -x string              |directories,        |Metacharacters can|
     |  * --exclude string       |excludes directories|be used.          |
     |                           |that match the      |                  |
     |                           |string.             |                  |
     |---------------------------+--------------------+------------------|
     |                           |When comparing      |                  |
     |                           |directories,        |                  |
     |  * -X file                |excludes files and  |                  |
     |  * --exclude-from = file  |directories that    |                  |
     |                           |match the pattern   |                  |
     |                           |listed in file .    |                  |
     |---------------------------+--------------------+------------------|
     |                           |Displays 3 lines    |                  |
     |-c                         |before or after each|                  |
     |                           |result.             |                  |
     |---------------------------+--------------------+------------------|
     |  * -y                     |Displays results in |                  |
     |  * --side-by-side         |two columns.        |                  |
     |---------------------------+--------------------+------------------|
     |                           |Includes            |                  |
     |  * -r                     |sub-directories when|                  |
     |  * --recursive            |comparing           |                  |
     |                           |directories.        |                  |
     |---------------------------+--------------------+------------------|
     |  * -q                     |Only displays       |                  |
     |  * --brief                |whether files       |                  |
     |                           |differ.             |                  |
     |---------------------------+--------------------+------------------|
     |  * -s file                |Only displays when  |                  |
     |  * --report-identical-file|files do not differ.|                  |
     +-------------------------------------------------------------------+

8.7.3 Comparing Three Files: Using the diff3 Command

The diff3 command compares three files, and reports on the differences between them. It does not give detailed information about the differences, the way that diff does. diff3 can also be used to merge files using a simple script (see the man pages for more detail).

Type: diff3 options 1st_ file 2nd_file 3rd_file.

Two results may display:

                   The diff3 Command (Comparing and Merging)              
     +-------------------------------------------------------------------+
     |  Option   |           Topic            |         Comments         |
     |-----------+----------------------------+--------------------------|
     |-a         |Compares binary files       |Binary files are treated  |
     |           |                            |as text files.            |
     |-----------+----------------------------+--------------------------|
     |  * -m     |Displays merged differences |Output can also be        |
     |  * --merge|on the screen.              |redirected to a file      |
     +-------------------------------------------------------------------+

[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ A ] [ B ] [ C ] [ D ] [ next ]

User's Guide


Version: 1.00p00, 2006.06.17-21:39

Progeny Linux Systems, Inc.