BIL101, Introduction to Computers
and Information Systems

Chapter 3
Introduction to Unix:Fundemental Commands

        This chapter is devoted to the hours of third week.The fundamental commands of the Unix operating systemare included in the chapter. Everything told for Unix here is ipplicable to the Linux operating system also.


Partially based on the chapters Shell Commands and Commands of the book
entitled The Linux Users' Guide by
Larry Greenfield
Edited and Reformatted by
Metin Demiralp
Istanbul Technical University, Informatics Institute
Maslak -- 80626, Istanbul, TURKIYE
Adopted to HTML by
Umut Can
Version 0.60




Making files is easy under the UNIX operating system. Therefore, users tend to create numerous files using large amounts of file space. It has been said that the only standard thing about all UNIX systems is the message--of--the--day telling users to clean up their files.

 

Unix Commands

When you first log into a unix system, you are presented with something that looks like the following:

tt/home/larry/#

    That ``something'' is called a prompt. As its name would suggest, it is prompting you to enter a command. Every unix command is a sequence of letters, numbers, and characters.There are no spaces, however. Some valid unix commands are mail, cat, and  CMU\_is\_Number-5. Some characters aren't allowed---we'll go into that later. unix is also case-sensitive. This means that  cat and Cat are different commands. (Case sensitivity is a very personal thing. Some operating systems, such as OS/2 or Windows NT are case preserving, but not case sensitive. In practice, unix rarely uses the different cases. It is unusual to have a situation where  cat and Cat are different commands.

    The prompt is displayed by a special program called theshell. Shells accept commands, and run those commands. They can also be programmed in their own language, and programs written in that language are called ``shell scripts''.

    There are two major types of shells in unix: Bourne shells and C shells. Bourne shells are named after their inventor, Steven Bourne. Steven Bourne wrote the original unix shell sh, and most shells since then end in the letters sh to indicate they are extentions on the original idea. There are many implementations of his shell, and all those specific shell programs are called Bourne shells. Another class of shells, C shells (originally implemented by Bill Joy), are also common. Traditionally, Bourne shells have been used for shell scripts and compatibility with the original sh while C shells have been used for interactive use. (C shells have had the advantages of having better interactive features but somewhat harder programming features.)         

    linux comes with a Bourne shell called  bash written by the Free Software Foundation.bash stands for Bourne Again   Shell, one of the many bad puns in unix. It is an ``advanced'' Bourne shell: it contains the standard programming features found in all Bourne shells with many interactive features commonly found in C shells. bash is the default shell to use running linux.

    When you first login, the prompt is displayed by bash, and you are running your first unix program, the bash shell. As long as you are logged in, the bash shell will constantly be running.

A Typical unix Command

 

    The first command to know is  cat. To use it, type cat, and then press enter key:

/home/larry# cat

    If you now have a cursor on a line by itself, you've done the correct thing. There are several variances you could have typed---some would work, some wouldn't.

1. If you misspelled cat, you would have seen

/home/larry# ct

ct: command not found

/home/larry#

    Thus, the shell informs you that it couldn't find a program named "ct"' and gives you another prompt to work with. Remember, unix is case sensitive: CAT is a misspelling.

2. You could have also placed whitespace before the command, like this: ( The " "  indicates that the user typed a space.)

/home/larry" "" "#" "cat

This produces the correct result and runs the cat program.

3. You might also press return on a line by itself. Go right ahead---it does absolutely nothing.I assume you are now in cat. Hopefully, you're wondering what it is doing. No, it is not a game. cat is a useful utility that won't seem useful at first. Type anything and hit return. What you should have seen is:

/home/larry# cat

Help! I'm stuck in a Linux program!

Help! I'm stuck in a Linux program!

    (The slanted text indicates what I typed to cat.) What cat seems to do is echo the text right back at yourself. This is useful at times, but isn't right now. So let's get out of this program and move onto commands that have more obvious benefits. 

     To end many unix commands, type end-of-file character (Hold down the key labeled "Ctrl'' and press "d'', then let go of both.). The end-of-file character is also denoted by EOF for short. Alternatively, it stands for end-of-text, depending on what book you read. I'll refer to it as an end-of-file. It is a control character that tells unix programs that you (or another program) is done entering data. When cat sees you aren't typing anything else, it terminates. For a similar idea, try the program  sort. As its name indicates, it is a sorting program. If you type a couple of lines, then press EOF, it will output those lines in a sorted order. These types of programs are called  filters because they take in text, filter it, and output the text slightly differently. Both cat and  sort} are unusual filters. cat is unusual because it reads in text and performs no changes on it. sort is unusual because it reads in lines and doesn't output anything until after it's seen the EOF character. Many filters run on a line-by-line basis: they will read in a line, perform some computations, and output a different line.

