[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

Other Useful Unix Commands

Contents Previous Topic Next Topic

This topic covers four useful commands:

grep and Regular Expressions

The most useful Unix command is grep. The name comes from the expression global/regular expression/print. This allows you to search (and print) all lines within a file which contain a particular string, or more correctly, a regular expression.

The statement

{ecelinux:1} grep int SingleList.h

will print all lines in SingleList.h which contains the three adjacent characters int. If you wanted to find all lines which contain int preceded by a space, you could use:

{ecelinux:1} grep " int" SingleList.h

If you are looking for simple names made of alpha-numeric characters, using quotes will be sufficient. If, however, you wish to perform a more powerful search, you can also replace the first argument to grep with a regular expression, that is, a description of a general class of strings (just like, with a shell, *.c refers to all files which end in .c, however, regular expressions are much more general than file wild-cards.

Computer engineering students and electrical engineers taking the Computer Engineering Option should familiarize themselves with the next section of regular expressions to help prepare you for ECE 251.

Regular Expressions

We an use much more expressive methods to describe what we are searching for. For instances:

As I find time, I will try to improve this section, however, this gives you a very basic flavour as to the purpose of regular expressions.

To be completed.

Options to grep

The most useful option is -l (ell), which lists only the names of the files which contain at least one instance of the regular expression. A full list of options is shown in Table 1.

Table 1. Options for grep.

-cprint the count of matching lines
-iignore differences between upper- and lower-case characters
-lprint only the file names of those files which have at least one match
-nprint the lines number as well as the line containing the match
-vprint those lines not matching the regular expression

sort

The sort command sorts the lines of a file based on some criterion. By default, it sorts the lines using ASCII lexicographical ordering, that is it prints those lines first, the first character of which has the smallest ASCII value.

Table 2. Options for sort.

-fignore the differences between lower- and upper-case characters
-nsort numerically
-rreverse the order of the sort
+Nbased on a space separated file, sort the Nth column.

zip

The zip command creates a zip archive file (.zip) from the files listed. The first argument is the name of the zip file, and all other files are then added to that archive. For example, suppose your Student ID Number is 12345678, then for Project 1, you could create the hand-in file by executing

{ecelinux:1} zip 12345678p1.zip *.h *.c
{ecelinux:2} 

assuming, of course, that the only .h and .c files in the current directory are those which are to be submitted.

who and whoami

The who command lists all the current users who are logged onto the same machine, while whoami returns the user ID of the current account.

{ecelinux:1} who
n2mohan  pts/4        Aug  2 12:27 (lion.uwaterloo.ca)
kziaei   pts/5        Aug  2 09:37 (congo.uwaterloo.ca)
root     pts/6        Jul 24 11:33 (eceserv.uwaterloo.ca)
cb3chan  pts/9        Jul 31 13:39 (gambier.uwaterloo.ca)
mghembru pts/33       Aug  1 12:42 (smtp.athenadesign.com)
broehl   pts/34       Aug  1 18:49 (cpe0006259bccc9-cm0014045ab4b0.cpe.net.cable.rogers.com)
ece427   pts/35       Jul 26 14:46 (chaco.uwaterloo.ca)
ssingh   pts/3        Jul 30 18:34 (area51.uwaterloo.ca)
ssingh   pts/65       Aug  1 18:54 (area51.uwaterloo.ca)
ece250   pts/66       Aug  2 12:11 (cheetah.vlsi.uwaterloo.ca)
cb3chan  pts/67       Jul 25 04:24 (gambier.uwaterloo.ca)
cb3chan  pts/68       Jul 25 04:14 (gambier.uwaterloo.ca)
{ecelinux:2} whoami
ece250
{ecelinux:3}

which

Most commands are associated with an executable file located somewhere on the system. To find out where that executable file is, you can use the which command:

{ecelinux:1} which ls
/usr/local/bin/ls
{ecelinux:2} which perl
/usr/local/bin/perl
{ecelinux:3} which maple
/software/.admin/bins/bin/maple
{ecelinux:4} which matlab
/software/.admin/bins/bin/matlab
{ecelinux:5} which who
/usr/local/bin/who
{ecelinux:6} which exit
exit: shell built-in command.
{ecelinux:7} which which
which: shell built-in command.
{ecelinux:8} which whatever
whatever: Command not found.
{ecelinux:9}
Contents Previous Topic Next Topic

Copyright ©2005-2012 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]