Last time we saw about how to use screen and the advantages of screen. But there are more useful screen tips and tricks.
Here I’m going to show you my most used screen commands.
As we saw we have created a screen session called ‘backup’ last time and logged in to that using screen -r backup or screen -x backup. But you need to remember that that is the name of that particular screen session. Inside a screen session we can have multiple screen windows. Not only that we can name each of these windows.
Once we create a screen with screen -S backup, we will have single screen window.
So I have to check other things while inside the screen during the back up run, for example, just need to see the back up logs at /var/mybackupscript. log how can I view it without detaching the screen? Opening a new terminal? Nah, that is not needed with the screen.
You can split your screen and see mutliple screen windows at a time. That is really cool… right? You will remember that Ctrl+a is the screen’s hot key. So just press Ctrl+a+| and it will split your screen vertically.
But there is nothing in the new screen window.
Now you need to create a new screen window there. You can move your focus from your current screen window to the newly created screen window by using Ctrl+a+Tab. Then press Ctrl+a+c. Now you see that you have got a terminal there and you can type the commands over there. So the back up script is running in our first screen window and we have split the screen and created a new window over the right side.
Now type the command tail -f /var/mybackupcript. log to see the real time out put of your back up scripts logs. Now you have got two screen windows and if can you the command Ctrl+a+Q to view it in normal mode. It will just minimize the other screen window and show your current screen window. If you want to see all the screen widnows in your screen session you can use the command, Ctrl+a+”. You may see that these screen windows does not have a name and it is simple called, 0 bash, 1 bash etc. You can name them also. You can use the arrow keys to select the screen window and Enter to open it.
Once you are in the screen window, press Ctrl+a+A and at the bottom of the screen the option will come “Set windows title to: bash”. Remove the bash and name it. In our case we will name the first screen window (0 bash) to backup script. Use the same method and we can name the second screen window (1 bash) to back up logs. There are many more key combinations for different tasks.
Below are my most used ones:
Ctrl+a+| –>> split the screen vertically
Ctrl+a+S –>> split the screen horizontally
Ctrl+a+Tab –>> move the focus between the screen windows once it has splitted
Ctrl+a+c –>> create a new screen window
Ctrl+a+Q –>> minimize all the other screen windows (in a split screen) and show only the current one.
Ctrl+a+” –>> show the all available screen windows
Ctrl+a+A –>> set the screen windows title (naming the screen windows)
Ctrl+a+spacebar –>> cycle through all the screen windows in order.
Ctrl+a+n –>> move to the next screen window
Ctrl+a+p –>> move to previous screen window
Ctrl+a+Esc+a –>> move the cursor to the top of the screen using the arrow key
Ctrl+a+x –>> lock the screen. You need to type your password to continue use the screen further to unlock it
Ctrl+a+h –>> writes hardcopy of current window to a file called hardcopy. n (just like a screenshot of the present screen window)
Ctrl+a+H –>> logs the screen window to the file screenlog. n. (this will log everything on the screen window continuously to the file screenlog. n (use tail -f screenlog. n to see the real time output) There are many more tips and tricks are there for screen.
But these are some of my most used shortcuts and commands. You can find an extensive list of option from screen man pages. (type man screen in the terminal)
UPDATE: More screen tutorials can be found below. How to directly login to the screen session running on your remote server using ssh