Helping Yourself

The man command displays reference pages for the command man will also display information on a system call, a subroutine, a file format, and more. In the original version of unix it showed the exact same information the printed documentation would. For now, you're probably only interested in getting help on commands.) you specify. For example:

 

/home/larry# man cat

cat(1)                      cat(1)

NAME

     cat - Concatenates or displays files

SYNOPSIS

 cat [-benstuvAET] [--number] [--number-nonblank] [--squeeze-blank] [--show-nonprinting] [--show-ends] [--show-tabs] [--show-all] [--help] [--version] [file...]

DESCRIPTION

    This manual page documents the GNU version of cat ...

There's about one full page of information about cat. Try running man now. Don't expect to understand the manpage given. Manpages usually assume quite a bit of unix knowledge---knowledge that you might not have yet. When you've read the page, there's probably a little black block at the bottom of your screen similar to "--more--'' or "Line 1''. This is the more-prompt, and you'll learn to love it. 

    Instead of just letting the text scroll away, man stops at the end of each page, waiting for you to decide what to do now. If you just want to go on, press Space and you'll advance a page. If you want to exit (quit) the manual page you are reading, just press q key. You'll be back at the shell prompt, and it'll be waiting for you to enter a new command.

    There's also a keyword function in man. For example, say you're interested in any commands that deal with Postscript, the printer control language from Adobe. Type man -k ps or  man -k Postscript, you'll get a listing of all commands, system calls, and other documented parts of unix that have the word "ps" "Postscript'') in their name or short description. This can be very useful when you're looking for a tool to do something, but you don't know it's name---or if it even exists!

Storing Information

    Filters are very useful once you are an experienced user, but theyhave one small problem. How do you store the information? Surely you aren't expected to type everything in each time you are going to use the program! Of course not. unix provides files and directories. 

    A directory is like a folder: it contains pieces of paper, or files. A large folder can even hold other folders---directories can be inside directories. In unix, the collection of directories and files is called the file system. Initially, the file system consists of one directory, called the "root"' directory. Inside this directory, there are more directories, and inside those directories are files and yet more directories. (There may or may not be a limit to how "deep" the file system can go. I've never reached it; one can easily have directories 10 levels deep.) 

    Each file and each directory has a name. It has both a short name, which can be the same as another file or directory somewhere else on the system, and a long name which is unique. A short name for a file could be joe, while it's ``full name'' would be  /home/larry/joe. The full name is usually called the path. The path can be decode into a sequence of directories. For example, here is how /home/larry/joe} is read: 

    The initial slash indicates the root directory. 

    This signifies the directory called  home. It is inside the root directory.

    The second slash corresponds to the directory larry which is inside home. joe is inside larry. A path could refer to either a directory or a filename, so joe could be either. All the items before the short  name must be directories. 

Looking at Directories with ls

    Now that you know that files and directories exist, there must be some way of manipulating them. Indeed there is. The command 

ls is one of the more important ones. It lists files. If you try ls as a command, you'll see:

 

/home/larry# ls

/home/larry#

    That's right, you'll see nothing. unix is intensionally terse: it gives you nothing, not even "no files" if there aren't any files. Thus, the lack of output was ls's way of saying it didn't find any files.

But I one can say that there could be 8000 or more files lying around: where are they? You've run into the concept of a "current" directory. You can see in your prompt  that your current directory is /home/larry, where you don't have any files. If you want a list of files of a more active directory, try the root directory:

 

/home/larry# ls 

bin    etc    install    mnt    root    user    usr    

dev    home    lib    proc    tmp    usr    vmlinux

 

/home/larry# In the above command, "ls" ," /" is a parameter. The first word of the command is the command name, and anything after it is a parameter. Parameters generally modify what the program is acting on---for ls, the parameters say what directory you want a list for. Some commands have special parameters called options or switches. To see this try:

/home/larry# ls -F /

bin    etc/    install/    mnt/    root/    user/    usr/    

dev/    home/    lib/    proc/    tmp/    usr/    vmlinux/

 

/home/larry#.The -F is an option. An option is a special kind of parameter that starts with a dash and modifies how the program runs, but not what the program runs on. For ls, -F is an option that lets you see which ones are directories, which ones are special files, which are programs, and which are normal files. Anything with a slash is a directory. We'll talk more about ls 's features later. It's a surprisingly complex program!

    Now, there are two lessons to be learned here. First, you should learn what ls does. Try a few other directories and see what they contain. Naturally, some will be empty, and some will have many, many files in them. I suggest you try both with and without the -F option. For example,  ls /usr/local looks like:

