FILE DISPLAY COMMANDS:
You can display the contents of files using several Linux commands. Here are some often-used commands for file display.
$cat
: This command is used to display the contents of a file on the standard output. It is often used to concatenate and display multiple files as well. For example:$ cat filename
more
: Themore
command is used to display the contents of a file one page at a time. You can scroll through the file using the Enter key or the spacebar. For example:$ more filename
less
: Similar tomore
, theless
command allows you to view file contents interactively. It provides more advanced features like backward scrolling and searching. For example:$ less filename
head
: This command displays the first few lines (by default, 10 lines) of a file. It is useful when you want to quickly view the beginning of a file. For example:$ head filename
tail
: Conversely, thetail
command displays the last few lines (by default, 10 lines) of a file. This is often used to monitor log files or view the latest entries in a file. For example:$ tail filename
COMPARE FILES (diff vs cmp) in Linux:
Linux offers a variety of functions for file comparison, including diff and cmp. These two commands are contrasted here:
$diff:
The main purpose of the diff command is to identify differences between two files. The files are compared line by line, with the different areas being highlighted. Here are a few crucial characteristics of diff:
Usage:
diff [options] file1 file2
Output: The command shows the differing lines between the two files, indicating additions, deletions, and modifications.
Context:
diff
provides context around the differing lines to give you a better understanding of the changes.Options:
diff
offers various options to control the output format, such as a unified format (-u
), context format (-c
), and suppressing common lines (-q
).Example usage of
diff
:
$ diff file1.txt file2.txt
Here are examples of using the output format options -u
(unified format), -c
(context format), and -q
(suppressing common lines) with the diff
command:
Unified Format (
-u
): The unified format is commonly used with thediff
command as it provides a compact and readable output format with context around the differences.Example: Let's assume we have two files,
file1.txt
andfile2.txt
, with the following contents:file1.txt:
Hello, this is file 1. Line 2 Line 3
file2.txt:
Hello, this is file 2. Line 2 Updated Line 3 Line 4
To compare these files using the unified format, you can execute the following command:
$ diff -u file1.txt file2.txt
Output:
diffCopy code--- file1.txt 2023-06-21 10:00:00.000000000 +0000 +++ file2.txt 2023-06-21 10:00:00.000000000 +0000 @@ -1,4 +1,4 @@ -Hello, this is file 1. +Hello, this is file 2. Line 2 -Line 3 +Updated Line 3 +Line 4
The output shows the differences between the files using the unified diff format. The lines starting with
-
represent lines removed fromfile1.txt
, while lines starting with+
represent lines added infile2.txt
.Context Format (
-c
): The context format is similar to the unified format but provides more context around the differences.Example: Using the same files as before (
file1.txt
andfile2.txt
), you can compare them using the context format:$ diff -c file1.txt file2.txt
Output:
diffCopy code*** file1.txt 2023-06-21 10:00:00.000000000 +0000 --- file2.txt 2023-06-21 10:00:00.000000000 +0000 *************** *** 1,4 **** ! Hello, this is file 1. Line 2 ! Line 3 --- 1,5 ---- ! Hello, this is file 2. Line 2 ! Updated Line 3 + Line 4
The output displays the differences in a context format. It provides a context around the differing lines and represents the added or removed lines using
!
.Suppressing Common Lines (
-q
): The-q
option is used to suppress the actual differences and display only a brief output indicating whether the files differ or are identical.Example: Let's consider two files,
file1.txt
andfile2.txt
, with identical contents:file1.txt:
This is a text.
file2.txt:
This is a text.
To compare these files and suppress the common lines, you can execute the following command:
$ diff -q file1.txt file2.txt
Output:
Files file1.txt and file2.txt are identical
The output indicates that the files are identical.
$cmp:
The
cmp
command is used to compare two files byte by byte and determine whether they are identical or different. Here are some key features:Syntax:
cmp [options] file1 file2
Differences Display:
cmp
does not display the actual differences between files but only provides an output based on the comparison result.Comparison Result: If the files are identical,
cmp
produces no output and returns no error code (exit status 0). If the files differ, it outputs the byte and line number where the first difference occurs and returns a non-zero exit status.Binary Comparison:
cmp
is suitable for comparing binary files or non-text files. It can handle files with null bytes or special characters.Partial Comparison:
cmp
allows you to compare only a portion of files by specifying the-n
option followed by the number of bytes to compare.Byte Comparison:
-b
or--print-bytes
: Displays the differing bytes in hexadecimal.$ cmp -b file1 file2
Quiet Mode:
-s
or--quiet
or--silent
: Suppresses output and only returns the exit status.$ cmp -s file1 file2
The command will exit silently if the files are identical, and it will return a non-zero exit status if they differ.
Limiting Comparison Size:
-n <bytes>
: Limits the comparison to the specified number of bytes.$ cmp -n 1024 file1 file2
The command will only compare the first 1024 bytes of the files.
Human-Readable Output:
-i
or--verbose
: Prints more verbose output, indicating the first differing byte and its position.$ cmp -i file1 file2
Displaying Line Numbers:
-l
or--verbose
: Prints the byte number and differing byte values for every difference found.$ cmp -l file1 file2
Comparison with Different Block Sizes:
-i <skip1>:<skip2>
or--ignore-initial=<skip1>:<skip2>
: Ignores the specified number of bytes from the beginning of each file.$ cmp -i 16:32 file1 file2
The command will skip the first 16 bytes in
file1
and the first 32 bytes infile2
before performing the comparison.
CONCLUSION:
It’s been great to have you all on my Blog!
Hope it helps you in your journey of learning the Operating System and I am glad that I am one of the part. Do follow for more and I am sure that I will engage you with profound insights.
Thank you!
#happylearning
TO CONNECT❤️: GITHUB.COM , LINKEDIN.COM