Linux output by page

Less Command in Linux

Less is a command line utility that displays the contents of a file or a command output, one page at a time. It is similar to more , but has more advanced features and allows you to navigate both forward and backward through the file.

When starting less doesn’t read the entire file which results in much faster load times compared to text editors like vim or nano .

The less command is mostly used for opening large files .

How to Use Less #

The general syntax for the less program is as follows:

For example, to view the content of the /usr/share/common-licenses/GPL-3 file you would type:

less /usr/share/common-licenses/GPL-3

You can also redirect the output from a command to less using a pipe. For example, to view the output of the ps command page by page you would type:

When opening a file which content is too large to fit in one page, you will see a single colon ( : ).

To go forward to the next page press either the f key or Space bar . If you want to move down for a specific number of lines, type the number followed by the space or f key.

You can press either the Down arrow or Enter to scroll forward by one line and Up arrow scroll backward by one line.

To go back to the previous page hit the b key. Move up for a specific number of lines, by typing the number followed by the b key.

If you want to search for a pattern, type forward slash ( / ) followed by the pattern you want to search. Once you hit Enter less will search forward for matches. To search backwards use ( ? ) followed by the search pattern.

Источник

bash commands that don’t fit on one page — make output scrollable

Probably been answered somewhere, but its difficult to frame the search phrase. I am running a bash terminal window and some commands are too big to fit on the page (e.g. ps -A ) I vaguely recall a command line parameter / method that shows the command output page by page so I can scroll through the output, but I can’t recall what it is. any pointers?

@Hastur Normally, yes, the terminal used has a buffer you can scroll, but I am using this on a really really basic terminal (seems to be from the 60’s), there is no scroll/buffer. 🙁

If you have less it’s somehow better than more : you can scroll easily up and down, it’s able to see if the file/stream is binary and to prompt before showing it (In this case you avoid to mess the character set of your screen with an uncontrolled binary text) :-).

3 Answers 3

For commands I use often, I generally set up a function in my .bashrc to make them paginate if longer than a screen.

This replaces ps with a function, named ps , which calls the original ps command with whatever arguments given on the command line, then pipes the output (stdout and stderr, using the |& pipe) into less -F , which pauses if there’s more than a screen-full, but exits immediately if it’s less than a screen-full.

Читайте также:  Linux command for list files

VERY handy, doesn’t interfere with anything I’ve worked with so far, and is just cool!

You can even add oft-used options into the command/functions too:

This makes nm always demangle C++ symbols. AND paginates the output. Yay!

I’m running Debian, so I use the apt-cache command quite often, search and show mostly. This function causes those particular options to paginate, search output is sorted, and everything paginates:

If the command is ‘search’, sort the output, then paginate with less -F , but if command is anything else, just paginate, without sorting.

Occasionally I forget I’ve got the functions, and I’ll do something like:

apt-cache search gcc | less 

The function doesn’t interfere, everything works as expected, no harm either way.

Another little tweak, I use the same .bashrc on all my systems, so sometimes a utility might not be installed, so there’s no need for the function. I make them conditional like this:

which apt-cache &>/dev/null && function apt-cache

This just uses the which command to determine if a program is available, if it isn’t, it quietly fails and skips installing the function. Taa Daa!

Источник

Less Command in Linux Explained [With Examples]

When we talk about viewing files from a Linux terminal, the known commands are cat, more, less, head, and tail. Out of which the most commonly used command is less. This is mainly because of its flexibility to seamlessly navigate through file contents.

In this guide, we learn about less command in Linux with examples.

less command: The Basics

The less command is used in Linux to view the contents of a file in a terminal. This content will be shown on one screen at a time. Unlike editors (such as vim, nano, etc.) which are used for editing, the less command is used for presenting the content, scrolling backward and forward, searching patterns, and viewing multiple files.

less is the preferred command for viewing large files when compared with more and cat. This is only because of its enhanced features of navigation tasks. When compared to more command, less has more options and navigation shortcuts.

Syntax and Options

The following is the basic syntax of less command.

  • [Options] — Used to change the behavior of less.
  • [Filename] — The name of the file you want to view the contents. You can also add multiple filenames.

Options

The following table lists some of the useful options with less.

Options Description
-N Print the line number at the beginning of the each line.
-I Ignore the case for all searches.
-M Show range of page numbers and percentage through the file at the bottom of the screen.
-m Print the line number at the beginning of each line.
-E Exit the file without the need to type Q in less.
-S Do not wrap long lines.
-X Default less clear the terminal. This keeps the content in terminal after exiting less.
+F Similar to tail -f. Track additional data at the end of the file in real-time.
-p pattern Tell less to go the start of first occurrence of the pattern.
-R Print the line number at the beginning of each line.
Читайте также:  Linux list opened port

Getting Started with less

To open and view a file with less:

You can replace filename with the name of the file you want to view.

viewing file content using less command

This command opens the file named /etc/ssh/ssh_config in less and allows you to navigate through it.

