Russian News Network




Home

Back to Lecture Index










UNIX Lecture 5

As it appeared when it was first sent via email.



Hello Everybody,

This is UNIX Lecture 5. If you are new, missed the old lectures, or are behind, you can find the old lectures at the following link:
http://www.russiannewsnetwork.com/unix-lectures.html

Today we will go over some commands.

Once you log into a UNIX system, you see a shell prompt. The shell prompt indicates that the shell is ready to receive the commands that the user inputs and take the appropriate action.

Commands are really a type of program, and they can normally be found in the directory /bin. When you issue a command, the shell calls the programs and executes it.

Most commands can be inputted with "options". Options serve to alter the default behavior of the command and cause the output to be somewhat varied. Options normally follow the command line, and in most cases are indicated by a dash (minus sign, ie, "-") followed by a single letter.

Here are some commands:

The Date command

This command displays the current date and time. You can alter the format of the output by including a proper argument to the date command.

Below is a list of the common field descriptors and their actions:

m month of year
d day of month
y last 2 digits of year
D date in the mm/dd/yy format
H hour in the format 00-23
M minute
S second
T time in the HH:MM:SS format
a abbreviated weekday
h abbreviated month

The who, w, and who am I command

The who command lists the login name, the terminal name and the time of login for each current user.

The w command gives you more info than the who command. This command displays the user's login name, the name of the terminal that the user is on, the time of day the user logged on, idle time, the CPU time used by all the processes, the name and augmenting arguments of the current process and the CPU time used by currently active processes.

"who am i" displays your hostname, terminal name, login time and login name.

The echo command

This is a nice little command and it basically writes its argument on the screen. What does this mean?? Well, if you type: echo Paris, France at your prompt, you will get in a line below that the following: Paris, France

If at your prompt you type: echo U S A
the output will be:
USA
Notice that the spaces between the letters U,S, and A are not present in the output. If you use the echo command as shown above, the echo command will replace multiple spaces with one space.

But, if you use quotes, like this: echo "U S A",
then the spaces between the letters U,S, and A will remain as you typed them.

The ls command

This command will list the files in the current directory, but it will not display file names starting with a period (.).

These are the common options:

-a lists all files including the dot files
-l list in long format
-d list its name only, if the argument is a directory
-t sort by modified time instead of by name (this is pretty useful)

Keep in mind that when the argument to the ls command is a file name, the output will be just the file name. When the argument is a directory, the output will be a listing of all files in that directory.

A little about the cat command.

This command is used to display the contents of a small file, to create a small file, and to concatenate several files.

For example:
If at the prompt you type: cat fileA, the result will be that you display the contents of fileA on your screen.

If at the prompt you type: cat > fileB,
you will be able to text for the file. Hitting control d will terminate this mode (the input mode). Keep in mind that whatever you type using this command will become part of the contents of the file.

If at the prompt you type: cat fileA fileB > fileC, will result in the combination of fileA and fileB and save it as fileC.

Now it's possible to talk about the set nonclobber command.

Let's say that at the prompt you type: cat: fileA > fileB
....what happens?

Assuming that fileB exists, then it is emptied first, and then the contents of fileA are copied into it. The old contents of fileB are permanently deleted.

This is where the set nonclobber comes is...

If at your prompt you type: set -o nonclobber before you proceed with the above cat commands, you will avoid the danger of deleting something that you may not really want to delete.

Now, with nonclobber set, if fileB exists, the command:
cat fileA > fileB
will result in the following message:
ksh: fileB: file already exists
and no copying will be performed.

The following command will overide the nonclobber feature:
at the prompt: cat fileA >| fileB

And to turn off the nonclobber feature, type:
set +o nonclobber at your prompt.

If you are wondering why would someone want to use nonclobber at all, it is best to experiment for yourself to understand why you might need it.

The cd command

The cd command changes to the specified directory. What does this mean?

Well, let's say that you have created a directory called "mydirectory". If at your prompt, you type: cd mydirectory
you will be taken to the "mydirectory" directory, containing whatever files you've placed in this directory. Once in the "mydirectory" directory, you can manipulate and work on the files within this directory (as opposed to some other directory). It can be thought of as simply turning to a new chapter in a book, with each chapter containing certain pages that you might wish to read or edit.

Some examples:

cd /usr/bin
swithces to the directory bin in the specified path.

cd /
switches to the root directory, and the space between the cd and the slash (/) is needed.

cd
will take you to the home directory. No matter where you are, typing cd at your prompt will automatically return you to the home directory.

cd -
will take you back to your previous directory. For example, lets say from your home directory you went to "directory_1" and then you went to "directory_2" and then to "directory_3". After working a bit in "directory_3", you decide that you want to go back to "directory_2". cd - will do this for you in one simple, 3 keystroke step.

The last command, well 2 really, that I would like to talk about are:
the head command and the tail command.

These are really simple commands. The head command will display several lines at the beginning of a file, and the tail command will display several lines at the end of a file.

Here's how you use them:
At the prompt, type:
head fileA
to display the first 10 lines of fileA

or...
at the prompt, type:
tail fileA
to display the last 10 lines of fileA

That will be all for today.

Hoping that we all have a great new year,
Vladimir Polyakov

PS Thanks to my friends at the Russian News Network website who have helped me in this endeavor.
http://www.russiannewsnetwork.com