Navigating the Linux File System
◾
87
from top to bottom. Referring back to Figure 3.3,
if all of the directories foxr, temp2 and b
were empty, and you wanted to delete them all, then from /home you could issue the com-
mand
rmdir -p foxr/temp2/b
.
3.3.5 Textfile Viewing Commands
There are a number of commands that allow you to obtain information about files. Three
commands to display the contents of one or more files to the terminal window are
cat
,
less
, and
more
. The cat command (short for concatenate) displays the contents of all of
the files specified, without pausing. The cat command is more commonly used with redi-
rection so that the contents of multiple files can be combined and placed into a new file.
For
instance,
cat *.txt
>
all_text_files.txt
would take all .txt files of the current directory
and combine them into one file, also in the
current directory. The command
cat foo.txt
>
foo2.txt
is the same as
cp foo.txt foo2.txt
.
If you wish to quickly see the contents of one or more files, you can also use cat, but
because the contents will not pause screen-by-screen, you might want to use
more
or
less
. Both of these commands will display the contents of the file(s), but will pause after
each screen to let the user control what happens next.
In more, the user either hits the enter key to move one line ahead, the space bar to move
one
screen ahead, or q to quit. The more command has a number of options to help more
precisely control the program’s behavior, for instance by using
-n
number
to specify the
number of lines that appear in each screen or
+
linenumber
to start the display at the
given line number. The less command, which is newer and more useful, gives the user more
control as the user can scroll both forward
and backward through the file, for instance by
using the arrow keys.
You can also view the first part or last part of a file easily using the commands
head
and
tail
, respectively. These two commands display the first and last 10 lines of a file,
respectively. You can control the amount of the file displayed with options -c and -n. Both
options expect an integer number to follow the option to indicate the amount of the file to
display. The option -c is used to specify the number of bytes (characters) to output and -n
is used to specify the number of lines to output.
For head, you can also precede the integer with a minus sign to indicate that the pro-
gram should skip some number of bytes or lines. Similarly, for tail, precede the integer with
a plus sign to indicate the starting point within the file.
For instance,
head -c -20
would start at the 21st byte of the file. The instruction
tail -n
+
12
will display the file starting at line 12. Some additional examples are shown
in Table 3.5. Assume that file.txt is a text file that consists of 14 lines and 168 bytes.
88
◾
Linux with Operating System Concepts
Another file operation is
sort
, to sort a given file line-by-line in increasing order. The
option -r causes sort to work in decreasing order. Sort can work on multiple files in which
case the lines are mixed, as if the files were first combined using cat. Another useful option
for sort is -f which causes sort to ignore case so that ‘a’ and ‘A’ are treated equally (otherwise
‘A’ comes before ‘a’).
3.3.6 File Comparison Commands
The instructions
cmp
,
comm
, and
diff
are all available to compare the contents of text
files. The diff instruction can operate on any number of files or whole directories. Both cmp
and comm expect only two files and comm expects the files to be sorted.
In diff, for each line that is in the first file but not the second,
it is output preceded by a
‘
<
’ and for every line that is in the second file but not in the first, it is output preceded by a
‘
>
’. For each line or group of lines that differ, a summary is provided indicating how the two
differ. For instance, if file 1 contains a line that is not found in file 2, you will be told that
the line had to be deleted to match file 2. If file 2 contains a line that is not found in file 1,
you will be told that the line had to be added. If two corresponding lines between the two
files do not match, then you will be told that the line had to be changed. These are indicated
using the letters ‘a’
for added, ‘d’ for deleted, and ‘c’ for changed. This notation might look
like 3a5,6 to indicate that at line 3 of the first file, we had to add lines 5 to 6 of the second file.
If the first filename is a directory, diff will find the file in the directory whose name
matches that of the second file. The option -i will cause diff to ignore case so that upper
and lower case letters are treated as one. Other options will cause diff to ignore white space
(blanks, blank lines, tabs). If a file does not exist, diff responds with an error.
Additionally,
if diff is provided with two directories, it will compare all pairs of files who share the same
name in both directories. To operate on more than two files, you must supply diff with the
option --from-file
=
. For instance, if you wanted to compare file1 to all of file2, file3, file4,
and file5, you would use the following instruction:
diff --from-file=file1 file2 file3 file4 file5
In such a case, the output of the comparisons is separated by --- to indicate that the next
file is being compared.
The cmp instruction compares exactly two files. Similar to diff, it compares these files
byte-by-byte and line-by-line but it
stops once it finds a mismatch, returning the byte and
TABLE 3.5
Examples Using
head
and
tail
Do'stlaringiz bilan baham: