Kill all detached screen sessions

When I execute screen -ls, I see the following. How can I kill all the detached sessions?

There are screens on:

    84918.ttys002.ros-mbp   (Detached)

    84944.ttys008.ros-mbp   (Detached)

    84970.ttys013.ros-mbp   (Attached)

    84998.ttys002.ros-mbp   (Detached)

    85024.ttys002.ros-mbp   (Detached) 

5 Sockets in /var/folders/86/062qtcyx2rxbnmn8mtpkyghs0r0r_z/T/.screen.


Solution 1:

screen -ls | grep pts | cut -d. -f1 | awk '{print $1}' | xargs kill

Kill only Detached screen sessions (credit @schatten):

screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill

Solution 2:

Here's a solution that combines all the answers: Add this to your .bashrc or .bash_profile:

killscreens () {
    screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
}
  • this is a convenient function, easy to remember
  • kills only the detached screens, to stop you from doing something dumb
  • remember to open a new bash terminal or run source .bashrc to make killscreens available

Thanks to @Rose Perrone, @Milind Shah, and @schatten