How to Copy Terminal Session into a File
Have you ever wondered how to copy the output of your terminal into a text file? Or maybe you teach Linux and you want to see what your students typed in and as well as the output? You think that running history is not enough? Then you need the script command.
Running script command
Open the man page of script command and you will see this:
Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).
In a nutshell, it is history and tee all rolled into one. It will record everything you see on your screen, even the color. So if you typed in an invalid command, you will see the error in the log or if you run it correctly, you will have the output. But commands like top that refreshes the screen at an interval will most likely ruin the session or the log, so try to avoid similar commands.
To use it, just type the command script and it will begin recording the session. Once you are done, just type exit.
This is script in action:
rai@host1:~> script -a /tmp/script_test.log
Script started, file is /tmp/script_test.log
rai@host1:~> ls /home
R20 r200 R21 rai xx19
rai@host:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit
Script done, file is /tmp/script_test.log
rai@host1:~> cat /tmp/script_test.log
Script started on Mon 17 Jan 2011 06:24:12 PM PHT
rai@lhost1:~> ls /home
R20 r200 R21 rai xx19
rai@host1:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit
Script done on Mon 17 Jan 2011 06:24:54 PM PHT
The example above shows that script was started with -a option meaning it will append the output the specified file.
A better way to do this is to use it together with mkfifo command:
On Terminal 1 (Student’s terminal):
rai@host1:~> mkfifo /tmp/script_test.fifo
rai@host1:~> script -f /tmp/script_test.fifo
On Terminal 2 (Teacher’s terminal, same machine):
rai@host1:/tmp> cat /tmp/script_test.fifo
The above scenario will perform the following:
1) On the Student’s terminal, it will create an named pipe /tmp/script_test.fifo (man mkfifo) then run the script command with the -f option that ‘flushes’ out the output after each run. The Student’s terminal will look like it is not responding at this point, but don’t worry, it is perfectly normal.
2) On the Teacher’s terminal, the command cat will read the output file. Once you run the cat command, the session will be started.
Try the above steps and see how each screen behaves. Check also if doing the script command will create a populated output file.
Tip: Remove Duplicate Lines in File Using Perl
Here is little nifty code I found to snip or remove duplicate lines in a file using Perl. An example of a file that has duplicate lines looks like this:
/home/index.php
/home/links.php
/home/index.php
The result we want to achieve is this:
/home/index.php
/home/links.php
And here goes the code:
$ftmp = 'file.txt';
my %match = ();
{
local @ARGV = ($ftmp);
local $^I = '.tmp';
while(<>){
$match{$_}++;
next if $match{$_} > 1;
print;
}
}
The original file, file.txt, has its duplicate lines removed.
Linux File Directory Structure
When I am introducing Linux to new users like students, I noticed that it takes time for the new user to get used to Linux, particularly the directory structure. Since most are Windows users, they know how the directory structure of Windows looks like, and expects that it will be the same with Linux. To properly illustrate the Linux directory structure, linuxconfig.org has an illustration of the Linux File Hierarchy for easy explanation:
Understanding the directory hierarchy of Linux will result in ease of navigation while running the OS, regardless of the distro (Ubuntu, Red Hat, Fedora, etc.).
Search PinoyTux
Subscribe to Email Feeds
Blog Lounge
Popular Posts
Recent Posts
Drop your Card Here
Recent Comments
- Anidich1 on Tip: Add User and Generate Password Script
- Tom S on Cebu Pacific Sucks
- kadersardar on PinoyTux Spreads Some CommentLuv
- Steve on Creative Labs Threatens Third Party Driver Modder
- Barry on Free Laptops with Broadband Connection









