Sunday, October 4, 2009

Linux - Viewing Files with Cat and Less

Linux - Viewing Files with Cat and Less

Cat Command

To displays the contents of a file at the terminal window, use the cat (concatenate) command,

$ cat file1.c
#include
int main() {
    printf("Hello world.\n");
    return 0;
}

With cat, the contents of the file will scroll right off the top of the window if the file is too large.

Less Command

You can use the less command to display a file on the window and then use the Page Up (or b for backwards) and Page Down (or f for forwards) keys to scroll through the file.

$ less file1.c

file1.c is displayed on window

Other Frequently-Used Less Commands

After you have a file opened in less, simply type a command to do something.

Command

Function

h

Display Help

q

Quit less

g

Go to 1st line

G

Go to last line

=

Display file name and current line

#g

Go to line #(ex: 5g)

/

Search forward for pattern(ex: /fred)

?

Search backward for pattern

n

Repeat search in same direction

N

Repeat search in opposite direction

!cmd

Run shell and execute cmd(ex: !ls)

v

Load current file in VI editor

  

 

No comments:

Post a Comment