Exercise 2: Running a Command and Capturing the Result Remotely — Possible Solution ==================================================================================== $ screen -dmS work # if it doesn't exist yet $ screen -S work -p 0 -X stuff $'ls -l ~\n' # type the command + Enter $ sleep 1 # give it a moment to run $ screen -S work -p 0 -X hardcopy /tmp/work.txt $ cat /tmp/work.txt /tmp/work.txt contains what window 0 is displaying: the prompt, the ls -l command, and its output. Notes: - The newline in $'ls -l ~\n' is what "presses Enter". Without it, the text just sits on the command line. - The short sleep matters: stuff returns immediately, before the command has finished, and hardcopy only captures what's on screen at that moment. - hardcopy captures the visible screen only. For long output, use logging (Ctrl-a H or -L) instead. WHY THIS WORKS AS AN ANSWER ------------------------------ It pairs stuff (input) with hardcopy (output) to control a window completely from outside, and explains the timing and screen-size limits that often catch people out.