Friday, 2 August 2019

Linux - more & less

Linux-more-less-command-practical-examples

More and less commands are used to show content of a file in page by page and percentage views.
These commands are advantageous over cat command in certain situations.
For example,
There is a file with multiple pages in it and you want to see the content page by page.

more command:

Syntax:
more file-name

Example:
Display content of a file with more command:
[rreddy@abclearn abclearn_dir1]$ more abclearn_lab1.txt
this line should replace the existing one
This is 2nd line of the file
3rd line

less command:

less command will show the file in a separate window.
Syntax:
less file-name

Example:
Display content of a file with less command:
[root@abclearn rreddy]# less .bash_history

We can use “space bar”, “ctrl+f”, “ctrl+b” options to move along file.
We can also press “h” to get the help command for moving & searching along the files.

Understanding the difference between Cat-more-less commands:

If we are looking at a file with no more than a page, then there won’t be much different in the output from cat & more & less commands.
For example, abclearn_lab1 is a text file with 3 lines. So we don’t see much difference in either of the command.
[rreddy@abclearn abclearn_dir1]$ more abclearn_lab1.txt
this line should replace the existing one
This is 2nd line of the file
3rd line

[rreddy@abclearn abclearn_dir1]$ cat abclearn_lab1.txt
this line should replace the existing one
This is 2nd line of the file
3rd line

If we start looking at any files which is having more than one page, then more & less seems much handy than cat.

For example, we will take “messages” file which is there in /var/log directory. We should have root privileges to view content of “messages” file.
[root@abclearn log]# cat messages

Observations:
  • It will take you to the last page of the file. To look back previous content, we have to scroll back.
  • Also scrolling of pages will depend on the buffer space allocated in putty settings.
[root@abclearn log]# more messages

Observations:
  • It will show page information in percentage manner at the bottom of the page.
  • To view the remaining information, press “space bar” and will take you.
  • We can’t go back to previous page. We can only go forward pages.
  • To come out of it, press “q” as in quits. We can use “ctrl+c” as well.
[root@abclearn log]# less messages

Observations:
  • Less command shows files pages one by one.
  • To see remaining pages, we can press “space bar” etc.
  • Use ctrl+f to go to next page
  • Ctrl+b to go to previous page.
  • press “q” or “ctrl+c” to come out of the file

0 comments:

Post a Comment