Linux Training

Linux training for private, public & voluntary sector.

0800 024 8425

City LinUX Training Courses

Section 6.
Linux commands.


"When in doubt, use brute force."

Ken Thompson.

6. Commands, arguments & options.

UNIX / Linux commands are terse, silent and sometimes cryptic.

Silence is golden!

That is when everything works say nothing, only when it does not work is any commentary required.

As we will see later Linux tools are expected to be linked with other tools. Spurious output would reduce the ease of linkage. Besides, Unix considers the user to be a cognisant adult who tells the shell what to do. If you tell the shell to copy file a to file b, then expect it to be done and have faith that it will be done. You should not need, nor expect, the shell to tell you that it was done.

Terse file names.

If N characters in a filename would describe a tools function, use N-M where M > 0.

When terminals were predominately slow and noisy teletypes any reduction in typing/printing was to be welcomed. Short command names leave less room for the "Fat finger gremlin" to cause tpying porblems.

6.1. Users of command.com.

Users familiar with PC/MS-DOS should note that Unix was one of the major inspirations for DOS, therefore at the conceptual level and at the software tool level there are many parallels. These tools are common to both operating systems: more, set, sort, shift, if, print, cd, mkdir, rmdir, date, echo.

Image imgs/sa101-5.png

6.2. End of input.

Many programs which expect a filename on the invoking command line will, if it is not given, automatically look to the keyboard, for input. To terminate the keyboard input you will need to type ^d, ( ^ indicates that the control key should be pressed and held so ^d means, hold down the control key and press the "d" key once. You don’t need to worry about shifting the d, either upper or lower case "d" will work.) ^d means "end of input" or "done". Whenever a terminal just echoes and does nothing try using ^D just in case some command is in input mode.

6.3. Options and switches.

Most Linux commands accept additional words, flags, switches or options on the command line. Collectively these are called parameters and may be referenced in scripts by their position on the command line as we will see later. The default action of the command can be modified by adding options to the command line. Most (but not all) options are preceded by the minus sign, for example,

sa101$ cat /etc/group
sa101$ cat -n /etc/group

The first command lists the contents of a file, whilst the second precedes each line with a line number, (which can be useful when compilers refer to errors on particular lines). In fact the ls command which simply lists the files in a directory, has in most flavors of Unix/Linux, over forty different options (not counting the alternative "long" options) which may be combined to provide specific details of the files.

E.g. The next command lists files and directories in the current directory in long format, in order of creation time,

$ ls -lt

6.4. Command line arguments.

Command line arguments are often essential for a command to work, for example to locate the string "root" within the UNIX user groups file /etc/group we use grep, or "get regular expression".

sa101$ grep root  /etc/group

The filename and the string are called arguments and can often be expressed in a metanotation common to many Linux/Unix tools, i.e.

sa101$ grep ’^[eat]\{3\}$’ /usr/share/dict/words

This command searches for anagrams of the word "eat". It scans the file containing the words (one per line) that is used by tools that check spellings. The pattern uses the following metanotation :-

Image imgs/sa101-6.png

NB. The pattern is placed in single quotes to stop the shell from interpreting the metanotation as file matching metacharacters. The shell passes the expression to the tool enumerated on the command line, i.e. grep which will then parse the regular expression.

The metanotation "\" is used to "escape" the braces and prevent them from being interpreted as literal characters.

The command doesn’t quite work because it would return "eet", "aat", "taa" etc if they existed. Using a restricted data set and limited testing creates an illusion of success.

6.5. Exercises .

Check that the file /usr/share/dict/words exists using ls.

If the file is missing use the package manager apt-get to install it with the following command.

sa101$ sudo apt-get install wbritish-large

Repeat the commands from the lesson above

Try the same command command using the string "dog".

Investigate the following commands. (By the end of the course you should be reasonably comfortable with reading and interpreting regular expressions.)

sa101$ grep -v ’[aeiou]’  /usr/share/dict/words
sa101$ grep  ’^[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u*$’\
/usr/share/dict/words
sa101$ grep  ’^[^a]*a[^b]*b[^c]*c[^d]*d[^e]*e’  /usr/share/dict/words

On first viewing some of the syntax demonstrated here is likely to be perplexing but because it is used by many different programs, there will come a time when familiarity breeds content! However, until then it will mean every typo that includes some punctuation characters is likely to generate error messages.


The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.