yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Terminal Lesson 24


7m read
·Nov 3, 2024

Hey guys, this is Matt Kidsonowan.

With our 24th terminal lesson, in this video, I'm going to be showing you how to use the Unix screen command.

Now, the screen command is basically a window manager which lets you run multiple commands and see multiple commands' output at the same time in one terminal window.

In extension, this works over SSH and over pretty much any other kind of remote shell program. So, it's pretty excellent for managing a server or even for just using on your Mac locally.

So, let me just jump right in and show you how to get started using the screen command.

All you have to do is type screen and hit enter, and then hit space. Now it brings you to a blank prompt, and currently, we're actually in a screen session. But it'll look normal. You know, you can type a normal command; it behaves like a normal shell and what have you.

But there are a couple new features that we now have because we're in a screen session. So, for instance, let me go ahead and run a command that's going to take a long time to execute: du /. This will list every file on my hard drive, and that's obviously going to take a little while.

But let's say I'm running this command, and I also want to run something else in terminal. Typically, what I would do is I would open a new tab and do my stuff in here, or I would open a new window and do my stuff in here.

But screen actually makes it possible to do everything in the same window while the commands are running side by side. So, all I have to do is create a new screen window.

To do this, I use the screen window create command, which is Control + A, C. So, pretty much every screen command begins with Control + A. You're usually going to be pressing Control + A and then press some other letter; in this case, the letter C is the create command.

But basically, what the create command does is it creates a new virtual window in the screen session, and just like I can create a new tab, Control + A, C just creates a new screen window.

So now, let's go ahead, and I'm going to show you how to switch between windows and see your windows.

So, let's say, for instance, that I type something in here, whatever, and I want to go back and just see the output of du / in the other window. All I have to do is press Control + A, N, and this will switch to the next window in screen.

So, Control + A, N will switch to the next window. When there are only two windows, it'll obviously swap between the two windows.

And I can actually see how many windows there are just by pressing Control + A, W. As you can see here in the corner, it says 0 is bash, 1 is bash, and there's an asterisk next to the current window.

The indexing begins at zero, so the first window you open is window zero, the second one is window one. Uh, it's kind of confusing, but you'll get used to it.

But, for instance, if I were to press Control + A, N, and then I did Control + A, W, you can see that there's an asterisk next to window 0, and if I go back here, there's an asterisk next window 1.

So, screen makes it very easy to switch between windows just using Control + A, and to go to the next window, and that's really useful when you only have two windows open at once in screen.

But let's say I just create another window, and I'm going to do this just to print this out so that way I can remember that this is the third window because it gets kind of confusing.

But as you can see here from my window list, I now have three windows, and I'm currently on the third one.

I can switch just like I normally would between these just with Control + A, N. However, if I go to the first window, let's say I just want to jump to the third one; I have to press Control + A, N twice still, and that's just frustrating.

And, you know, you have six windows, you don't want to press it six times or, you know, five times or however many.

And because of this, screen actually has a way to jump to a certain window number. So, if you review right here, windows 0 is the first one, window 1 is the second one, etc.

Let's say I just want to switch to window 1. All I have to do is press Control + A, 1, and it switches me right over there.

And, as you can see, I'm at window 1 now, and Control + A, N will bring me back here, and I can even do like Control + A, 0 will bring me to the first one again.

So, Control + A and then a number will jump to a certain window number, and Control + A, N will just go to the next window.

Now, there's one other thing that's pretty essential to window management, and that is closing a window.

Because let's say I have three windows open, and I'm no longer doing anything with this third window right here. So, what if I want to just close it?

Well, all I have to do is press Control + A, K, and then hit yes to confirm, and now it's just brought me back over here to my second window.

I can still switch between the two windows; it just closed my third window. And I can close this one as well.

I can do another Control + A, K, and I can even create another window here and just show the list one more time.

So, right now, you know how to basically manage a window in every way possible.

You know how to create windows, you know how to switch between windows, you know how to delete a window.

But this isn't all that screen has to offer. Screen also has something called session management, and this makes it possible to quit terminal, quit SSH, kill whatever, and keep your session running, your screen session.

And then you can resume the session later. So, let's say I want to do that; I have two windows here in screen right now, and I want to quit terminal and keep the programs that are running in the screen session running.

All I have to do is detach the screen session by typing Control + A, D, and as you can see, screen has just been terminated; it says detached.

