How to close, minimize, and maximize a specified window from Terminal?

There are two important tools to manipulate windows from cli; xdotool and wmctrl. Both tools overlap each other in functionality, but the most important commands referring to your question:

In an (no doubt incomplete) overview, listing the commands I use most:

Closing a window

wmctrl:

wmctrl -ic <window_id>

Minimizing a window

xdotool:

xdotool windowminimize <window_id>

Un- minimizing a window

wmctrl:

effectively un- minimizing is done by:

wmctrl -ia <window_id>

I mention effectively, since the command moves to the corresponding desktop, unminimizes and raises the window, but the command also does that on windows which are not minimized.

Maximizing a window

xdotool:

xdotool windowsize <window_id> 100% 100%

wmctrl:

wmctrl -ir <window_id> -b add,maximized_vert,maximized_horz

Un- maximizing a window:

wmctrl -ir <window_id> -b remove,maximized_vert,maximized_horz

Notes

  • Both xdotool and wmctrl are not on your system by default:

    sudo apt-get install xdotool wmctrl
    
  • To run any of the commands on the currently active window:

    • for wmctrl commands, remove the -i option, replace <window_id> by :ACTIVE:

    • for xdotool commands: replace <window_id> by $(xdotool getactivewindow)

  • In many cases, commands can be run by using either the window id or the window name. The -i option in wmctrl tells wmctrl to use the window id. I'd suggest not using the window's name as an identifier, to prevent name clashes. It happens more easily then you'd expect.

  • From my own experience, using maximizing in a script; Using wmctrl to maximize / unmaximize can be a bit buggy on both Unity and Gnome, while the xdotool option works more robust in my experience. In most scripts, I end up in using a mix of both wmctrl and xdotool.

More info on man wmctrl and man xdotool (mainly the section: WINDOW COMMANDS).


Adding to Jacob Vlijm's answer:

xdotool windowactivate $minwinid
xdotool windowraise $minwinid
xdotool windowfocus $minwinid

Also works for unminimizing a window. The animation is faster for me.