7. Miscellaneous

$Id: misc.xml 2330 2009-04-17 15:45:52Z rafi $

Table of Contents

7.1. Reading Text Files
7.2. Editing Text Files
7.3. Printing
7.4. Search for Text in Files
7.5. File Manager

7.1. Reading Text Files

The easiest way to read text files is by using either more or less. Instead of piping the output from a command to them, you provide the file name of the file to be opened, as shown in Example 34, “Reading text files with more or less”.

Example 34. Reading text files with more or less

$ more <filename>

Replace <filename> with the file name of the file to be read. You could use less instead of more as well.


7.2. Editing Text Files

This is a somewhat difficult topic. There are many good editors out there for UNIX, but it heavily depends on the installation of the UNIX machine, which editors are available. No doubt, the most (in)famous editors are Emacs and vi. On almost every UNIX system you will find a sort of vi, but doing the most basic editing stuff can be a nuisance in vi.

The text editor I recommend for newbies is jed. It is easy to learn and has a text menu. You start jed by typing jed on the command line. You can optionally provide the file to open right away as argument to jed.

Jed is available for FreeBSD, Linux, and Sun Solaris, as far as I know.

7.3. Printing

This topic I will cover briefly. Ask your system administrator for further assistance with this topic.

7.3.1. Printing under FreeBSD

You submit a file to print to the default printer by calling

$ lpr <filename>

Replace <filename> with the file to print. Handling of different file types depends on the configuration. So it may not be possible to print PDF files, for instance. If you are in doubt, ask your system administrator for further help.

If you want to see which files are in the print queue, call lpq

$ lpq
lp@ava (originally hl5050) 0 jobs

To remove the currently active print job, type lprm on the command line. You can only remove jobs you sent to the printer, hence if you are the owner.

7.3.2. Printing under Linux and Sun Solaris

Submitting a file to the default printer is accomplished by issuing the following command

$ lp <filename>

Replace <filename> with the file to print. Handling of different file types depends on the configuration. So it may not be possible to print PDF files, for instance. If you are in doubt, ask your system administrator for further help.

If you want to see which files are in the print queue, call lpstat

$ lpstat
Printer: lp@ava 'hl5050'
 Queue: no printable jobs in queue
 Server: no server active
 Status: job 'william@penny+624' saved at 18:21:15.497
 Rank   Owner/ID               Pr/Class Job Files                 Size Time
done   william@penny+624 B/penny.example.org 624 stdin     1502236 18:20:46

To remove the currently active print job, type cancel on the command line. You can only remove jobs you sent to the printer, and thus you are the owner.

7.4. Search for Text in Files

The grep command lets you search for text in files, or if you pipe output from other commands to it, within that output. Grep uses very powerful regular expressions, but this topic is not covered here. In its simplest form, it searches for just the text you specify as shown in Example 35, “Using grep the easy way”.

Example 35. Using grep the easy way

The -n option tells grep to print the line numbers the text has been found. If this option is omitted, it simply prints the line containing the text.

$ grep -n '<searchstring>' <filename>

Replace <searchstring> with the text grep has to look for (grep is by default case-sensitive, so searching for Text is not the same as text). <filename> is replaced by the file grep has to search in. See below for a concrete example.

$ grep -n 'sec' unixsurvialguide.xml
17:<!-- The sections -->
20:<!ENTITY sec.conventions SYSTEM "typography.xml">
21:<!ENTITY sec.terminal SYSTEM "terminal.xml">
22:<!ENTITY sec.user SYSTEM "user.xml">
23:<!ENTITY sec.commands SYSTEM "commands.xml">
24:<!ENTITY sec.filesystem SYSTEM "filesystem.xml">
25:<!ENTITY sec.processes SYSTEM "processes.xml">
26:<!ENTITY sec.misc SYSTEM "misc.xml">
27:<!ENTITY sec.fdl SYSTEM "fdl-1.3.xml">
53:  &sec.conventions;
55:  &sec.terminal;
57:  &sec.user;
59:  &sec.commands;
61:  &sec.filesystem;
63:  &sec.processes;
65:  &sec.misc;
67:  &sec.fdl;
$