And now, I could go ahead and quit terminal like I just did, but now, let's say I open terminal back up, and I want to resume my screen session from before.

All I have to do is type screen -r, and it brings me right back to where I was.

I can see the list of windows, I can switch back between the two, and I can detach again. You know, do various things like that, and I can reattach as many times as I want.

Um, so this is a very easy way to run a couple programs in the background at once. You know, I could run a process here, detach it, quit terminal, open terminal back up, reattach, and I'll just see the output.

So, screen makes it really easy to run stuff in the background, and this is especially useful if you have a server and you want to just keep a program running, like a script or something running on the server.

You know, you can run it in screen, detach your screen session, and log out of SSH, do whatever, come back later and see the output of your script.

And on top of all of these features, screen also can manage multiple sessions.

So, let me just detach the screen session again, and now I'll type screen -list.

Screen -list will show a list of all the screen sessions that are currently running, and right now there's only one session right here, and it's listed as detached because, you know, currently it's detached because I just pressed Control + A, D.

But I can actually run another screen session, and all I have to do to start another screen session is type screen again with no -r, and you know, I'll just type like ls -lr / and this will start listing every file on my hard drive again.

I can detach this, and now if I type screen -list, you'll see two things in the list.

But now there's a complication because screen -r won't just resume the screen session; it doesn't know which one to resume.

And all you have to do for this is just type screen -r, and then copy and paste this name right here and paste it right in and hit enter, and it'll resume whichever session you just specified.

In this case, it's this one, and I can detach again, and I can see the list, and they're both still there.

But now, let's say I want to actually kill a screen session; I want to stop the screen session. I want to get it out of this list altogether.

All I have to do is let me just resume one of these. I'll just resume this one. All you have to do to kill a screen session once you're in the screen session is press Control + A, Control + Backslash, and then hit Y.

And now it just terminated that screen session, and if I show my list now, you'll only see this one.

So, I can resume this, I can kill this, and I can do, you know, another Control + A, Control + Backslash, and kill this one as well.

And if I do a screen -list now, it'll show that there are no currently active screen sessions.

But anyway, this is just how to use screen to maintain, uh, manage and maintain windows, to keep processes running in the background, to resume your screen sessions, all the good stuff.

I really appreciate screen; I think it's great for anyone who has a server, you know, for sysadmins.

I can't imagine you working without screen; you know, it's just an excellent command.

Screen also supports copy and paste, but that's not really useful if you're working in a terminal application like this one, so I'm just gonna skip over that in this video, but you can certainly look it up.

But anyway, thanks for watching Mac Heads 101. Leave any comments with any questions you might have, subscribe, and goodbye.

More Articles

View All
Warren Buffett: How to Find Great Stocks for 2023
Okay, so you’ve seen that the market is down at the moment. You know you should be investing right now, but how on Earth do you actually find great stocks to invest in? Well, in this video, we’re going to talk about a surprisingly simple screening method …
Mr. Freeman, part 04
Hi, how many people are there! What are you chewing? Mmm… I thought trophy canned food. Common, enough! Draw your attention away from your meal and let’s imagine… You are a rich manufacturer. Yes-yes, you are. On the excellent tall ship, you carry brocad…
You can use data to boost your career. Here’s how. | Neil Irwin | Big Think
So one of the most fundamental shifts which happened in technology and how big organizations work is that we have more and more data about what we do. Some of that is simple forms. I mentioned I work in the media industry; I know how many people read each…
Can You Build a House With Hemp? | National Geographic
[Music] Some of the most practical uses of industrial hemp in the modern day, of course, are the same as they ever were: building materials, paper, textiles, seed oil, nutrition. Hempcrete, of all the 50,000 known products that we can make with industrial…
The Monkeys of Sri Lanka | Born Wild: The Next Generation
The tropics of South Asia are teeming with wildlife of all kinds. National Geographic Explorer Sandesh Qader travels to a magical setting in Sri Lanka, where different species of baby monkeys learn to navigate life in a complex community. I’m an incredibl…
Adding vectors in magnitude and direction form | Vectors | Precalculus | Khan Academy
We’re told that vector A has magnitude 4 in direction 170 degrees from the positive x-axis. Vector B has magnitude 3 in direction 240 degrees from the positive x-axis. Find the magnitude and direction of vector A plus vector B. So pause this video and see…