/home/larry# ls /usr/local

archives    bin    emacs/    etc/    ka9q/    lib/    tcl/

/home/larry#

The second lesson is more general. Many unix commands are like ls . They have options, which are generally one character  after a dash, and they have parameters. Unlike ls, some  commands require certain parameters and/or options. To show what commands generally look like, we'll use the following form:

ls [-aRF]  [directory]

    I'll generally use command templates like that before I introduce any command from now on. The first word is the command ls. Following the command are all the parameters. Optional parameters are contained in brackets "[" and "]". Meta-variables are slanted---they're words that take the place of actual parameters. (For example, above you see directory, which should be replaced by the name of a real directory.) Options are a special case. They're enclosed by brackets, but you can take any one of them without using all of them. For instance, with just the three options given for ls you have eight different ways of running the command: with or without each of the options. (Contrast ls -R with ls -F.)

The Current Directory and cd

pwd

    Using directories would be cumbersome if you had to type the full path each time you wanted to access a directory. Instead, unix shells have a feature called the ``current'' or ``present'' or ``working'' directory. Your setup most likely displays your directory in your prompt: /home/larry. If it doesn't, try the command pwd, for present working directory. (Sometimes the prompt will display the machine name. This is only really useful in a networked environment with lots of different machines.)

mousehouse>pwd

/home/larry

mousehouse>

cd [ directory ]

    As you can see, pwdtells you your current directory. You'll see all the terms in this book: present working directory, current directory, or working directory. I prefer ``current directory'', although at times the other forms will be used for stylistic purposes---a very simple command. Most commands act, by default, on the current directory. For instance, lswithout any parameters displays the contents of the current directory. We can change our current directory using cd. For instance, try:

/home/larry# cd /home

/home# ls -F

larry/     sam/     shutdown/     steve/    user1/ 

/home#

    If you omit the optional parameter  directory, you're returned to your home, or original, directory. Otherwise, cd will change you to the specified directory. For instance:

/home# cd

/home/larry# cd /

/# cd home

/home# cd /usr

/usr# cd local/bin

/usr/local/bin#

As you can see, cd  allows you to give either absolute or relative pathnames. An absolute path starts with / and specifies all the directories before the one you wanted. A relative path is in relation to your current directory. In the above example, when I was in /usr, I made a relative move to local/bin--- is a directory under usr, and bin is a directory under local! (cd home was also a relative directory change.)

    There are two directories used only for relative pathnames: ".'' and "..". The directory ".'' refers to the current directory and ".." is the parent directory. These are "shortcut"directories. They exist in every directory, but don't really fit the ``folder in a folder'' concept. Even the root directory has a parent directory---it's its own parent!

    The file ./chapter-1 would be the file called chapter-1 in the current directory. Occasionally, you need to put the "./" for some commands to work, although this is rare. In most cases, ./chapter-1 and chapter-1will be identical.

    The directory     ".."' is most useful in "backing up"':

/usr/local/bin# cd ..

/usr/local# ls -F

archives/     bin/     emacs@     etc/     ka9q/     lib/     tcl@

/usr/local# ls -F ../src

cweb/     linux/     xmris/

/usr/local# 

    In this example, I changed to the parent directory using  cd .., and I listed the directory /usr/src from /usr/local using ../src. Note that if I was in  /home/larry, typing ls -F ../src wouldn't do me any good! The directory  ~ is an alias for your home directory

/usr/local# ls -F ~/

/usr/local #

    You can see at a glance that there isn't anything in your home directory! ~ will become more useful as we learn more about how to manipulate files.

 

Creating and Removing Directories

 

mkdir directory1  [directory2 ... directoryN]

    Creating your own directories is extremely simple under unix, and can be a useful organizational tool. To create a new directory, use the command mkdir. Of course, mkdir stands for make directory.

    Let's do a small example to see how this works:

/home/larry# ls -F

/home/larry# mkdir report-1993

/home/larry# ls -F

report-1993/

/home/larry# cd report-1993

/home/larry/report-1993#}

    mkdir can take more than one parameter, interpreting each parameter as another directory to create. You can specify either the full pathname or a relative pathname; report-1993 in the above example is a relative pathname.

 

/home/larry/report-1993# mkdir /home/larry/report-1993/chap1 

~/report-1993/chap2

/home/larry/report-1993# ls -F

chap1/         chap2/

/home/larry/report-1993#

 rmdir  directory1 [ directory2 ... directoryN]

 

    The opposite of mkdir is  rmdi (remove directory).  rmdir works exactly like mkdir. An example of rmdir is:

 

