Learn › Linux Foundations › Special
script - a hands-on Linux lab on a real virtual machine.
Record a whole terminal session to a file with script. Name the file, exit to save, -a to append, -c to capture one command, and -q to record without banners. script runs a nested shell, so the drills teach the started-to-done shape and the one rule people forget: nothing is saved until you exit.
You just finished a long stretch of work in the terminal. Twenty commands, a wall of output, one error buried somewhere in the middle. Your boss asks: what exactly did you run, and what did it say? You scroll up. The top of it is already gone, pushed off the screen forever.
Copy and paste will not save you here. It misses whatever scrolled past. What you needed was a recorder running the whole time, quietly saving every line to a file you can read later. That recorder exists, and it has one name: script.
One honest thing up front. script works by starting a nested shell, a fresh terminal inside your terminal, and recording everything until you leave it. A practice sandbox cannot host that live sub-session, so the black boxes below show you the real banners script prints and the file it leaves behind, captured from a real machine. The graded part is the command YOU type. On the live machine at the end of this section, you run script for real and read back your own recording.
script records everything printed in your terminal session to a file, from the moment you start it until the moment you leave. Every command you type, every line of output, every error message, all captured in order.
The file it writes is called a typescript. That is an old word for a typed copy of something, like a transcript of a meeting. It has nothing to do with the TypeScript programming language; the name is decades older. If you do not name the file yourself, script calls it typescript by default.
The mental model is a security camera for your terminal. You switch it on, it records everything it sees, and it keeps recording until you switch it off. Nothing is saved to the file until you stop, so leaving is not optional; it is how the recording gets written.
script has been on Unix since 4.2BSD in 1983, which is why almost every Linux machine already has it. It ships as part of a core package called util-linux, so you rarely have to install it.
The simplest useful form is script followed by a filename. That starts the recorder and tells it where to save. Hand it a name like session.log and it prints one line to confirm it has begun:
script session.log
Before you run it, decide what you expect to see: does script drop you back at your normal prompt, or does it open a new recording shell first? Run it and watch:
prompt: student@linuxcamp:~$ answer: script session.log output: Script started, output log file is 'session.log'. hint: Type the word script, a space, then the filename: script session.log
That one line, Script started, output log file is 'session.log'., is the recorder confirming it is live. What you cannot see in a sandbox is that script has now dropped you into a fresh shell INSIDE your old one. From here, every command you type and every line it prints is being copied into session.log. You are on the record. The next question is how you get off it.
Here is the part people forget, and it is the whole trick. Nothing lands in the file until you LEAVE the recording shell. You leave the same way you leave any shell: type exit, or press Ctrl+D. That closes the nested shell, writes the file, and prints a matching goodbye line.
So the full cycle is three moves: script session.log to start, your commands in the middle, then exit to finish. Type the closing move now:
prompt: student@linuxcamp:~$ answer: exit output: Script done, output log file is 'session.log'. hint: Just the four letters and Enter: exit (Ctrl+D does the same thing)
Script done, output log file is 'session.log'. is the recorder telling you it stopped and saved. Notice the symmetry: Script started on the way in, Script done on the way out. Now session.log holds everything that was displayed between those two lines. Read it back with cat session.log and you will see your whole session, commands and output together, exactly as it happened. If you had never typed exit, none of it would have been written; the recorder only commits when you leave.
By default, pointing script at a file that already exists overwrites it. The old recording is gone. That is fine for a one-off, but sometimes you want to build one long record across several sittings.
The -a flag means append. It tells script to add the new session to the END of the file instead of wiping it. The started banner looks identical; the difference is that your earlier recording is still there, underneath the new one.
script -a session.log
prompt: student@linuxcamp:~$ answer: script -a session.log output: Script started, output log file is 'session.log'. hint: Put the -a flag between script and the filename: script -a session.log
The banner does not tell you whether you appended or overwrote; both print the same Script started line. -a is a promise to script, not something you can read back from the screen. When you care about keeping old recordings, reach for -a every time you reopen the same file.
Not every recording needs an interactive shell. Sometimes you want the output of exactly ONE command captured, with no sub-shell to enter and no exit to remember. The -c flag does that. You hand it a command in quotes and a filename; script runs that single command, saves its output, and quits on its own.
script -c "uptime" uptime.log
prompt: student@linuxcamp:~$ answer: script -c "uptime" uptime.log output: Script started, output log file is 'uptime.log'. 14:22:07 up 3 days, 2:16, 1 user, load average: 0.00, 0.01, 0.05 Script done, output log file is 'uptime.log'. hint: Pass the command in quotes after -c, then the filename: script -c "uptime" uptime.log
See the difference from the interactive form: both banners printed on their own, wrapped around the command's output, and you never had to type exit. With -c, the session begins and ends by itself the instant the command finishes. That makes -c the form to reach for inside an automated script, where nobody is sitting there to leave a shell. The uptime numbers above are one machine's; yours will differ. What holds everywhere is the shape: started banner, output, done banner, all captured to the file.
The Script started and Script done lines are helpful when you are learning, but they clutter a clean log or an automated run. The -q flag means quiet: it suppresses both banners. The recording still happens; you just do not see the announcements.
Because -q prints no banner of its own, there is nothing for the sandbox to show. Run it, then check the exit code with echo $? to prove it started and returned cleanly. Zero means success:
prompt: student@linuxcamp:~$ answer: script -q -c "true" quiet.log ; echo $? output: 0 hint: Combine -q and -c so it runs and quits with no banner, then ; echo $? to see the exit code: script -q -c "true" quiet.log ; echo $?
This is worth saying plainly: with -q, script prints nothing to the screen at all, no started line and no done line. That is not a failure. The 0 you see comes from echo $?, which reports the exit code of the command before it. A 0 there means the recording ran and finished cleanly, even though the recorder stayed silent.
Scaffolding off. No command is printed this time. You have every piece you need.
You want to capture the output of the single command hostname into a file called host.log, and you do NOT want to enter an interactive shell or type exit afterward. Recall the flag that records exactly one command and quits on its own, then put the command in quotes and name the file.
prompt: student@linuxcamp:~$ answer: script -c "hostname" host.log output: Script started, output log file is 'host.log'. linuxcamp Script done, output log file is 'host.log'. hint: The one-command flag is -c. Think script, then -c, then the command in quotes, then the filename.
That is the automated form, typed from memory. The -c flag ran hostname once, captured its output between the two banners, and quit without any shell for you to leave. The hostname line above is this machine's; yours will read your own. You recalled the flag and assembled the whole command yourself, which is the exact recall the real machine will ask of you.
Three failures come up often. Learn the literal text so you recognise each one instantly.
script: cannot open session.log: Permission denied This means script could not create or write the file where you asked. You are trying to record into a directory you do not have permission to write, such as a system folder. Check first WHERE you are pointing the file. Aim it at a folder you own, like your home directory, and it will open fine.
The recording seems to hang, and nothing you type ends the lab You are still inside the nested recording shell and forgot. It looks exactly like your normal prompt, which is the trap. Nothing is written and nothing moves on until you leave. Type exit (or press Ctrl+D) to close the recorder and write the file. When in doubt, type exit.
script: command not found Rare, but it means the util-linux package that provides script is not installed on this machine. Check that you spelled it correctly first, since that is the usual cause. If the spelling is right, the package is genuinely missing and an administrator would need to install it.
You earned this cheat sheet. Every row is a form of script you just ran:
The two lines to memorise are the pair that bracket every recording: Script started, output log file is '...'. on the way in, and Script done, output log file is '...'. on the way out. If you see the first but never the second, you are still recording.
script can also record TIMING, the pauses between each burst of output, if you ask for it. A partner command called scriptreplay reads that timing and plays the whole session back at its original speed, like watching a video of someone typing. That is one for later; for now, the started-to-done recording is the core you need.
The practice terminal has shown you the shape of script: a filename to start, exit to stop and save, -a to append, -c to capture a single command, and -q to record without the banners. Every one of those you typed yourself, and you learned the one rule people trip on: nothing is saved until you leave the recording shell.
The Special section ends with one real Linux machine, booted just for you, with its own live shell to record. The mission shows no commands. You read each objective, recall the form of script it calls for, and type it, then leave the recorder with exit and read your own typescript back. That recall, and remembering to exit, is what makes it stick.
Finish the other Special lessons, then go record a real session for yourself.
Practice script in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.