[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ 14 ] [ 15 ] [ 16 ] [ 17 ] [ 18 ] [ 19 ] [ 20 ] [ next ]


Debian User Reference Manual (Obsolete Documentation)
Chapter 6 - Basic Commands and Tools


author = Oliver Elphick olly@lfix.co.uk

topics = head, tail, grep, etc.

Latest update to this chapter: 14th December 1997


6.1 What this chapter covers

This chapter tells you how to run the basic commands that you need in order to use Debian Linux.

We assume that you have already logged in and are in a shell where you can enter commands. Refer back to Overview of a Debian GNU/Linux System, Chapter 2 for instructions on how to log in. If you are not running X-Windows, you will normally be put in an interactive shell as soon as you log in. If you are running X-Windows, you should refer to X Windows, Chapter 17 for instructions on how to start up a window with a shell.

The available shells are described in Shells, Chapter 5. All the examples given here assume that you are using the Bourne-Again Shell, /bin/bash, though in most cases there will be no difference between the command syntax for any shell. Bash is the default shell when Debian Linux is first installed.


6.2 Running commands


6.2.1 How to enter commands

To run a command, type the command at the prompt, followed by any necessary options, and then press the Enter key. The shell will interpret the command, first handling any special shell syntax, and will then start the command as a new process.

Most commands operate silently unless they are specifically asked to say what they are doing. If there is no error message, the command should have worked.


6.2.2 How Linux finds commands

When you type in a command name, Linux has to find the file which contains the program you want to run. This is how it does it:

The first word of what you type in is the command[3] itself. If this contains a slash character (/) the whole command is taken to be a path, either absolute (if it starts with /) or relative to the current directory. The file indicated by that path is used, provided that it exists, that it is executable and (in the case of a script) that it is readable.

If the command does not contain a slash, the shell searches for it in your current search path. This is a list of directories, separated one from another by colons. The command search path is held in the environment variable PATH: type echo $PATH to see what it is. For example,

     
     $ echo $PATH
     /usr/bin:/bin:/usr/bin/X11:/usr/local/bin:/usr/games:/home/olly/bin

Each directory is searched in turn, starting at the beginning of the list. If a match for the name is found, the shell also considers whether the file found is executable (and readable, if it is a script). As soon as such a match is found, the search stops[4]. This is why new programmers often have trouble getting their first program to run. They frequently call it test and run it without a path; the shell finds the system program test first and runs that instead.

If you need to change your path, you should add the new directories to the end of the list; the current directory (.), if it is included at all, should go last of all[5]. The command to use is export PATH=$PATH:new_directory[6] .


6.2.3 Command options

The operation of most commands can be changed by putting command options immediately after the command name. There are several styles of options used, and you have to check the documentation for each command to know what options it can take, and what they do.

The most common form of option is a letter preceded by a hyphen, thus:

     ls -l

With this option style, several single-character options can be combined into one, thus:

     ls -ali

which is equivalent to

     ls -a -l -i

Sometimes, single-letter options like this need an extra argument:

     awk -F: -f /tmp/bar /tmp/foo

Here : and /tmp/bar are arguments to the options -F and -f respectively.

A few commands have options which need not be preceded by a hyphen. The one you are most likely to use is tar.

     tar cvmf /dev/st0 /usr

is the same as

     tar -cvmf /dev/st0 /usr

A third option style, commonly used by GNU programs, is whole words or phrases introduced by --. Many commands can use both the single-letter and whole-word options:

     tar --create --verbose --modification-time --file /dev/st0 /usr

Finally, commands may define completely different option patterns for themselves, such as options preceded by + as the opposite to -.


6.2.4 Essential commands

These commands are the ones you really need to know to get anything done.

We give only a brief description of the command, describing its basic use. For full details, you should always refer to the detailed documentation of the manual pages and the info files.

The brief examples given here are not by any means the only way of doing things; the key to using Unix is to learn the building blocks and their capabilities and become able to use them in your own way. Whatever the task, there is almost always more than one way to carry it out. Sometimes there will be five or six different ways!

In this chapter, things which are optional are shown like this. Words looking like this are to be replaced by whatever is appropriate to what you are trying to do.


6.2.4.1 Commands for reading documentation

To avoid annoying other users by questions on elementary matters, do, please, read the manuals!

These are the commands to let you do that:

man [section-number] subject