/home/larry/report-1993# rmdir chap1 chap3

rmdir: chap3: No such file or directory

/home/larry/report-1993# ls -F

chap2/

/home/larry/report-1993# cd ..

/home/larry# rmdir report-1993

rmdir: report-1993: Directory not empty

/home/larry#}

 

As you can see,  rmdir will refuse to remove a non-existant directory, as well as a directory that has anything in it. (Remember,  report-1993} has a subdirectory,  chap2, in it!) There is one more interesting thing to think about  rmdir: what happens if you try to remove your current directory? Let's find out:

 

/home/larry# cd report-1993

/home/larry/report-1993# ls -F

chap2/

/home/larry/report-1993# rmdir chap2

/home/larry/report-1993# rmdir .

rmdir: .: Operation not permitted

/home/larry/report-1993#

 

Another situation you might want to consider is what happens if you try to remove the parent of your current directory. This turns out not to be a problem since the parent of your current directory isn't empty, so it can't be removed!

Moving Information

    All of these fancy directories are very nice, but they really don't help unless you have some place to store you data. The unix Gods saw this problem, and they fixed it by giving the users files.    

    The primary commands for manipulating files under unix are  cpmv, and  rm. They stand for  copy,  move, and  remove, respectively.

 

cp Like a Monk

         cp  [-i]  source destination

         cp [-i]  file1 file2 ... fileN destination-directory ( cp has two lines in its template because the meaning of the second parameter can be different depending on the number of parameters.)  

     cp is a very useful utility under unix, and extremely powerful. It enables one person to copy more information in a second than a fourteenth century monk could do in a year. Caution!  Be careful with  cp if you don't have a lot of disk space. No one wants to see a ``Disk full'' message when working on important files. cp can also overwrite existing files without warning---I'll talk more about that danger later.

    We'll first talk about the first line in the command template. The first parameter to  cp is the file to copy---the second is where to copy it. You can copy to either a different filename, or a different directory. Let's try some examples:

 

/home/larry# ls -F /etc/passwd

/etc/passwd

/home/larry# cp /etc/passwd .

/home/larry# ls -F

passwd

/home/larry# cp passwd frog

/home/larry# ls -F

frog passwd

/home/larry#

 

    The first  cp command I ran took the file  /etc/passwd, which contains the names of all the users on the unix system and their (encrypted) passwords, and copied it to my home directory.  cp doesn't delete the source file, so I didn't do anything that could harm the system. So two copies of  /etc/passwd exist on my system now, both named passwd, but one is in the directory  /etc and one is in  /home/larry.

    Then I created a third copy of  /etc/passwd when I typed cp passwd frog---the three copies are now:  /etc/passwd, /home/larry/passwd and  /home/larry/frog. The contents of these three files are the same, even if the names aren't.  

    cp can copy files between directories if the first parameter is a file and the second parameter is a directory. In this case, the short name of the file stays the same.

    It can copy a file and change it's name if both parameters are file names. Here is one danger of  cp}. If I typed  cp /etc/passwd /etc/groupcp would normally create a new file with the contents identical to  passwd and name it  group. However, if  /etc/group already existed,  cp would destroy the old file without giving you a chance to save it! (It won't even print out a message reminding you that you're destroying a file by copying over it.)

Let's look at another example of  cp:

 

/home/larry# ls -F

frog passwd

/home/larry# mkdir passwd\_version

/home/larry# cp frog passwd passwd\_version

/home/larry# ls -F

frog         passwd         passwd\_version/

/home/larry# ls -F passwd\_version

frog passwd

/home/larry#

 

    How did I just use  cp? Evidentally,  cp can take more  than two parameters. (This is the second line in the command template.) What the above command did is copied all the files listed ( frog and  passwd) and placed them in the  passwd\_version directory. In fact,  cp can take any number of parameters, and interprets the first n-1 parameters to be files to copy, and the first n-th parameter as what directory to copy them too.

    Caution! You cannot rename files when you copy more than one at a time---they always keep their short name. This leads to an interesting question. What if I type  cp frog passwd toad, where frog and  passwd exist and  toad isn't a directory? Try it and see.

 

    Pruning Back with  rm

 

        rm [-i] file1 file2 ... fileN

 

    Now that we've learned how to create millions of files with  cp (and believe me, you'll find new ways to create more files soon),  it may be useful to learn how to delete them. Actually, it's very simple: the command you're looking for is  rm, and it works just like you'd expect: any file that's a parameter to  rm gets deleted.

    For example:

 