The basic navigation controls you should know are:

  • Space bar: Scroll down one page.
  • b: Scroll up one page.
  • Up arrow or k: Move up one line.
  • Down arrow or j: Move down one line.
  • g: Go to the beginning of the file.
  • G: Go to the end of the file.

To exit less, you simply press q.

The following table shows an overview of navigation shortcuts of less:

Keyboard Commands Description
Up arrow, y or k Move up (backward) by one line.
Down arrow or j Move down (forward) by one line.
Space Move down by one page.
b or B Move up by one page.
d Move down by half a page.
u Move up by half a page.
Right Arrow Key Scroll horizontally to the right
Left Arrow Key Scroll the page Horizontally to the left
G or > Go to the end of the file
g or Go to the start of the file
ng Move to the nth line (ie nth line will be at the top of the screen). Type this after opening the file. Don’t press Enter.
Ctrl+F Navigate forward one window
Ctrl+B Navigate backward one window
Ctrl+G Display filename, lines showing, bytes, and percentage statistics
Shift + F Used inside less. This track additional data coming to the end of the file.
:n Move to the next file
:p Move to the previous file
h or H Display summary of less commands while na
/pattern Search forward in the file for pattern.
?pattern Search backward in the file for pattern.
n Repeat the last search (in the same direction).
N Repeat the last search (in the opposite direction).
= Detailed prompt including bytes. Similar to -M option
q or Q Move to the nth line of the output

Searching Within Files

In less, you have the ability to search within files. Following are the search shortcuts.

  • /pattern : Search forward in the file for pattern.
  • ?pattern : Search backward in the file for pattern.
  • n : Repeat the last search (in the same direction).
  • N : Repeat the last search (in the opposite direction).

To search forward for the string named Port within the file /etc/ssh/ssh_config, open the file with less and type:

search a pattern within less

Remember the search starts from the current location in the file. It will highlight all matches found on the page. Press n to continue to the next match of the search term in the forward direction. If you want to search in the reverse direction (up the file), press N .

If it doesn’t find any more matches, it will display a message like Pattern not found (press RETURN) at the bottom of the screen.

By default search function within less is case-sensitive. In our above example, less won’t match Ssh or SSH. You can change the behavior using -i or I options. When the -i option is used, case is ignored in searches unless the search pattern contains an uppercase letter (In our example /Ssh won’t match). Instead, you can use -I to search completely case insensitive.

If you want to search backward use ? instead of / .

Just like when searching forward, you can n ( next match in reverse) and N ( for forward search).

Another method of pattern matching is using -p option. This shows the first occurrence of a specific pattern.

less -p Ssh -I /etc/ssh/ssh_config

case insensitive search using capital i option

Here we have combined multiple options — used -p to search pattern Ssh case-insensitively.

Line Numbers and less

In less, use the -N option to display line numbers. The line number will be displayed at the beginning (left side) of each line.

show line numbers in each line of the file using less

This command will open the SSH configuration file with less, and display line numbers next to each line.

Go to a specific line number

In less, you can use two methods to go to a specific line number.

To navigate from within the file using ng. This shows the nth line of the file to position at the top of the page.

Instead of navigating, you can directly specify the line number when opening the file.

You can add -N option to confirm this.

less +10 -N /etc/ssh/ssh_config

go to 10th line of the file as well as showing line number in less

Working with Multiple Files

You can open multiple files in less by passing each filename as arguments.

less /etc/ssh/ssh_config /etc/rsyslog.conf

viewing multiple files in less

Now you may wonder how to navigate between these two files. Use :n to move to the next file and :p to move to the previous file. In case you forgot where you are, you can use :f display the status of the current file ( especially showing the filename currently viewing).

Display content on the terminal after exited

By default after exiting from less the content of the file won’t be visible on the terminal. You can change this behavior by using -X option — this enables less to keep the content on the terminal even after you have exited.

less -X option

Here you can see the content of file /var/log/dmesg till remain on the terminal. If it is a big file you can scroll up using the terminal bar on the right side to navigate the portion of the file you have viewed before.

Marking Positions

In less you to mark positions in a file and can go back to that position using the reference.

You may insert a mark by typing ‘m’ followed by a reference character. If you want to go back to that marked position, use a single apostrophe (‘) followed by the character typed previously.

For example, press m when you need to set mark a place, type any letter as an identifier, here I pressed the letter ‘f’.

set a mark

To go to the mark press ‘f from the prompt:

go to the reference mark

Less +F

The +F option in less enables tracking the new lines being added to the end of the file. This is something similar to tail -f command.

When you run less +F filename, less command wait for additional data, and as soon as it comes shown in real-time. Generally, this comes useful for monitoring log files for errors. If already inside less you can use SHIFT + F to track the file. To stop tracking press Ctrl + C and you can press +F again to tell less to wait for data. When finished tracking press Q to exit from less.

less +F tracking additional data at end of file in real time.

Once the tail -f is closed it cant scroll back to see data on the terminal. Whereas for less +F you can navigate (after pressing CTRL + C).

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector