Four More Utilities

Four More Utilities

The echo and date utilities are two of the most frequently used members of the large collection of Linux utilities. The script utility records part of a session in a file, and unix2dos makes a copy of a text file that can be read on either a Windows or a Macintosh machine.

echo: Displays Text

The echo utility copies anything you put on the command line after echo to the screen. Some examples appear in . The last example shows what the shell does with an unquoted asterisk (*) on the command line: It expands the asterisk into a list of filenames in the directory.

Figure 5-9. echo copies the command line (but not the word echo) to the screen
$ ls
memo  memo.0714  practice
$ echo Hi
Hi
$ echo This is a sentence.
This is a sentence.
$ echo star: *
star: memo memo.0714 practice
$

The echo utility is a good tool for learning about the shell and other Linux programs. Some examples on page use echo to illustrate how special characters, such as the asterisk, work. Throughout , , and , echo helps explain how shell variables work and how you can send messages from shell scripts to the screen. Refer to the echo info page for more information.

date: Displays the Time and Date

The date utility displays the current date and time:

$ date
Thu Jan 20 10:24:00 PST 2005

The following example shows how you can choose the format and select the contents of the output of date:

$ date +"%A %B %d"
Thursday January 20

Refer to the date info page for more information.

script: Records a Shell Session

The script utility records all or part of a login session, including your input and the system's responses. This utility is useful only from character-based devices, such as a terminal or a terminal emulator. It does capture a session with vim; however, because vim uses control characters to position the cursor and display different typefaces, such as bold, the output will be difficult to read and may not be useful. When you cat a file that has captured a vim session, the session quickly passes before your eyes.

By default script captures the session in a file named typescript. To use a different filename, follow the script command with a SPACE and the new filename. To append to a file, use the a option after script but before the filename; otherwise script overwrites an existing file. Following is a session being recorded by script:

$ script
Script started, file is typescript
$ date
Thu Jan 20 10:28:56 PST 2005
$ who am i
alex      pts/4    Jan  8 22:15
$
$ apropos mtools
mtools               (1)  - utilities to access DOS disks in Unix
mtools.conf [mtools] (5)  - mtools configuration files
mtoolstest           (1)  - tests and displays the configuration
$  exit
Script done, file is typescript
$

Use the exit command to terminate a script session. You can then view the file you created with cat, less, more, or an editor. Following is the file that was created by the preceding script command:

$ cat typescript
Script started on Thu Jan 20 10:28:56 2005
$ date
Thu Jan 20 10:28:56 PST 2005
$ who am i
alex      pts/4    Jan  8 22:15
$
$ apropos mtools
mtools               (1)  - utilities to access DOS disks in Unix
mtools.conf [mtools] (5)  - mtools configuration files
mtoolstest           (1)  - tests and displays the configuration
$ exit
Script done on Thu Jan 20 10:29:58 2005
$

If you will be editing the file with vim, emacs, or another editor, you can use dos2unix to eliminate from the typescript file the ^M characters that appear at the ends of the lines. Refer to the script man page for more information.

unix2dos: Converts Linux and Macintosh Files to Windows Format

If you want to share a text file that you created on a Linux system with someone on a Windows or Macintosh system, you need to convert the file before the person on the other system can read it easily. The unix2dos utility converts a Linux text file so that it can be read on a Windows or Macintosh system. Give the following command to convert a file named memo.txt (created with a text editor) to a DOS-format file:

$ unix2dos memo.txt

Without any options unix2dos overwrites the original file. You can now email the file as an attachment to someone on a Windows or Macintosh system.

dos2unix

You can use the dos2unix utility to convert Windows or Macintosh files so they can be read on a Linux system:

$ dos2unix memo.txt

See the unix2dos and dos2unix man pages for more information.

You can also use tr to change a Windows or Macintosh text file into a Linux text file. In the following example, the d option causes tr to remove RETURNs (represented by \r) as it makes a copy of the file:

$ cat memo | tr -d '\r' > memo.txt

The greater than (>) symbol redirects the standard output of tr to the file named memo.txt. For more information refer to "" on page . Converting a file the other way without using unix2dos is not as easy.