If you omit the file name, grep expects to get its input fed from a pipe as shown Example 36, “Grepping the standard output”.

Example 36. Grepping the standard output

This example shows how to search for a process name using grep. The ps command is explained in Section 6, “Processes”.

$ ps ax | grep 'ssh'
40009  ??  Ss     0:00.05 sshd: joe [priv] (sshd)
40011  ??  S      0:00.01 sshd: joe@ttyp0 (sshd)
40037  p0  S+     0:00.00 grep ssh

7.5. File Manager

Here I briefly introduce the GNU Midnight Commander, a text based file manager, that resembles the Norton Commander for UNIX.

It runs on Sun Solaris, FreeBSD, and Linux. It makes the file and directory operation trivial. See Figure 2, “The GNU Midnight Commander” for a screen shot of mc.

Figure 2. The GNU Midnight Commander

  Left     File     Command     Options     Right
+<-~---------------------------------v>++<-/---------------------------------v>+
|       Name      | Size  |   MTime    ||       Name      | Size  |   MTime    |
|/.cache          |     18|Dec 16 12:18||/etc             |    251|Jan  7 10:13|
|/.config         |     69|Dec 16 12:18||/export          |      4|Dec  3 21:16|
|/.dbus           |     24|Apr 25  2008||/home            |      2|Jan  7 10:13|
|/.dbus-keyrings  |     36|Jan  7 15:51||/kernel          |     19|Dec 18 23:11|
|/.designer       |      6|Dec 22 21:47||/lib             |    241|Dec 18 23:14|
|/.devsketch      |     25|Dec 22 22:17||/mnt             |      4|Jan  7 19:37|
|/.dia            |    111|Dec 16 00:22||/net             |      2|Nov 29 23:53|
|/.dt             |   4096|Dec 15 21:49||/opt             |     20|Dec 28 14:52|
|/.emacs.d        |     79|Oct 23 21:17||/platform        |      5|Nov 29 23:25|
|/.enlightenment  |   4096|Dec 15 21:48||/proc            | 130496|Jan  7 19:42|
|/.fluxbox        |     54|Dec 15 21:49||/rmdisk          |      2|Jan  3 19:59|
|/.fontconfig     |   8192|Dec  7 10:39||/rpool           |      4|Dec 19 10:46|
|/.gaim           |     82|Apr 22  2008||/sbin            |     52|Dec  4 22:59|
|/.gconf          |     44|Jan  7 10:17||/share           |      3|Dec  3 22:38|
|/.gconfd         |     24|Jan  7 15:52||/system          |      4|Nov 29 23:03|
+--------------------------------------++--------------------------------------+
|/.enlightenment                       ||/proc                                 |
+--------------------------------------++--------------------------------------+
Hint: Want your plain shell? Press C-o, and get back to MC with C-o again.
jack@salma ~>                                                            [^]
1Help   2Menu   3View   4Edit   5Copy   6RenMov 7Mkdir  8Delete 9PullDn 10Quit

If mc is installed, you may start it by typing mc on the command line. See Table 3, “Midnight Commander keys” for a short overview about the keys you can use with mc.

Table 3. Midnight Commander keys

KeyAction
F1Bring up a help window.
F2Show the user customizable menu.
F3View the currently selected file.
F4Edit currently selected file.
F5Copy the selected file or directory to the directory active in the other pane.
F6Move the selected file or directory to the directory active in the other pane. Can also be used to rename files.
F7Create a new directory in the currently active directory.
F8Delete the selected file or directory.
F9Activate the menu.
F10Quit Midnight Commander.
TabSwitch to the other pane.
InsertMark the currently selected entry.
Ctrl+OSwitch to the shell. Press again to switch back to Midnight Commander.