Internet Windows Android

Screen - multiple screens in one SSH session. Screen - multiple screens in one SSH session Screen does not scroll terminal emulator in ubuntu


Free translation: Alexey Dmitriev
Date of translation November 19, 2007

We are talking here about the GNU / Linux text console - not what appears in the terminal window, or the X-window manager, or the desktop. There is so much confusion about this that I have to say trivially: - "If you, while in graphical mode, press" Alt-Ctrl-F1 "(or" F2 ", etc.), then you will be taken to the text console" ...

0. What are we talking about?

The computer boots up. You are in the text console, watching the text flowing down quickly. You want to view the already flashed piece of this text. You press "Shift-PgUp" or "Shift-PgDn", and if you have already changed the default keyboard shortcuts (no, no, not the keyboard layout in X), then some other keys. With these keys, you make the text move up and down the screen. You are currently using a scrollback buffer (let's call it scrollback buffer for brevity).

When you start using this buffer with might and main, two things very soon become clear:
Only part of the text accompanying the download can be returned.
The file "/ var / log / messages" captures much less than what you wanted to see.

You start scouring the Internet, and soon preliminary results emerge: the Linux scrolling backwards publications are very ancient and can be traced back to the early 90s. However, everything that came before the 2.6.x kernel should be forgotten.

Indeed, you should be selective in what you read: most of the advice pertaining to versions prior to 2.6.x is likely to confuse you and lead you astray. Trust me - I've killed a lot of time on this. The scrolling back behavior is defined in the file "vc.c", otherwise a small file in the kernel sources, with "vc" standing for "virtual console".

Note that when switching consoles, the scroll buffer is lost - thus the full size of this buffer is available on the current console. (There is a particularly large amount of information garbage here from the days before the 2.6.x kernel).

By default, the buffer size is 32K. This is enough for 4 presses of the scroll back keys - each press scrolls half the screen, 25 lines per screen. There are about 50 lines. And you won't get any more, even if you have 30 or 50 lines per screen. I must say that 50 lines is a small fraction of what flies across the screen during loading. In connection with the above, two questions arise:

How do I increase the scroll buffer?

Why don't the logs capture some of the messages that you saw while loading?

1. How to increase the scrollback buffer?

The first solution is to increase the default (default) buffer size in the kernel sources and recompile it. Let me assume that you are as reluctant to do this as I am, and look for a more flexible remedy.

And there is such a tool, and it is called "framebuffer console", for short "fbcon". This device has a documentation file "fbcon.txt"; if you installed the kernel documentation, you have it. Look for it somewhere in the "/ usr / share" branch (I can’t give you the exact path due to the difference in distributions). You can always download it as a single file.

At this point, I apologize: we have to digress a little and talk a little about the framebuffer.

A video buffer is a buffer between the display and the video adapter. Its beauty is that it can be manipulated: it allows tricks that would not work if the adapter was connected directly to the display.

One such trick has to do with the scroll buffer; it turns out you can "ask" the videobuffer to allocate more memory to the scrollbuffer. This is achieved through kernel boot parameters. First you require a framebuffer; Then you ask for a larger scroll buffer.

The following example is for GRUB, but can be easily adapted to LILO. In the GRUB configuration file - "menu.lst" - find the line corresponding to the kernel, and then:
Remove the "vga = xxx" option if present.
Add the option "video = vesabf" or whatever matches your hardware.
Add the option "fbcon = scrollback: 128".
After this procedure, the kernel parameter line should look something like this:
"kernel / vmlinuz root = / dev / sdb5 video = radeonfb fbcon = scrollback: 128".

Why remove the "vga = xxx" option? Due to possible conflicts with the video option. On my ATI adapter, I cannot change the scroll buffer if "vga = xxx" is listed. Perhaps this is not the case in your case. If the above options work fine; but what if you want to increase the number of lines, or set a smaller font on the screen? You always did it with the "vga = xxx" option - and it disappeared. Don't worry - the same can be achieved by changing the fbcon parameters as described in the "fbcon.txt" file (but not covered in this article). Ubuntu and Debian have a more convenient way: "dpkg-reconfigure console-setup". It is worth using this command also because it also overrides the "initrd" to your new settings.

2. Expanding the limits

The "fbcon = scrollback: 128" option gives you the ability to move around the scroll buffer 12-13 screens. That's about 150 lines - pretty good, but maybe not enough to see all the boot messages yet. Can I put more than 128, say 256? The limit is 128 in "vc.c". If you want more, edit and compile. For myself, I decided that the game is not worth the candle.

When I completely decided that I had reached the highest limit - I quote from "fbcon.txt":
fbcon = scrollback: [k]
...
The "k" suffix is ​​optional, and will multiply the "value" by 1024.
(The "k" suffix is ​​optional, and will multiply the "value" by 1024 times).
I rushed to check .. and whatever the author thought about this, I was convinced that 128 and 128k give exactly the same result. The default limit is 128KB of memory, that's the whole story.

Finally, note that using the extended scroll buffer uses both the graphics memory and the buffer in RAM. If you are content with the default 32KB, you are only using the video card memory. In practice, however, I did not notice the slightest slowdown.

3. What is missing from the logs?

On Ubuntu and Debian and other distributions, all system messages are stored in the file "/ var / log / messages". This applies even if the logging service (daemon) is not the old "syslog" but "syslog-ng" ("ng": "new generation" / new generation /).

In both cases, you can view the messages by typing "dmesg" at the command line. This does not help: you clearly saw during loading the messages that are not in the output of the "dmesg" command. How so?

This is a property, not a bug! The messages logged in the file come from various OS subsystems. Subsystems that output messages to the log file are referred to as "facilities" in syslog terminology, there are only 8 of them. During the loading process, programs and scripts run, which may not belong to any of the 8 "tools". We see their messages scrolling across the screen, but nothing will be recorded in the log file!

For example, you will not see messages from "loadkeys" (openSUSE) or "consolechars" (Ubuntu and Debian) when the keyboard layout is loaded at boot time. Another example: when using a console text editor, you can scroll backward in the display (including colors) using the scroll buffer. But what the editor produces will never fall into any system protocol. The reason, of course, is that neither "loadkeys" nor "consolechars" nor the editor belong to any of the 8 "tools".

Can the situation be changed? Of course - "just" redo and recompile the applications you want to log. Add or rewrite boot scripts. Or force the system to capture messages from outside the 8 system "tools".

I bet you won't do this. And I even know your likely answer:
- "Nothing disappears from my logs." Yes of course...

If you liked the article, share it with your friends:

Screen is a very handy program if you often work in the console by connecting to the server via SSH. In this case screen allows you to keep several different screens in one console, conveniently switching between them.

Screen allows you to disconnect from SSH while saving the jobs running on the remote server.

To install on Ubuntu, you need to run the command

$ sudo apt-get install screen

After that, you need to run screen:

$ screen

After starting, you will see either a command line prompt or a "welcome screen" by pressing SPACE or ENTER in which, you will also be taken to the shell. In this case, all commands will be launched already "inside" the screen.

In order to exit the screen (exit, not detach), it is enough to exit all open shells in it, or press the key combination Ctl-a \, and answer "y" to the question "do you really want to exit" ...

You can disconnect from the current screen without closing or ending the session by pressing Ctrl-a d.

Screen connection

If the machine already has a running screen session and you want to connect to it, then

  1. if there is only one session, just the command is enough: $ screen –x
  2. if there are several sessions, then: you can view the list of running screens using the screen -ls command: $ screen -ls There are screens on: 2762.pts-0.debian (Detached) 2743.pts-0.debian (Detached) 2 Sockets in / var / run / screen / S-diesel.

    Select the screen we need, and attach to it:

    Screen -x 2762.pts-0.debian

    Session names are not very informative by default - just process numbers, if you want to start and use several screen sessions for different tasks, you can come up with more sane names. We come up with a name, then start a new screen like this:

    $ screen -S "job1"

    where job1 is the name of our named session. Now in -ls we will see a lot more clear:

    $ screen -ls There are screens on: 2762.pts-0.debian (Detached) 2795.job1 (Detached) 2743.pts-0.debian (Detached) 3 Sockets in / var / run / screen / S-diesel.

    and we can connect to the job1 session by simply specifying its name:

    $ screen -x job1

Switching between screens

As you already understood, sessions are saved even while you are disconnected from the server, and everything that is running in them continues to run. However, the screen's possibilities are not limited to this. Sometimes, when working via ssh, I really want several terminals to be open at once, while connecting several times is a bummer - especially if you need to connect through several servers. Screen can help with this too. Just press Ctrl - a c, and another shell will open. You can move between open shells either through Ctrl - a<Цифра>, where<Цифра>- the number of the open shell, starting from zero, or Ctrl - a n / Ctrl - a p - forward backward. You can get a list of open "windows" - consoles (and switch to the one you need) by pressing Ctrl - a "; to give a normal name (not just boring "bash") for a window thread, go to it and press Ctrl - a A (case matters), then enter the name at the prompt that appears.

Monitoring command execution

If several windows are open, it can be useful to be able to monitor the activity in one of them. For example, if you run a "long running" command and you wonder when it will finish. Go to the window with this command, press Ctrl - a m, we see an inscription like this: "Window 0 (bash) is now being monitored for all activity". When something in the window changes, screen will beep, and an "@" symbol appears after the number in the window list.

Scrolling the screen in screen

To view the output of the command, which has gone above the borders of the window, you need to press Ctrl-a [

This will switch screen to Copy mode. To exit this mode, just press Esc.

Screen configuration

Screen is configured with the ~ / .screenrc file in your home directory. For example, you can write there:

Caption always "% (= 45)% (+ bw) Screen:% n |% h% =% t% c" hardstatus alwayslastline "% -Lw% (= BW)% 50>% n% f *% t% ( -)% + Lw%<"

This will give you an all-time visible list of open windows, and other useful information at the bottom of the screen.

Also, when Screen starts, by default, commands from the / etc / screenrc file and the .screenrc file from the user's home directory are executed. Many commands that can be used in configuration files were discussed in the tables above in the "Text command" field. Below is an example of a Screen configuration from a .screenrc file:

# Disable the output of license information at startup Screen startup_message off # Open a shell for working chdir screen -t Work # Open a shell for managing configuration chdir / etc screen -t Configuration # Open a shell for viewing logs chdir / var / log screen -t Logs # Select first window after start select 0

This fairly simple configuration file opens three windows named Work, Configuration, Logs, respectively, in the user's personal directory, in the / etc directory and / var / log directory. After launch, the screen will display the first window named Work. Figure 1 shows an example of how Screen works with this configuration file.

Screen Completion

If for some reason the screen session becomes unresponsive, you can terminate it. To do this, you need to connect to the desired session, then press Ctrl - a and enter the command ": quit".

Screen will terminate this session by forcibly closing all windows.

Working with Screen

One of the main features of Screen is the creation and management of multiple console windows, or text applications, in one single terminal window. Only one of the windows can be on the terminal screen at a time (there can be several of them only if they divide the terminal screen into parts). Screen allows you to quickly switch between windows, save and later return to a text session and open access to it to other users. Plus, Screen allows you to give windows titles, track activity in each window, and share window contents using copy and paste. And in addition, Screen allows you to remap keys, manage terminal emulation settings, and many more small text-window tasks that may not be necessary permanently, but can help perform administrative work at specific points in time.

The figure shows a Screen with three named windows (Work, Configuration, Logs) and opened in it with a text editor Vim, with an example of the Screen configuration file.

First start

Screen can be started with the screen command line, but first of all, check if Screen is installed on your system with the following command:

$ which screen / usr / bin / screen

If the command returned the path as in the example above, you can start using it right away, otherwise Screen can be installed by the standard package support tools of your operating system, or downloaded from the official site http://www.gnu.org/software/screen/.

Now we can start Screen by simply typing:

$ screen

After launching, information about the version and license may appear on the screen (when considering the configuration, we will consider how you can cancel the display of this message) and in this case you need to press the "Space" or "Enter" key, after which we get into the seemingly usual , a text wrapper. It should be noted that in the command line you can immediately specify a command to launch a specific application, for example, to start top, you need to type:

$ screen top

Ok, now Screen is running, but, at first glance, there are no external changes - we can work in a running shell, or use top. The trick is that while Screen is running, it passes all keystrokes directly to the application, but (by default) leaves control commands for yourself, starting with the key combination Ctrl + a.

This control key combination can be changed through the configuration files (which we will look at below), but in all examples, only the default configuration will be considered. Thus, all Screen keyboard commands consist of two keyboard shortcuts - the control Ctrl + a and the key itself, or a keyboard shortcut representing the command. Almost every command also has its own textual counterpart, which can be executed either through a configuration file or by running the following command:

In this case, we have to execute two keyboard shortcuts, first by pressing Ctrl + a and then the colon key. At the same time, Screen will display a colon prompt at the bottom of the screen. The same command that we just ran with a keyboard shortcut can be executed through the config file using the colon text command, which may, for example, be needed to display a command prompt at startup, or to reassign the keyboard shortcut that invokes this prompt. Below, as in the example above, for each key combination, I will write the corresponding keywords that can be used in the configuration files, or entered with Ctrl + a and :.

A natural question may arise here - what if the application needs to directly pass the Ctrl + a key combination, which is already used in Screen as a service one? For example, the same combination is used by the readline command line editing library to jump to the beginning of a line. For this case, Screen provides the following command:

Window management

In the previous section, we learned how to launch Screen and learned the basic template for all keyboard commands. It's time to figure out the commands for managing windows. When Screen starts, it creates one window (unless the configuration specifies creating several windows at once) and assigns it number 0. A new window with a command shell can be created using the command below:

KEY COMBINATION TEXT COMMAND DESCRIPTION
Ctrl + a c screen Create a new window

This will cause Screen to create a new window and assign it the next consecutive number, in our case the new window will be window 1.

We now have two text windows, of which the newly created window with number 1 is the current one, and the window with number 0 is in the background. But, it should be noted that even if the window is in the background, it can perceive the output of the application running in it as if it were current. For example, we could start a process in window 0 that outputs additional information at regular intervals (for example, this could be the tail -f command) and then switch to window 1 to work in a text editor. After a while, if we want to see what is happening in window 0, we can switch to this window and see all the output of the process running there. Then you can switch back to the editing window.

In the previous paragraph, I described an example with switching between windows, but I have not yet said what commands are used for this. Let's take a closer look at the most commonly used commands for managing windows:

KEY COMBINATION TEXT COMMAND DESCRIPTION
Ctrl + a w windows Displays a list of numbers and titles of all windows in the bottom line of the screen
Ctrl + a « windowlist Displays an interactive list of numbers and titles of all windows from which you can select a window to switch
Ctrl + a Window number select Window selection by number. For example, the command Ctrl + a and 0 makes the current window with number 0
Ctlr + a n next Switch to the next window in order, or to the first window if we were in the last window
Ctrl + a p prev Switch to the previous window in order, or to the last window if we were in the first window
Ctrl + a select Display a prompt to enter the window number to switch
Ctrl + a Ctrl + a other Switch to the last used window
Ctrl + a N number Show the number and title of the current window
Ctrl + a A title Set the title of the current window
Ctrl + a k kill Forced closing of a window, for example, if the application cannot be closed in the usual way

When one of the windows is closed, the numbers of other windows do not change and "holes" appear in the numbering, which can later be filled with new windows. Although Screen has a command for changing the window number, it is not considered here and remains for independent study. If all windows are closed, Screen exits and displays a message:

Work with text

Above, we learned how to launch Screen and manage windows - create them, switch from window to window, assign names and close them. Now let's look at working with the contents of windows - copying and pasting the text content of one window into another, monitoring windows, saving a text copy of the screen, and other similar tasks:

KEY COMBINATION TEXT COMMAND DESCRIPTION
Ctrl + a M monitor Enable / disable activity monitoring for the current window. When monitoring is enabled and text is output in a window in the background mode, information about activity is displayed in the service line.
silence Enable / disable inactivity monitoring for the current window. When monitoring is enabled and no text is output in a window in the background mode for a certain period of time, information about inactivity is displayed in the service line.
Ctrl + a h hardcopy Write the text image of the current window to hardcopy.window_number in the default Screen directory.
Ctrl + a H log Enable / disable tracing of the text of the current window to the file screenlog.window_number in the Sccreen directory by default.
Ctrl + a [ copy Enter copy mode. Additional commands for this mode are described below.
Ctrl + a ] paste Paste the text from the default register into the window (where it was previously copied using the command above).

In copy mode, Screen launches a text editor similar to the Vi editor. The most commonly used editor commands are:

  • Cursor keys move the cursor over the screen buffer;
  • The space bar marks the beginning and end of the region to copy. On pressing Space a second time, the marked text is copied to the default case, and Screen exits from copy mode;

Multiple windows on the screen

By default, Screen leaves only one logical window visible on the screen, but if desired, there is an additional option to split the terminal screen into horizontal regions. Thus, it is possible to display several windows at once on one terminal screen. Let's take a closer look at the commands for working with screen regions:

Figure 2 shows a Screen with three named windows (Work, Configuration, Logs) and a screen divided into two regions. A Work window is open in the upper region with a Vim text editor and an example Screen configuration file. In the lower region, the Configuration window with part of the general configuration file / etc / screenrc.

Console sessions

Finally, we'll take a look at one of the most popular features of Screen. Let's imagine the following scenario: a sysadmin at home, logs into a remote server, opens several windows, starts the necessary tasks, then closes the connection and drives to work. At work, he again logs into the same server and restores windows open at home with the results of completed tasks. Sounds a little weird? In no case! Screen console sessions are designed to accomplish this task. They allow you to disconnect the current Screen session with all windows and running applications from the terminal and then rejoin it at any time. At the same time, all applications running under Screen will continue to work and use their windows to display information. Plus, there can be several console sessions, but in this article we will not dwell on this in detail.

Consider the commands to disconnect the console session from the terminal:

KEY COMBINATION TEXT COMMAND DESCRIPTION
Ctrl + a d detach Detach the console session and leave it running in the background. At the same time, Screen writes:
Ctrl + a D D pow_detach Detach the console session, leave it running in the background, and send the HANGUP signal to the parent Screen process. When you launch Screen from a remote terminal, it means disconnecting Screen and then closing the connection. At the same time, Screen writes:
Ctrl + a z suspend Detach the console session and leave it running as a background process in the parent shell. In this case, to restore the session, in addition to the commands described below, the standard commands for managing the background processes of the command shell can be used
Ctrl + a Ctrl + quit Close all session windows and exit Screen. It should be noted that many distributions override the keyboard shortcut for this command, but the text command can be used anyway

To join back to the disconnected session, or see the list of available sessions, you need to run Screen with one of the following keys:

What's next?

Screen has a huge number of different possibilities and it is practically impossible to describe them all in one article, so some of the functionality was left behind the scenes. Here are some Screen features / settings that were not discussed above and remain for independent study:

  • Terminal emulation configuration commands;
  • Multi-user console sessions;
  • Assigning or reassigning keyboard shortcuts to commands;
  • Text command helper options. For example, the -t option of the screen command, which is used in the sample configuration file;
  • Commands that are rarely used, such as the commands that you can use when configuring the Screen startup, are echo and sleep;
  • Environment variables that can be useful when customizing Screen;
  • Additional Screen launch keys;

Screen alternatives

Screen is a program with a long history, it should be noted that the first version appeared in 1987. At the moment, practically no new features are being added to Screen (one of the reasons may be the presence of a large number of them at the moment) and, in general, only bug fixes are performed. If someone does not like the relative "old age" of Screen, or does not need such a large number of possibilities, then you can turn to one of the following programs:

  • dtach () - the main emphasis in the development of this program is made on compactness. By analogy with Screen, it supports the ability to work with console sessions, but does not have the other Screen capabilities.
  • SplitVT (http://www.devolution.com/~slouken/projects/splitvt/) - allows you to split the terminal screen into two halves and work with two shells at once.
  • Tmux () - Tmux uses a client-server model to work with console sessions.
  • Twin (http://linuz.sns.it/~max/twin/) - an attempt to create a text window interface similar to its graphical counterparts.

The topic of useful tricks for working in the Linux terminal is endless. It would seem that everything is arranged very simply: the shell prompt, and the commands entered from the keyboard. However, in this simplicity lies an abyss of non-obvious, but useful possibilities. That is why we regularly publish materials on the specifics of working in the Linux command line. In particular, today it will be a translation of an article, the author of which is keen on saving time by increasing labor productivity.

If you are interested in working on the Linux command line, here are some of our resources on this topic:

  • [ +114 ] Most Useful Linux Command Line Tricks
  • [ +70 ] 10 Linux Terminal Tricks Few Know About
  • [+65 ] An amazingly useful tool: lsof
  • [ +32 ] Linux: redirect
  • [ +29 ] PDF version of articles on Bash scripts

Linux command line techniques and time savings

Have you ever seen a colleague use some simple Linux commands to solve problems that take you a few lines to solve? It must be when this happened, you could only say to yourself: “Well, never mind! I didn’t know it could be done so easily. ”

In this post, I'll show you some professional Linux command line tricks that will save you a lot of time and, in some cases, get rid of your bad mood. Not only that, your friends or colleagues will say: “Well, you must!”, Looking at you. These techniques will also help you increase your personal productivity, as you will need to type fewer commands and even do fewer mouse clicks.

This is not to say that these Linux tips are for beginners only. It is possible that even experienced users will find something useful here that they didn’t know about despite having been using Linux for many years. Linux is learned through experience. Both your own experience and the experience of other people will do.

Before we get started, I'd like to point out that some of the methods shown here are shell-specific.

0. Command completion using the Tab key

I'll start with something quite obvious, but very important: command completion with the Tab key.

When you start typing something in the Linux terminal, you can press the Tab key, after which a prompt will be displayed containing a set of continuation options starting with the characters you just entered.

For example, if you are about to copy a file named my_best_file_1.txt, you can simply type cp m and press Tab to see the options for the command to continue.


Using the Tab key for auto-completion

The Tab key can also be used for command completion.

1. Go to the last working directory

Imagine that you were working in a directory that is not easy to reach, and then changed to another directory located in a completely different place. Then you realized that you need to go back to the previous directory. In this case, it is enough to use the following command:

Cd -
It will take you to the last working directory. Now, in such situations, you will no longer need to enter long paths, or use copy and paste.


Easy switching between directories

2. Return to home directory

This technique is also quite obvious. You can use the command below to return to your home directory from anywhere:

Cd ~
However, you can return to your home directory even faster with the cd command.

In most modern Linux distributions, the shell is preconfigured to accept the cd command in this way. This will save you a couple of keystrokes on your keyboard.


The fastest way to go to your home directory

3. Displaying the contents of the directory

You may be wondering what is the subtlety of using the command to display the contents of a directory. Everyone knows that the ls -l command serves this purpose.

But that's the whole point. Almost everyone uses the ls -l command to list the contents of directories, while the same can be done with the following command:

Ll
This command depends on your Linux distribution and shell configuration, but it is very likely that you will be able to use it on most distributions.


Using ll command instead of ls -l command

4. Calling several commands on one line

Imagine that you need to execute several commands in sequence. Probably, you enter one command, then wait for it to complete, then enter the next one?

A command separator is useful in this situation; (semicolon). With this approach, you can enter multiple commands on one line. At the same time, in contrast to the usual input of commands, to execute the next command it is not necessary to wait for the completion of the previous one.

Command_1; command_2; command_3

5. Execution of several commands in one line and the condition for the successful completion of the previous command

We just looked at a way to call multiple commands on one line. This saves time. But what if you need, say, when calling two commands, so that the next command is executed only if the previous one completes without errors?

Imagine you want to build the code, and then, if the build is successful, call make?

In this situation, you can use the && separator. This separator ensures that the next command will only be executed if the previous one succeeds.

Command_1 && command_2
Here's a good example of using &&:

Sudo apt update && sudo apt upgrade

6. Simple search and use of previously entered commands

Imagine that you, a couple of minutes or a couple of hours ago, entered a long command, and you need that command again. Moreover, the problem is that you cannot remember exactly this command.

In such a situation, a reverse search will save you. This technique allows you to search the command history by keyword. Here it is enough to use the key combination Ctrl + R to start a reverse search and enter something related to the command. The system will review the command history and show the commands that match the entered request.

Ctrl + R search_term
By default, only one result will be shown. In order to see more results matching your query, you will need to use the Ctrl + R keyboard shortcut over and over again. To exit reverse search mode, press Ctrl + C.


Reverse search command history

Note that in some Bash shells, you can use the Page Up and Page Down keys to iterate through the search results.

7. Unlocking the terminal after accidentally pressing Ctrl + S

You may be accustomed to using the Ctrl + S keyboard shortcut to save files. But if you press these keys in the Linux terminal, you will block it.

If earlier you, in order to correct the situation, had to close and restart the terminal - now you can breathe easy, you will not have to do this again. In order to bring the terminal into working condition, just use the key combination Ctrl + Q.

8. Move to the beginning or end of a line

Imagine that you enter a long command and somewhere in the middle you realize that you have to change something at the beginning. You are probably using the arrow keys to move to the beginning of the line first and then back to the end.

Of course, in a similar situation, you can use the Home and End keys, but, alternatively, using the Ctrl + A key combination, you can go to the beginning of the line, and with the Ctrl + E key combination, to the end.


Move to the beginning or end of a line

I find this method more convenient than using the Home and End keys, especially when I work on a laptop.

9. Reading log files in real time

When you need to analyze log files that some running application writes data to during analysis, you can use the tail command with the -f option.

Tail -f path_to_Log
Alternatively, you can use the grep command in its usual form to display only the lines that interest you:

Tail -f path_to_log | grep search_term
You can also use option F here. This will cause the tail utility to continue running even if the log file being viewed is deleted. With this approach, if this file is created again, tail will continue to display data from it to the screen.

10. Reading compressed log files without unpacking them

Server logs are usually gzipped to save disk space. This can lead to some inconvenience when analyzing the logs by developers or system administrators. You may need to use the scp command to copy the file to your local directory, and then extract the file from the archive in order to view it, since you may not have write permission to where the file you are interested in is stored.

Here a group of commands comes to our aid, the name of which begins with the letter "z". These commands are an alternative to the regular commands used for working with log files. Among them are less, cat, grep, and others.

So, using the commands zless, zcat, zgrep and the like, you don't have to explicitly unpack compressed files. Details about these commands can be found.

By the way, when I told my colleague about these "secret" commands, I got a cup of coffee.

11. Using the less command to read files

If you need to view a file, especially a large one, you can try cat, but it's much better to look elsewhere. The point is that cat will display the entire file on the screen, which is not so convenient.

You can use editors like Vi or Vim in the terminal to view the files, but if you just need to read a file, less is a great command.

Less path_to_file
During a session of work with less, you can search for the necessary fragments of text by keywords, move through pages, display data with line numbers, and so on.

12. Reuse the last item from the previous command with! $

In many situations, it is very useful to be able to use the argument of the previous command.

Let's say you need to create a directory and then navigate to it. Here you can use the! $ Option by entering the jump command after the command to create a directory.


Using! $ To Use the Last Command Argument

Better yet, do the same with the Alt + keyboard shortcut. (point). The period can be used multiple times to iterate over the options of the last command.

13. Using the previous command in the current command with !!

By using!! you can call the entire previous command. This technique is especially useful when you need to execute a command and it turns out that you need superuser privileges to execute it. For example, the figure below shows a situation in which the sudo !! saves a lot of time.


Take advantage !!! to substitute the last command as an argument

14. Using aliases to correct typing errors

You may already be familiar with the alias command. It can be used to fix errors in entered commands.

For example, it may happen that you often type gerp instead of grep. If you can't cope with a bad habit, write the alias to your bashrc file as follows:

Alias ​​gerp = grep
Now you don't have to retype this command if you enter its name incorrectly.

15. Copy and paste in Linux terminal

This advice is somewhat controversial, as it depends on the Linux distribution and the terminal.

But usually the copy and paste commands can be called like this:

  • Select the text to be copied and right-click to paste it (works in Putty and other Windows SSH clients).
  • Select the text to be copied, then click with the central mouse button (scroll wheel) in order to paste it.
  • Use the keyboard shortcuts Ctrl + Shift + C to copy, and Ctrl + Shift + V to paste.

16. End a command or process

Perhaps what I want to advise here is quite obvious, however, I will tell you about it anyway. If a command is running in the foreground and you want to exit it, you can press Ctrl + C to stop it.

17. Using the yes command to automate interactive commands or scripts

If you have to work with some commands or scripts that involve user interaction, and you know that you will enter Y in response to each program question, you can automate this process with the yes command. This is done like this:

Yes | command_or_script

18. Clearing the contents of a file without deleting the file itself

If you want to clear the contents of a text file without deleting the file itself, you can use the following command:

> filename

19. Finding out if the directory contains files containing certain text

The Linux command line supports many ways to find information. However, if you just need to find out if there are files in a directory that contain certain text, you can use this command:

Grep -Pri Search_Term path_to_directory
However, my advice to anyone looking for Linux search is to get the hang of the find command.

20. Calling help for any command

I want to conclude this material with one very obvious, but nevertheless very important "trick", which is to invoke help for a command or command line tool.

Almost all command line tools contain help information that explains how they can be used. Usually, reading the help will help you understand the basics of the command. You can call the help like this:

Command_tool --help

I have deliberately left out commands like fuck in this material, as they are not standard tools that can be found literally everywhere. What I've just talked about here can be beneficial when working with almost all Linux distributions and command line shells without having to install new programs.

161 Jane doh

I understand that Up / Down will give you the command history. But how do you look at past results by scrolling up and down?

I've used Shift + Page Up / Page Down, Alt + Shift + Up / Down and Page Up / Page Down, but none of these seem to work.

This is the Redhat Linux box.

linux terminal rhel

14 replies

SHIFT + Page Up and SHIFT + Page Down. If that doesn't work, try this and then it should:

Go to your terminal program and make sure Edit / Profile Preferences / Scrolling / Scrollback / Unlimited
checked.

The exact location of this option may be different somewhere, but I can see that you are using Redhat.

SHIFT + Page Up and SHIFT + Page Down are the correct keys to work in the virtual console, but the vmware console doesn't have these terminal settings. The virtual console has a fixed scroll back size, it looks like it is limited by the video memory size according to this Linux Virtual Console Scrolling Documentation.

ALTERNATIVE FOR LINE-LINE PROCESSING

Ctrl + Shift + Up Arrow or Down Arrow

Unlike Shift + Page Up or Page Down, which scrolls the entire page, this will help with smooth linear scrolling, which is exactly what I was looking for.

Another alternative that may already be installed on your system is to use the GNU screen:

# This starts screen which adds basic window management in terminals screen # This starts the copy mode you can use to scroll [# Now use the arrows to scroll # To exit copy mode, do

Have a look at the man screen for more useful options (multiple windows, ...) ...

It seems not easy to do this: The Arch Linux Wiki does not list how to do this on the console (although it is easily possible on a virtual terminal).

Ctrl - b, then [, then you can use normal navigation keys to scroll (like Up Arrow or PgDn). Press q to exit scroll mode.