man shows the manual page on the command (use q or ctrl-C to get out of it if it doesn't terminate at the end of the text). More details are given in Documentation, Chapter 3.

This command is crucial for helping you learn how to use it and other Linux commands.

info [subject]

A lot of Debian Linux documentation is provided in info format. This is similar to a hypertext format, in that you can jump to other sections of the documentation by following links embedded in the text.

See Documentation, Chapter 3 for a full description of how to use info.

help [subject]

Use help for on-line help about bash's built-in commands.

help by itself prints a list of subjects for which you can ask for help.


6.2.4.2 Commands for basic file-handling

A fuller description of file-systems and file-handling is given in Files and File Systems, Chapter 4.

These are the commands that you really have to know:

pwd

Show the current directory.

cd [directory]

Change your current directory to the named directory. If you don't specify directory, you will be returned to your home directory.

mkdir new_directory

mkdir is used to create a new directory. new_directory can be specified as a path, or as a simple name (in which case it is created in the current directory).

mv source target

mv moves source file to target and deletes source; source can be a list of files, but in that case target must be a directory.

This is also the way to rename a file or directory.

cp source target

cp copies source to target. Again, target can be a directory and source a list of files.

ln source target

ln links source to target with hard or symbolic links according to the options.

rm filelist

rm deletes files. With the -r option it can delete directories and their contents.

Be very careful with rm; once a file is deleted, you cannot get it back. There is no undelete command.

rmdir directorylist

rm deletes empty directories.

chmod mode filelist

chmod changes the permissions of files and directories. The mode is either an octal number, such as 644, or a symbolic representation of it, such as u=rw,go=r.

chown owner[.group] filelist

chown changes the ownership of files. Use the -R option to do this for an entire directory tree.

If .group is specified as well as owner, the group ownership of the files is changed as well.

chgrp group filelist

chgrp changes the group ownership of files. Use the -R option to do this for an entire directory tree.

ls directory

ls lists the contents of directory; by default, the output is sorted.

There are more options to ls than to any other command. Those most commonly used are -l, to show the file permissions and the latest change date and -a, to show hidden dot-files.


6.2.4.3 These commands are used for managing file-systems

Files are organised into file-systems (see Files and File Systems, Chapter 4).

Every Debian Linux system (and every Unix system) has a root file-system, called, simply, /. All files and devices in the system, and possibly files on other systems as well, are accessible under /. Files are placed under directories, which may themselves be placed in directories, and so on. Segments of this universal tree will be on different physical devices, or different sections (`partitions') of a single physical device. New file-systems, each with its own directory tree, may be grafted into the universal directory tree at arbitrary `mount-points'. (These are simply directories which are nominated to be the head of the new tree.)

The process of grafting a segment into the main tree is called `mounting'; the file system is mounted on a mount-point, which is a directory.

df

df lists mounted partitions and shows the amount of space present, used and available. (The partitions will probably include /proc, which is a special `non-existent' partition, that contains information about the current state of the Linux kernel.)

mount [partition directory]

mount by itself lists mounted partitions.

It is also the command to use to add a partition's file-system into the universal directory tree. For example:

     mount /dev/hda2 /usr

You have to be superuser (log in as root or run su) in order to mount or unmount file-systems.

umount directory

umount removes a partition's file-system from the universal directory tree. For example:

     umount /usr

You have to be superuser (log in as root or run su) in order to mount or unmount file-systems.


6.2.4.4 Looking at files

Files include physical devices, executable programs and directories, as well as text files. It is as well not to look at compiled executables on the screen however. At best they will be garbage; at worst they will make everything on the screen unreadable (see Managing your screen, Section 6.2.4.5 for a way to recover from this.)

cat textfile[s]

cat dumps textfile[s] to screen without paging (like DOS's type). If you name more than one file, each will be sent in order, with no break or heading.

more textfile[s]

more lists the textfile[s] to the screen in pages so that you can read it. Press Space-bar to move on one page, Enter to move on one line, and b to move back one page. Use q or ctrl-c to quit.

more will quit automatically when it gets to the end of the last file.

less textfile[s]

less is like more but has more features and somewhat different behaviour. You have to type Q to terminate it.

head textfile[s]

head is used to see the first few lines of a file (by default, it shows 10 lines).

tail textfile[s]

tail is used to see the last few lines of a file (by default, it shows 10 lines).

od -cx file[s]

od is used to look at the contents of binary files. With no options, it displays the file contents as a series of octal numbers. Since few people are fluent with this form, the -c and the -x options together are probably more useful. They display the file contents as real characters where possible and as octal numbers if the characters are not displayable, with, underneath, the hexadecimal representation. For example:

     $ od -cx /bin/ls | more
     0000000 177   E   L   F 001 001 001  \0  \0  \0  \0  \0  \0  \0  \0  \0
             457f 464c 0101 0001 0000 0000 0000 0000
     0000020 002  \0 003  \0 001  \0  \0  \0 240 217 004  \b   4  \0  \0  \0
             0002 0003 0001 0000 8fa0 0804 0034 0000
     0000040 204   o  \0  \0  \0  \0  \0  \0   4  \0      \0 005  \0   (  \0
             6f84 0000 0000 0000 0034 0020 0005 0028
     0000060 026  \0 025  \0 006  \0  \0  \0   4  \0  \0  \0   4 200 004  \b
             0016 0015 0006 0000 0034 0000 8034 0804
     0000100   4 200 004  \b 240  \0  \0  \0 240  \0  \0  \0 005  \0  \0  \0
             8034 0804 00a0 0000 00a0 0000 0005 0000

[7]

The numbers in the left hand column are always octal; they indicate the byte offset of the first character on the line from the beginning of the file.

dd if=input_file of=output_file

dd is used for arbitrary copying between files and devices (either file1 or file2 or both could be devices instead of files.) You can use dd to copy a file while dropping its first few bytes, to break a file into chunks, to copy a disk image onto a floppy (for instance to get a Debian boot floppy), to inspect the contents of a tape, or anything else that your ingenuity can suggest.

find directory -name filename

find tells you where filename is in the tree starting at directory. This command has many other useful options.

xargs command

xargs is commonly used with find. It breaks long lists of files given to a command into chunks that are short enough for the shell to handle. In this way, you can run through the entire file system and execute some command on all the selected files. For example, this command looks for all files in your home directory that have been changed within the last two days, and sets group write access on them all:

     find $HOME -mtime -2 | xargs chmod g+w
grep pattern filelist

grep scans each file in filelist for lines which match pattern (see Regular expressions, Section 6.3).

file file

file reports what kind of thing file is.

type file

type says where to find file in your search path.

whereis file

whereis is like type, but gives more information.


6.2.4.5 Managing your screen

Managing your screen can be a very frustrating experience.

When you have sat helpless for a while trying to work out why your entire display contains total garbage, or why pressing an arrow key puts irrelevant characters up, you may well feel that it's time to tip the whole computer system into the dustbin and go back to the abacus. This section offers you a lifeline.

First of all, here are some commands to handle what you can see on your screen. The screen can be either the whole text display or a single text window in X.

tput clear

All VDUs attached on serial cables, and all memory-mapped displays that emulate VDUs, including your text console and your xterm windows, respond to special character sequences that will change the appearance of the whole screen, or portions of it, when they are sent to the screen. Usually, these sequences start with the ESCAPE character, but they don't have to. Because of the vast number of different VDUs produced by many different manufacturers, mostly with different control sequences, Linux has a database of the sequences that apply to different types of VDU. The database appears in two forms -- the older is called termcap and the newer, terminfo.

tput is a command that reads the terminfo database for the sequence named, translates that sequence to screen control characters and sends those characters to the screen. The clear sequence blanks the screen and puts the cursor in the top left hand corner.

tput rmacs

If, intentionally or not, you display a binary file on the screen, you may chance to send to the screen the sequence that makes it display its alternative character set; this usually contains a lot of graphics line-drawing characters, card suits, odd mathematical symbols and so on. The computer's attempt to display ordinary text in this character set produces a screen containing total garbage. The rmacs sequence switches the display out of the alternate character set. Use this command to stop the display showing garbage graphics characters and make it show proper text instead. (The effect is usually to change new characters sent to be proper text, but leave text already displayed as it is.)

stty

stty controls communication between a VDU and the computer. It controls characteristics such as the baud-rate, parity, number of stop-bits and handshaking, all of which should be set up for you by your system administrator (if that is you, read the System Administration manual). In the case of your console, or of an xterm window, which are mapped directly from memory, the line characteristics are irrelevant, but there are certain characteristics set by stty which are relevant to every kind of display.

These are some characteristics could be used by you if it seemed necessary. (If it is necessary, you may find that nothing at all seems to work; this may be because the return key no longer works and Linux has reverted to using the basic default, which is new line (Ctrl-J). If nothing at all seems to work, press Ctrl-J before entering any command and finish your commands with Ctrl-J instead of return.)

sane

stty sane is one option to try if everything has stopped working. Remember to use Ctrl-J both before and after the command and don't use the return key.

echo

Use stty echo if what you type doesn't appear on the screen.

intr '^C'

Use stty intr '^C' to make Ctrl-C work as your interrupt key (which will tell the running program to stop).

-a

Use stty -a to show you what your existing stty settings are.


6.2.4.6 Compressed files

In order to save space on your disks, many files are compressed. They are scanned by a program such as gzip or compress and reduced in size, often to about one third of their original size.

Compressed files cannot be read unless they are uncompressed again.

gunzip file.suffix

gunzip uncompresses file.suffix (where suffixis .gz or perhaps .Z), deletes it and writes an uncompressed file called file.

In spite of its name, gunzip cannot handle zip format files created by PKZIP. There is a pkunzip program that can handle such files.

zcat compressed_file

zcat does the same job as cat, but uncompresses compressed_file first, without changing it on the disk.

zmore compressed_file

zmore does the same job as more, but uncompresses compressed_file first, without changing it on the disk.

zless compressed_file

zless does the same job as less, but uncompresses compressed_file first, without changing it on the disk.

zgrep compressed_file

zgrep does the same job as grep, but uncompresses compressed_file first, without changing it on the disk.


6.2.4.7 Commands for backup

You need to do regular backups, in order to be able to recover your system and data in case of disaster. In order to get full access to all the files that need to be backed up, system backups must be done by the superuser. This section refers only to backups of your own home directory and of other files to which you have access.

These programs are used for backups of various kinds:

tar

tar is designed to archive directories with all their contents. It has many options, but here is a simple example of using it to create a backup of your home directory to a SCSI tape:

     tar cvf /dev/st0 $HOME

Here is how to read the contents of a tape that has been written with tar:

     tar tf /dev/st0

and here is how to restore a particular directory (with its contents) from such a tape backup:

     tar xvf /dev/st0 $HOME/private/letters

You can also restore a single file:

     tar xvf /dev/st0 $HOME/private/letters/bank.21Aug
cpio

cpio is used to write individual files to a backup. A list of files is prepared in some way and then piped into the standard input of cpio. This is how to use it to archive your home directory to SCSI tape:

     find $HOME -print | cpio -ovBH newc >/dev/st0

Here is how to read the contents of a tape that has been written with cpio:

     cpio -itBH newc </dev/st0

and here is how to restore a set of files in one directory whose names all start with `bank' from such a tape backup:

     cpio -itBH newc "$HOME/private/letters/bank*" </dev/st0
afio

afio is used like cpio to write individual files to a backup. It has the additional capability of compressing files before it writes them on the backup. This is useful if you have a lot of information to store on media of limited capacity. This is how to use it to archive your home directory to SCSI tape, using compression:

     find $HOME -print | afio -ovZ >/dev/st0

6.2.4.8 Printing files

There is one basic command for printing (though there are many for specialised uses):

lpr file

lpr prints file to the default printer. lpr is the user command for adding jobs to the spooler queue. The jobs will continue to print, even if you log out.

If you have more than one printer, you can use the option -Pprinter to direct a job to one particular printer.

lpq

lpq shows the current list of jobs on the printer spool queue.

lprm jobId

lprm removes the job identified by jobId from the spooler queue. If a job is already printing, this may not stop it; the job may already be stored in the printer's memory


6.2.4.9 Changing files

See Text Editing, Chapter 8 for information on editing files.


6.2.4.10 Session management

A session is the series of events between logging in and logging out of the computer.

When you first log in, you will see a prompt, which may contain your current machine's hostname, and perhaps the current directory. The prompt ends with a dollar sign: $, which indicates that you are an ordinary user. If you become superuser, the dollar sign should change to a hash (or sharp) sign: #. You need to know that you are superuser, because you can then do a great deal of damage to your system by mistyping ordinary commands.

These commands relate to handling your session:

exit or logout

Use exit or logout to terminate your session and log out. All jobs you have started are terminated, unless they were started with nohup, and your screen should return you to the log-in prompt. You can often use Ctrl-D to do the same, but that capability can be disabled.

su

Use su to become superuser. You will be asked to enter a password; this is the password for the root login.

Becoming superuser is almost the same as logging in as root. You get the same power to do damage. Be very careful!

Use exit, logout or ctrl-D to stop being superuser.

bg and fg

If you are running one job and want to run something else in the same screen without stopping the first job, you can put the first job in the background by typing Ctrl-Z. You can then run other commands. You can start the first job running again in the background by using bg. It will continue to run in the background until it wants to send anything to the screen; then it will stop and wait. You can bring it forward again and let it continue by using the command fg.

script file

Use script to record everything that appears on the screen (until the next exit) in file. This is useful if you need to record what's going on in order to include it in your message when you ask for help. Use exit, logout or ctrl-D to stop the recording session.

ps

Use ps to list your processes.

     $ ps
       PID  TT STAT   TIME COMMAND
      6122  p1 S      0:00 bash 
      6123  p2 S      0:00 bash 
      6408  p1 S      0:00 ftp 
      6916  p4 R      0:00 ps 
     13115  p7 SW     0:00 bash 
     19102  p3 SW     0:00 bash 
     19497  p4 S      0:00 -bash 
     19554  p3 S     11:25 uvsh 
     19623  p4 S      5:37 xemacs 
     21272  p6 SW     0:00 bash

You can also use ps to see all processes on the system:

     $ ps -ax
       PID  TT STAT   TIME COMMAND
         1  ?  SW     0:18 init 
         2  ?  SW     0:04 kflushd 
         3  ?  SW<    0:00 kswapd 
        10  ?  S      0:18 update 
        31  ?  SW     0:00 nfsiod 
        32  ?  SW     0:00 nfsiod 
        33  ?  SW     0:00 nfsiod 
        34  ?  SW     0:00 nfsiod 
       143  ?  S      0:08 /sbin/syslogd 
       145  ?  S      0:00 klogd 
       154  ?  S      0:00 /sbin/kerneld 
       164  ?  S      0:00 inetd 
       167  ?  S      0:05 named 
       179  ?  SW     0:00 run_job 
       183  ?  SW     0:00 bootpd 
       205  ?  SW     0:00 gpm 
       209  ?  S      0:52 /usr/sbin/tcplogd 
       212  ?  SW     0:00 icmplogd 
       ... and so on ...

The number in the column headed PID is the identifying number of the process. Under column TT is the tty (screen or window identifier) where it is running. A question mark here means that it is not attached to any screen or window, but is running in the background.

tty

tty shows you which terminal (screen or window) you are in.

     $ tty
     /dev/ttyp4

6.2.4.11 Identity

These commands relate to your identity as a user and the machine's own identity.

passwd

Use passwd to change the password that you use when you log in. Follow the instructions on screen about what kind of password to choose. When you type the password in, it doesn't show on the screen, so you have to enter it twice, to ensure that you make no typing errors.

id

id tells you what user you are and what group you are in.

     $ id
     uid=1000(olly) gid=1000(olly) groups=1000(olly),6(disk),20(dialout),25(floppy),26(tape),29(audio),34(backup),37(operator),101(priory)

uid indicates your user number, gid your group number (which will be assigned to files that you create) and groups lists the other groups that you are a member of.

who -u

who -u reports everyone who is logged on.

uname -a

uname -a reports the name, type and operating system of your machine.

date

date reports the current time and date.

The superuser can use this command to set the date and time.


6.2.4.12 Turning off the computer

Turning the computer on and off is really a system administration subject, but I include it here because it is something that every user who is his own administrator needs to know. If someone else is responsible for administering the machine, you should neither need nor be able to shut it down.

shutdown -t 0 -h now

This command shuts the computer down safely. You can also use ctrl-alt-del if your system is set up for that. (If you are in X, ctrl-alt-del will be intercepted by X. Get out of X first by using ctrl-alt-backspace.) Never turn off a Unix machine without doing a safe shutdown: if you do, you will damage your file-systems.


6.2.5 Special characters in commands

When you type a command, certain characters have a special meaning to the shell and are interpreted before the command starts to run. Full details are given in the chapter on shells, but you need to understand what is happening, or you will get results that you did not expect.

Shell special characters are interpreted by the shell as soon as it is given the command. For example, if you type ls *.bak, the shell translates *.bak to the list of all files in the current directory whose names end in .bak. The ls command never sees the asterisk. So if you want to search for files which actually have an asterisk in their names, you have to escape the asterisk to stop the shell from interpreting it.

     linda:/tmp/junk$ ls **
     fred     george*  joe      mary     peter*
     linda:/tmp/junk$ ls *\*
     george*  peter*
     linda:/tmp/junk$

In the example, the first command matched all files; the second matched only those with a literal asterisk at the end of their names.

The same goes for other special characters. here is the full list:

\

\ escapes itself and other specials

[ ]

[ ] encloses patterns for matching a single character

*

* stands for anything (including nothing)

?

? stands for any single character

( )

( ) runs the contents of the parentheses in a sub-shell

;

; terminates a command pipeline - use it to separate commands on a single line

&

& runs the preceding pipeline in the background.

This is the same as typing Ctrl-Z and then bg to an already running program.

$word and ${word}

$word and ${word} are interpreted as a shell variable (use ${word} if there is danger of ambiguity). There are other characters that can follow $ and give other meanings. (See Shells, Chapter 5.)

``

The contents of the backquotes are run as a command and its output is used as part of this command

""

The contents of the quotes are treated as one argument; any specials inside the quotes, except for $ and ``, are left uninterpreted.

''

The contents of the single quotes are passed to the command without any interpretation.

|

The vertical bar sign is used to indicate a pipe. The command on the left-hand side of the pipe sends its output to the input of the command on the right side of the pipe.

< and >

These symbols are used for redirection; see below.

&&

The double ampersand is used between two commands (on the same line). It tells the shell to run the second command only if the first one was successful[8] .

||

The double vertical bar is used between two commands (on the same line). It tells the shell to run the second command only if the first one was unsuccessful.

For example, ls -d /[uv]* will list the names of all entries in the root directory that start with u or v. Unlike DOS, you can use patterns like g*.gz (with the wildcard bit in the middle).

These characters have a special meaning only if they are the first non-space character in the command:

!

If ! is immediately followed by a character other than =, it begins a history substitution; if it is followed by a space it negates the return code of the following command pipeline. (For more details see Shells, Chapter 5.)

^

Quick history substitution, changing one string to another:

     $ ls *.cc
     SQLBuilder.cc      dbwindow.cc        listitem.cc
     $ ^cc^h^
     ls *.h
     SQLBuilder.h      dbwindow.h        listitem.h
#

# turns the line into a comment; the line is not processed in any way.

Don't confuse shell special characters with special characters in regular expressions (see Regular expressions, Section 6.3). Regular expressions must be protected from the shell by enclosing them in single quotes.


6.2.6 Filters

Many commands are filters, so you can pass the output of one command to the input of another:

     awk -F: '{print $1 " " $6}' /etc/passwd | sort

This is a fundamental concept of all Unix and Unix-like systems. Many commands are designed to be filters. They can therefore be used as building blocks to make more complex commands, in whatever way is needed at the time.


6.2.7 Standard input, standard output and standard error

When the shell starts a command for you, it opens files for standard input, standard output and standard error. By default, standard input is your keyboard, standard output is your screen, and standard error is also your screen.

Although the defaults are keyboard and screen, this can easily be changed. In fact, the shell copies its own standard input, output and error when it starts a command, so the command inherits whatever the shell has. A command can change its own settings, by opening a different file, and you can tell the shell to use a file instead of the default of keyboard or screen.

The file descriptor numbers for standard input, standard output and standard error are 0, 1 and 2 respectively.


6.2.8 Redirection

You can redirect standard input, output and error by adding these sequences after a command:

<

< file means take input from file.

>

> file means send output to file (overwriting and destroying whatever is in the file already).

You can use this feature to empty a file, by typing

     >file_with_unwanted_contents

on a line by itself.

>>

>> file means append output to file.

2>

2> file means send error messages to file (overwriting). (This means that errors can be directed separately from normal output.)

There are more complex aspects to redirection, which may be studied in the shell manual pages.

Here are some very common uses of redirection:

cat [filelist] >output_file

We have already seen that cat can be used to look at files. In conjunction with redirection of standard output, it can be used to write files as well.

If you omit filelist, cat will take its input from the keyboard, until you type ctrl-D.

echo text

echo sends text to the standard output. It is commonly used in script programs to put text on the screen.

It can also be used with redirection of output to write to a file. For example,

     echo "An example file containing
     two lines of text" >/tmp/example

and

     echo "A third line added to the first two" >>/tmp/example

6.2.9 Shell programs

Redirection is a simple part of shell programming, for which see the manual pages of bash, zsh, tcsh, or whatever shell you have chosen. A shell program is a set of commands in a file or typed in.

     for f in `grep -l widgets`
     	do
     		sed -e 's/widgets/gadgets/g' <$f ${f}gadget
     		echo $f
     	done

Once you have stored a shell program in a program file, you have to make it executable by running chmod:

             chmod a+x my_program_file

6.2.10 Errors

Unix commands normally execute silently unless asked to be verbose or unless there is an error. An error means something that does not make sense to Linux; it doesn't mean something you don't want to happen!

For example, I once had a client who had some unwanted files in his root directory which were named &TEMP& and so on. As superuser in the root directory he typed rm -rf /&* when he should have typed rm -rf \&* What he actually asked for was to delete his entire file system, as a background job. It did too.


6.3 Regular expressions

Regular expressions are a way of specifying some text to match without having to spell it out exactly. Regular expressions are used inside editors and in the grep command. They are not the same as the limited file pattern matching that is done by the command line.

A regular expression can be very simple or very complicated. Complicated expressions are built up by joining together simple ones. In this section we are only dealing with simple uses of regular expressions in the grep and zgrep commands. The simplest expressions are called atoms, because they cannot be broken down any further.

Here are the regular expression atoms:

character

Any single character matches itself.

.

A full stop matches any single character.

[ ]

A list of characters in square brackets matches any single character provided that it occurs inside the brackets. A range of characters can be expressed inside the brackets by specifying the two ends of the range separated by a minus sign. For example, the regular expression atom [ACafjl-rx] matches any one occurrence of the letters `A', `C', `a', `f', `j', `l', `m', `n', `o', `p', `q', `r' or `x'.

The effect of this atom can be reversed by making `^' the first character inside the brackets. This means that any character matches except those in the brackets.

^

A circumflex character outside square brackets indicates the beginning of a line. (So it can only have this meaning at the beginning of a regular expression.)

$

A dollar character outside square brackets indicates the end of a line. (So it can only have this meaning at the end of a regular expression.)

\

A backslash escapes the character that follows it; that is, it alters that character's meaning. For example, use \* to signify a real asterisk, or \\ to signify a real backslash.

Some letters have special meanings if they follow a backslash: \t matches a tab character, \f matches a form feed character, and so on.

The regular expression atoms can be affected by these modifiers (+ and ? have to be preceded by a backslash when used with grep):

re*

A regular expression followed by an asterisk is matched by 0 or more occurrences of that regular expression. For example, b* matches `', `b', `bb', `bbbbbb' and so on.

re+

A regular expression followed by a plus sign is matched by 1 or more occurrences of that regular expression. For example, b+ matches `b', `bb', `bbbbbb' and so on.

re?

A regular expression followed by a question mark is matched by 0 or one occurrences of that regular expression. For example, b? matches `' or `b'.

Here is an example of the use of a regular expression with grep:

     grep '^[A-Z][a-z]\+' basic_cmds.sgml

This command would list all lines in basic_cmds.sgml that start with a capital letter immediately followed by one or more lower case letters. The plus sign is escaped with a backslash to make it have its special meaning. The whole regular expression is in single quotes to protect it from being evaluated by the shell.


[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ 14 ] [ 15 ] [ 16 ] [ 17 ] [ 18 ] [ 19 ] [ 20 ] [ next ]


Debian User Reference Manual (Obsolete Documentation)

version 0.1, 29 Dezember 2009

Ardo van Rangelrooij ardo@debian.org
Jason D. Waterman jwaterma@bbn.com
Thalia L. Hooker th@visi.com
Havoc Pennington rhpennin@midway.uchicago.edu
Oliver Elphick - Maintainer Oliver.Elphick@lfix.co.uk
Bruce Evry bruce@fantek.org
Karl-Heinz Zimmer khz@snafu.de
Andreas Franzen anfra@debian.org