/home/larry# ls -F

frog      passwd       passwd_version/

/home/larry# rm frog toad passwd

rm: toad: No such file or directory

/home/larry# ls -F

passwd_version/

/home/larry#

 

    As you can see,  rm is extremely unfriendly. Not only does it not ask you for confirmation, but it will also delete things even if the whole command line wasn't correct. This could actually be dangerous. Consider the difference between these two commands:

 

/home/larry# ls -F

toad frog/

/home/larry# ls -F frog

toad

/home/larry# rm frog/toad

/home/larry#}

and this

 

/home/larry# rm frog toad

rm: frog is a directory

/home/larry# ls -F

frog/

/home/larry#}

    Caution! As you can see, the difference of one character made a world of difference in the outcome of the command. It is vital that you check your command lines before hitting return!

 

A Forklift Can Be Very Handy

 

     mv [-i] old-namenew-name

     mv [-i]  file1 file2 ... fileN      new-directory

 

Finally, the other file command you should be aware of is  mv.mv looks a lot like  cp, except that it deletes the original file after copying it. It's a lot like using  cp and  rm together. Let's take a look at what we can do:

 

/home/larry# cp /etc/passwd .

/home/larry# ls -F

passwd

/home/larry# mv passwd frog

/home/larry# ls -F

frog

/home/larry# mkdir report

/home/larry# mv frog report

/home/larry# ls -F

report/

/home/larry# ls -F report

frog

/home/larry#

 

    As you can see,  mv will rename a file if the second parameter is a file. If the second parameter is a directory,  mv} will move the file to the new directory, keeping it's shortname the same. 

    Caution! You should be very careful with  mv---it doesn't check to see if the file already exists, and will remove any old file in its way. For instance, if I had a file named  frog already in my directory  report, the command  mv frog report would delete the file ~/report/frog and replace it with  ~/frog.

    In fact, there is one way to make  rm, cp and  mv ask you before deleting files. All three of these commands accept the  -i option, which makes them query the user before removing any file. If you use an alias, you can make the shell do  rm -i automatically when you type  rm.

 

Some Other UNIX Commands

 

The Power of unix

 

    The power of unix is hidden in small commands that don't seem too useful when used alone, but when combined with other commands (either directly or indirectly) produce a system that's much more powerful and flexible than most other operating systems. The commands I'm going to talk about in this chapter include  sort,grep,more,cat,wc,spell,diff,head,and  tail. Unfortunately, it isn't totally intuitive what these names mean right now. 

     Let's cover what each of these utilities do seperately and then I'll give some examples of how to use them together. (Please note that the short summaries on commands in this chapter are not comprehensive. Please consult the command's manpage if you want to know every option. )

 

Operating on Files

 

     In addition to the commands like  cd,mv,and rm you learned in shell section, there are other commands that just operate on files but not the data in them. These include  touch,chmod,du,and df. All of these files don't care what is in the file---the merely change some of the things unix remembers about the file.

     Some of the things these commands manipulate:

1.  The time stamp. Each file has  three dates associated with it. (Older filesystems in linux only  stored one date, since they were derived from Minix. If you have  one of these filesystems, some of the information will merely be  unavailable---operation will be mostly unchanged.) The three dates  are the creation time (when the file was created), the last  modification time (when the file was last changed), and the last  access time (when the file was last read).

2. The owner. Every file in unix is owned by one  user or the other.

3. The group. Every file also has a group of users  it is associated with. The most common group for user files is called  users, which is usually shared by all the user account on the  system.

4. The Permissions.. Every file has permissions  (sometimes called ``privileges'') associated with it which tell unix  who can access what file, or change it, or, in the case of programs,  execute it. Each of these permissions can be toggled seperately for  the owner, the group, and all other users.}

 touch file1 file2 ... fileN

 

     touch will update the time stamps of the files listed on the command line to the current time. If a file doesn't exist,  touch will create it. It is also possible to specify the time that  touch will set files to---consult the the manpage for  touch

