h7

来源:互联网 发布:更换mac电脑登录帐号 编辑:程序博客网 时间:2024/04/28 04:44

$ wc -l log.log

 

Using file to Identify File Types

$ file suncheck.log boss

suncheck.log:   ascii text

boss:           director

 

$ file *

 

Don’t misinterpret the results of these examples as proof that the file command is useless and that you shouldn’t use it. Quite the opposite is true. UNIX has neither a specific file-naming convention (DOS has its three-letter filename suffixes) nor indication of file ownership by icon (Macintosh does this with creator information added by each program). As a result, it’s vital that you have a tool for helping ascertain file types without actually opening the file.

 

Peeking at the First Few Lines with head

$ head -3 .profile /etc/passwd

==> .profile <==

#

# Initial EIS settings for user root

# This file is set up by the setup-standard script.

 

==> /etc/passwd <==

root:x:0:1:Super-User:/:/sbin/sh

daemon:x:1:1::/:

bin:x:2:2::/usr/bin:

 

$ who | head -5

The special symbol for creating UNIX pipelines is the pipe (|) character. Pipes are read left to right.

The simplest of programs for viewing the contents of a file, head, is easy to use, efficient, and works as part of a pipeline, too.

 

Viewing the Last Few Lines with tail

It shows the last 10 lines of a file.

$ tail -10 log.log

 

$ head -5 vgop.log

$ head -5 vgop.log|tail -3

 

Combining the two commands head and tail can give you considerable power in viewing specific slices of a file on the UNIX system. Try combining them in different ways for different effects.

 

Viewing the Contents of Files with cat

-v

The cat program also has a valuable secret capability, too: Through use of the -v flag, you can use cat to display any file on the system, executable or otherwise, with all characters that normally would not be printed (or would drive your screen bonkers) displayed in a special format I call control -key notation. In control key notation, each character is represented as ^n, where n is a specific printable letter or symbol. A character with the value of 0 (also referred to as a null or null character) is displayed as ^@, a character with the value 1 is ^A, a character with the value 2 is ^B, and so on.

 

-s

Another cat flag that can be useful for certain files is -s, which suppresses multiple blank lines from a file. It isn’t immediately obvious how that could help, but there are some files that can have a screen full (or more) of blank lines. To avoid having to watch them all fly past, you can use cat -s to chop ’em all down to a single blank line.

 

$ cat log.log|tail -5

 

$ cat -v /bin/ls|head -1

This is complex and confusing, indeed! What’s worse, this isn’t the entire first line

of the executable. You can see that, because this block of data ends with Broken

pipe, which indicates that a lot more was being fed to head than it could process,

due to the constraint of having only the first line listed—a line that head defines as

no more than 512 characters long.

 

Viewing Larger Files with more

There are three primary flags in more:

-s    Suppresses multiple blank lines, just like the -s flag to cat

-d    Forces more to display friendlier prompts at the bottom of each page

-c    Causes the program to clear the screen before displaying each screen full of text

 

The program also allows you to start at a specific line in the file by using the curious +n notation, where n is a specific number. Finally, you can start also at the first occurrence of a specific pattern by specifying that pattern to the program in a format similar to +/pattern.

 

Try starting up the program with the tenth line of the file

$ more +10 log.log

 

$ more -10 log.log

$ more +/pattern log.log

 

Commands available within the more program.

Command            Function

[Space]                Press the spacebar to display the next screenful of text.

n[Return]              Display the next n lines (the default is the next line only of text).

h                          Display a list of commands available in the more program.

d                          Scroll down half a page.

q                          Quit the more program.

ns                         Skip forward n lines (default is 1).

nf                         Skip forward n screenfuls (default is 1).

B or Control-b     Skip backward a screenful of text.

=                          Display the current line number.

/pattern                 Search for an occurrence of a pattern.

n                          Search for the next occurrence of the current pattern.

v                          Start the vi editor at the current line.

Control-l              (That’s a lowercase L.) Redraw the screen.

:f                          Display the current filename and line number.

原创粉丝点击