New FAMILUG

The PyMiers

Friday 20 June 2014

[CLI] less - view file trên terminal


root@hvn-do-workspace:~# whatis less
less (1)             - opposite of more
root@hvn-do-workspace:~# whatis more
more (1)             - file perusal filter for crt viewing
Các UNIX developer khá là vui tính trong chuyện trả lời "whatis". Một câu trả lời cũng vui tính không kém khi hỏi :
# whatis git
git (1)              - the stupid content tracker
Khái niệm
Cơ bản thì less là 1 chương trình để view file text trên terminal. ``less`` chỉ để view chứ không edit file, các UNIX-like OSes đều cài sẵn ``vi`` và ``less``. So với ``vi``, khi xem các file lớn, ``less`` không cần phải đọc toàn bộ input file trước khi start (``vi`` sẽ đọc toàn bộ và nhét vào RAM) vì vậy less sẽ start nhanh hơn.

``less`` là một phiên bản cải tiến của ``more``, đây là một kiểu chơi chữ trong tiếng Anh:
less is more

Có 2 cách dùng ``less`` cơ bản:

1. less filename
đọc input file tên là filename 
Ví dụ:
less /etc/passwd 
 2. some commands | less
đọc qua pipeline (|) output của câu lệnh trước nó
Ví dụ:
ls -la | less (dùng khi output quá dài)
Các phím tắt
j : xuống
k : lên
space: xuống 1 màn hình
q: quit
/ tu_khoa: search down
? tu_khoa: search up
-N : hiển thị số dòng (bấm -N, enter)

Đọc thêm

Từ Description section trong ``man 1 less``

Less  is  a  program  similar  to  more (1), but which allows backward movement in the file as well as forward movement.  Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi (1).  Less uses termcap (or terminfo on some systems), so it can run on  a  variety  of terminals.  There is even limited support for hardcopy terminals.  (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.)  Commands are based on both more and vi.  Commands may be preceded by a decimal number, called N in the descriptions below.  The number is used by some commands, as indicated.

Hết!

1 comment:

  1. di chuyển đến dòng thứ N:
    Ng

    VD đến dòng 69:
    69g

    ReplyDelete