chmod 

        [-Rfv]  mode  file1 file2 ... fileN

 

    The command used to change the permissions on a file is called  chmod, short for change mode. Before I go into how to use the command, let's discuss what permissions are in unix. Each file has a group of permissions associated with it. These permissions tell unix whether or not the file can be read from, written to, or executed as a program. (In the next few paragraphs, I'll talk about users doing these things. Any programs a user runs are allowed to do the same things a user is. This can be a security problem if you don't know what a particular program does.) Unix recognizes three different types of people: first, the owner of the file (and the person allowed to use  chmod on that file). Second, the ``group''. The group of most of your files might be ``users'', meaning the normal users of the system. (To find out the group of a particular file, use  ls -l  file.) Then, there's everybody else who isn't the owner and isn't a member of the group, appropriately called ``other''.

    So, a file could have read and write permissions for the owner, read permissions for the group, and no permissions for all others. Or, for some reason, a file could have read/write permissions for the group and others, but no permissions for the owner!

     Let's try using  chmod to change a few permissions. First, create a new file using cat,  emacs, or any other program. By default, you'll be able to read and write this file. (The permissions given other people will vary depending on how the system and your account is setup.) Make sure you can read the file using  cat. Now, let's take away your read privilege by using  chmod u-r  filename. (The parameter  u-r} decodes to ``user minus read''.) Now if you try to read the file, you get a  Permission denied error! Add read privileges back by using  chmod u+r filename.

    Directory permissions use the same three ideas: read, write, and execute, but act slightly differently. The read privilege allows the user (or group or others) to read the directory---list the names of the files. The write permission allows the user (or group or others) to add or remove files. The execute permission allows the user to access files in the directory or any subdirectories. (If a user doesn't have execute permissions for a directory, they can't even  cd to it!) 

    To use  chmod, replace the mode with what to operate on, either user, group, other, or  all, and what to do with them. (That is, use a plus sign to indicate adding a privilege or a minus sign to indicate taking one away. Or, an equals sign will specify the exact permissions.) The possible permissions to add are read, write, and execute.

 chmod's  R flag will change a directory's permissions, and all files in that directory, and all subdirecties, all the way down the line. (The `R' stands for recursive.) The  f  flag forces  chmod to attempt to change permissions, even if the user isn't the owner of the file. (If  chmod} is given the f flag, it won't print an error message when it fails to change a file's permissions.) The v flag makes  chmod verbose---it will report on what it's done.

 

System Statistics

Commands in this section will display statistics about the operating system, or a part of the operating system.

du [-abs] [path1 path2 ... pathN]

     du stands for disk  usage. It will count the amount of disk space a given directory and all its subdirectories take up on the disk.  du by itself will return a list of how much space every subdirectory of the current directory consumes, and, at the very bottom, how much space the current directory (plus all the previously counted subdirectories) use. If you give it a parameter or two, it will count the amount of space used by those files or directories instead of the current one.

     The  a flag will display a count for files, as well as directories. An option of  b will display, instead of kilobytes (1024 characters), the total in bytes. One byte is the equivalent of one letter in a text document. And the  s flag will just display the directories mentioned on the command-line and not their subdirectories.

 

df

 

     df is short for ``disk filling'': it summarizes the amount of disk space in use. For each filesystem (remember, different filesystems are either on different drives or partitions) it shows the total amount of disk space, the amount used, the amount available, and the total capacity of the filesystem that's used. One odd thing you might encounter is that it's possible for the capacity to go over 100%, or the used plus the available not to equal the total. This is because unix reserves some space on each filesystem for  root. That way if a user accidentally fills the disk, the system will still have a little room to keep on operating. For most people,  df doesn't have any useful options.

 

uptime

 

    The  uptime program does exactly what one would suspect. It prints the amount of time the system has been ``up''---the amount of time from the last unix boot.  

    uptime  also gives the current time and the load average. The load average is the average number of jobs waiting to run in a certain time period.  uptime displays the load average for the last minute, five minutes, and ten minutes. A load average near zero indicates the system has been relatively idle. A load average near one indicates that the system has been almost fully utilized but nowhere near overtaxed. High load averages are the result of several programs being run simultaneously. 

    Amazingly,  uptime is one of the few unix programs that have no options!

 

who

 

who displays the current users of the system and when they logged in. If given the parameters  am i (as in:  who am i), it displays the current user.

 

 w [-f] [username]

 

    The  w program displays the current users of the system and what they're doing. (It basically combines the functionality of  uptime and  who. The header of w is exactly the same as uptime, and each line shows a user, when the logged on (and how long they've been idle).  JCP is the total amount of CPU time used by that user, while  PCPU the the total amount of CPU time used by their present task.

    If  w is given the option f, it shows the remote system they logged in from, if any. The optional parameter restricts  w to showing only the named user.

 

What's in the File?

There are two major commands used in unix for listing files,  cat and  more. I've talked about both of them in shell related sections.

 

cat [-nA] [file1 file2 ... fileN]

 

   cat is not a user friendly command---it doesn't wait for you to read the file, and is mostly used in conjuction with pipes. However, cat does have some useful command-line options. For instance,  n will number all the lines in the file, and  A will show control characters as normal characters instead of (possibly) doing strange things to your screen. (Remember, to see some of the stranger and perhaps ``less useful'' options, use the  man command:  man cat.) cat will accept input from stdin if no files are specified on the command-line.

 

more [-l] [+linenumber}] [file1 file2 ... fileN] 

     more is much more useful, and is the command that you'll want to use when browsing ASCII text files. The only interesting option is  l, which will tell  more that you aren't interested in treating the character  Ctrl-L} as a ``new page'' character.  more will start on a specified linenumber.

    Since  more is an interactive command, I've summarized the major interactive commands below:

Spacebar Moves to the next screen of text.

             d This will scroll the screen by 11 lines, or about half a normal, 25-line, screen}

             / Searches for a regular expression. While a regular expression can be quite complicated, you can just type in a text string to search for. For  example, the entering of  /toad  would search for the next occurence of ``toad'' in your current file. A slash followed by a return will search for the next occurence of what you last searched for.

             n This will also search for the next occurence of your regular expression.

             :If you specified more than one file on the command line, this will move to the next file.

             : This will move the the previous file

            q Exits from  more.

 

head [- lines}] [l file1 file2 ... fileN]

 

     head will display the first ten lines in the listed files, or the first ten lines of stdin if no files are specified on the command line. Any numeric option will be taken as the number of lines to print, so  head -15 frog will print the first fifteen lines of the file  frog.

 

tail [-lines] [l file1 file2 ... fileN]

 

    Like  headtail will display only a fraction of the file. Naturally,  tail will display the end of the file, or the last ten lines that come through stdin. tail also accepts a option specifying the number of lines.

 

file [file1 file2 ... fileN]

 

    The  file command attempts to identify what format a particular file is written in. Since not all files have extentions or other easy to identify marks, the  file command performs some rudimentary checks to try and figure out exactly what it contains. Be careful, though, because it is quite possible for   file to make a wrong identification.

 

Information Commands

 

    This section discusses the commands that will alter a file, perform a certain operation on the file, or display statistics on the file.

 

 grep [-nvwx] [-number] { expression} [file1 file2 ... fileN]

 

    One of the most useful commands in unix is  grep, the generalized regular expression  p}arser. This is a fancy name for a utility which can only search a text file. The easiest way to use  grep is like this:

 /home/larry# cat animals

Animals are very interesting creatures. One of my favorite animals is the tiger, a fearsome beast with large teeth. 

I also like the lion---it's really neat!

/home/larry# grep iger animals

the tiger, a fearsome beast with large teeth.

/home/larry#

    One disadvantage of this is, although it shows you all the lines containing your word, it doesn't tell you where to look in the file---no line number. Depending on what you're doing, this might be fine. For instance, if you're looking for errors from a programs output, you might try  a.out | grep error, where  a.out is your program's name.

    If you're interested in where the match(es) are, use the  n switch to  grep to tell it to print line numbers. Use the  v switch if you want to see all the lines that don't match the specified expression. 

    Another feature of  grep is that it matches only parts of a word, like my example above where  iger matched  tiger. To tell  grep  to only match whole words, use the  w, and the  x switch will tell grep to only match whole lines.

    If you don't specify any files,  grep will examine stdin.

 

wc [-clw] [file1 file2 ... fileN]

 

     wc stands for word count. It simply counts the number of words, lines, and characters in the file(s). If there aren't any files specified on the command line, it operates on stdin.

     The three parameters,  clw, stand for character,  line, and word respectively, and tell  wc which of the three to count. Thus,  wc -cw will count the number of characters and words, but not the number of lines.  wc defaults to counting everything---words, lines, and characters.

One nice use of  wc is to find how many files are in the present directory:  ls | wc -w. If you wanted to see how many files that ended with  .c} there are, try  ls *.c | wc -w.

 

 spell [file1 file2 ... fileN]

 

     spell is a very simple unix spelling program, usually for American English. (While there are versions of this for several other European languages, the copy on your linux machine is most likely for American English.)  spell is a filter, like most of the other programs we've talked about, which sucks in an ASCII text file and outputs all the words it considers misspellings.  spelloperates on the files listed in the command line, or, if there weren't any there, stdin. 

    A more sophisticated spelling program,  ispell is probably also available on your machine.  ispell will offer possible correct spellings and a fancy menu interface if a filename is specified on the command line or will run as a filter-like program if no files are specified.

While operation of  ispell should be fairly obvious, consult the man page if you need more help.

 

cmp file1 [ file2]

 

    cmp compares two files. The first must be listed on the command line, while the second is either listed as the second parameter or is read in from standard input.  cmp is very simple, and merely tells you where the two files first differ.

 

diff file1 file2

 

One of the most complicated standard unix commands is called  diff. The GNU version of  diff has over twenty command line options! It is a much more powerful version of  cmp and shows you what the differences are instead of merely telling you where the first one is.

    Since talking about even a good portion of  diff is beyond the scope of this book, I'll just talk about the basic operation of  diffIn short, difftakes two parameters and displays the differences between them on a line-by-line basis. For instance:

 

/home/larry# cat frog

Animals are very interesting creatures. One of my favorite animals is the tiger, a fearsome beast with large teeth.

I also like the lion---it's really neat!

/home/larry# cp frog toad

/home/larry# diff frog toad

/home/larry# cat dog

Animals are very interesting creatures. One of my favorite animals is the tiger, a fearsome beast with large teeth.

I also like the lion---it's really neat!

/home/larry# diff frog dog

1c1,2

< Animals are very interesting creatures. One of my favorite animals is

---

> Animals are very interesting creatures. One of my favorite animals is

3c4

< I also like the lion---it's really neat!

--- > I also like the lion---it's really neat!

/home/larry#}

    As you can see,  diff outputs nothing when the two files are identical. Then, when I compared two different files, it had a section header,  1c1,2  saying it was comparing line 1 of the left file,  frog, to lines 1--2 of  dog and what differences it noticed. Then it compared line 3 of frog to line 4 of  dog. While it may seem strange at first to compare different line numbers, it is much more efficent then listing out every single line if there is an extra return early in one file.

 

gzip [-v#] [file1 file2 ... fileN]

gunzip [-v] [file1 file2 ... fileN]

zcat [{file1 file2 ... fileN]

 

These three programs are used to compress and decompress data.  gzip, or GNU Zip, is the program that reads in the original file(s) and outputs files that are smaller.  gzip deletes the files specified on the command line and replaces them with files that have an identical name except that they have `` .gz}'' appended to them.

 

tr string1  string2

 

    The ``translate characters'' command operates on standard input---it doesn't accept a filename as a parameter. Instead, it's two parameters are arbitrary strings. It replaces all occurences of string1 in the input with string2 . In addition to relatively simple commands such as  tr frog toadtr can accept more complicated commands. For instance, here's a quick way of converting lowercase characters into uppercase ones:

 

/home/larry# tr [:lower:] [:upper:]

this is a WEIRD sentence.

THIS IS A WEIRD SENTENCE.

         tr  is fairly complex and usually used in small shell programs.

 

 Editors

 

There are a lot of available editors under linux operating system.  Amongst these vi is the most common one. One can claim that every unix system has vi. The other famous editor is emacs  which has some artificial intelligence properties. The mailing  facility pine uses the pico editor. However, perhaps  the simplest one of the editors is  joe. It has a lot of  flexible features of emacs and pico beside the user friendiness  of the turbo type of editors. Hence we give here only the joe  editor. We insert the manual pages of joe from suse linux 6.3  after some rapid editing. 

 

Copyright Notice

In the following section we give the copyright file content of  Larry Greenfield's freeware book since it is a requirement  for making some transfers from that resource. 

 

 UNIX is a trademark of X/Open

    MS-DOS and Microsoft Windows are trademarks of Microsoft Corporation

    OS/2 and Operating System/2 are trademarks of IBM 

    X Window System is a trademark of X Consortium, Inc.

    Motif is a trademark of the Open Software Foundation

    Linux is not a trademark, and has no connection to UNIX, Unix System Labratories, or to X/Open.

    Please bring all unacknowledged trademarks to the attention of the author.

Copyright 1996 Larry Greenfield

    427 Harrison Avenue

    Highland Park, NJ

    08904

     leg+@andrew.cmu.edu

 

Permission is granted to make and distribute verbatim copes of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the sections that reprint ``The GNU General Public License'', ``The GNU Library General Public License'', and other clearly marked sections held under seperate copyright are reproduced under the conditions given within them, and provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

 

Permission is granted to copy and distribute translations of this manual into another language under the conditions for modified versions. ``The GNU General Public License'' and ``The GNU Library General Public License'' may be included in a translation approved by the Free Software Foundation instead of in the original English.

At your option, you may distribute verbatim and modified versions of this document under the terms of the GNU General Public License, excepting the clearly marked sections held under seperate copyright. 

    Exceptions to these rules may be granted for various purposes: Write to Larry Greenfield at the above address or email  leg+@andrew.cmu.edu, and ask. It is requested (but no required) that you notify the author whenever commercially or large-scale printing this document. Royalties and donations are accepted and will encourage further editions.