screen Command in Linux

Learn via video courses
Topics Covered

Overview

The screen command in Linux is a powerful tool that allows users to manage multiple terminal sessions within a single terminal window. It is particularly useful for running long processes, remote administration, and multitasking without using multiple terminal windows.

Linux screen Command Syntax

The syntax for the screen command is as follows:

Where:

  • options: These are the flags or parameters that modify the behavior of the screen command.
  • command: The command or program to be executed within a new screen session.

screen Command Options:

  • -S: Creates a new session with a specified name.
  • -ls: Lists all the active screen sessions.
  • -r: Reattaches to an existing screen session.
  • -X: Sends a command to a running screen session.

Example Usages

  • Create a new screen session:

    Output:

    Explanation: The command creates a new screen session without any specified options.

  • List all active screen sessions:

    Output:

    Explanation: The command lists all the active screen sessions with their respective session IDs.

Tips

  • To detach from a screen session without closing it, press 'Ctrl + A' followed by 'D'.

  • To switch between screen sessions, press 'Ctrl + A' followed by 'N' for the next session or 'P' for the previous session.

Advanced Use Cases of screen Command in Linux

  • Create a new named screen session:

    Output:

    Explanation: The command creates a new screen session with the name 'example'.

  • Reattach to an existing screen session:

    Output:

    Explanation: The command reattaches to the screen session with the name 'example'.

  • Send a command to a running screen session:

    Output:

    Explanation: The command sends the 'ls' command to the running screen session named 'example'.

Conclusion

  • The screen command is a powerful tool for managing multiple terminal sessions within a single terminal window.

  • Screen sessions can be named, listed, and reattached to as needed.

  • Commands can be sent directly to running screen sessions.