< Zurück | Inhalt | Weiter >

1.3.7 File Contents

Let’s look at a directory of files. How do you know what’s there? We can start with an ls to list the names:


$ ls

ReadMe.txt Shift.java dispColrs moresrc Shift.class anIcon.gif jam.jar moresrc.zip

$


That lists them alphabetically, top to bottom, then left to right, arranged so as to make the most use of the space while keeping the list in columns. (There are options for other orderings, single column, and so on.)

An ls without options only tells us the names, and we can make some guesses based on those names (for example, which file is Java source, and which


is a compiled class file). The long listing ls -l will tell us more: permissions, links, owner, group, size (in bytes), and the date of last modification.


$ ls -l


total 2414

-rw-r--r--

1

albing

users

132

Jan

22

07:53

ReadMe.txt

-rw-r--r--

1

albing

users

637

Jan

22

07:52

Shift.class

-rw-r--r--

1

albing

users

336

Jan

22

07:55

Shift.java

-rw-r--r--

1

albing

users

1374

Jan

22

07:58

anIcon.gif

-rw-r--r--

1

albing

users

8564

Jan

22

07:59

dispColrs

-rw-r--r--

1

albing

users

1943

Jan

22

08:02

jam.jar

drwxr-xr-x

2

albing

users

48

Jan

22

07:52

moresrc

-rw-r--r--

1

albing

users

2435522

Jan

22

07:56

moresrc.zip

$










image

While ls is only looking at the “outside” of files,5 there is a command that looks at the “inside,” the data itself, and based on that, tries to tell you what kind of file it found. The command is called file, and it takes as arguments a list of files, so you can give it the name of a single file or you can give it a whole long list of files.



NOTE

Remember what was said about pattern matching in the shell: we can let the shell construct that list of files for us. We can give file the list of all the files in our current directory by using the “*” on the command line so that the shell does the work of expanding it to the names of all the files in our directory (since any filename will match the star pattern).


$ file *

ReadMe.txt: ASCII text

Shift.class: compiled Java class data, version 45.3 Shift.java: ASCII Java program text

anIcon.gif: GIF image data, version 89a, 26 x 26,

dispColrs: PNG image data, 565 x 465, 8-bit/color RGB, non-interlaced jam.jar: Zip archive data, at least v2.0 to extract

moresrc: directory

moresrc.zip: Zip archive data, at least v1.0 to extract

$


image

5. Technically, ls (without arguments) need only read the directory, whereas ls -l looks at the contents of the inode in order to get all the other information (permissions, size, and so on), but it doesn’t look at the data blocks of the file.


The file looks at the first several hundred bytes of the file and does a statis- tical analysis of the types of characters that it finds there, along with other spe- cial information it uses about the formats of certain files.

Three things to note with this output from file. First, notice that

dispColrs was (correctly) identified as a PNG file, even without the .png suffix that it would normally have. That was done deliberately to show you that the type of file is based not just on the name but on the actual contents of the file.

Second, notice that the .jar file is identified as a ZIP archive. They really do use a identical internal format.

Thirdly, file is not foolproof. It’s possible to have perfectly valid, compil- able Java files that file thinks are C++ source, or even just English text. Still,

it’s a great first guess when you need to figure out what’s in a directory.

Now let’s look at a file. This simplest way to display its contents is to use cat.


$ cat Shift.java import java.io.*; import java.net.*;

/**

* The Shift object

*/

public class Shift

{

private int val; public Shift() { }

// ... and so on


} // class Shift


When a file is longer than a few lines you may want to use more or less

to look at the file.6 These programs provide a screen’s worth of data, then pause


image

6. Like any open marketplace, the marketplace of ideas and open source software has its “me- too” products. Someone thought they could do even better than more, so they wrote a new, improved and largely upward compatible command. They named it less, on the minimalist philosophy (with apologies to Dave Barry: “I am not making this up”) that “less is more.” Nowadays, the more is rather passe. The less command has more features and has largely replaced it. In fact, on many Linux distributions, more is a link to less. In the name of full


for your input. You can press the space bar to get the next screen’s worth of output. You can type a slash, then a string, and it will search forward for that string. If you have gone farther forward in the file than you wanted, press “b” to go backwards.

To find out more about the many, many commands available, press ?

(the question mark) while it’s running.

Typical uses for these commands are:

• To view one or more files, for example more *.java, where you can type

:n to skip to the next file.

• To page through long output from a previous pipe of commands, for ex- ample, $ grep Account *.java | more, which will search (see more on grep below) for the string Account in all of the files whose names end in .java and print out each line that is found—and that output will be paginated by more.

If you need only to check the top few lines of a file, use head. You can choose how many lines from the front of the file to see with a simple parameter. The command head -7 will write out the first seven lines, then exit.

If your interest is the last few lines of a file, use tail. You can choose how

many lines from the end of the file to see; the command tail -7 will write out the last seven lines of the file. But tail has another interesting parameter,

-f. Though tail normally prints its lines and then, having reached the end of file, it quits, the -f option tells tail to wait after it prints the last few lines and then try again.7 If some other program is writing to this file, then tail will, on its next read, find more data and print it out. It’s a great way to watch a log file, for example, tail -f /tmp/server.log.

In this mode, tail won’t end when it reaches the end of file, so when you

want it to stop you’ll have to manually interrupt it with a ^C (Control-C— i.e., hold down the Control key and press the C key).



image

disclosure, there is also a paging program called pg, the precursor to more, but we’ll say no more about that.

7. The less command has the same feature. If you press “F” while looking at a file, it goes into an identical mode to the tail -f command. As is often the case in the wacky world of Linux, there is more than one way to do it.