Home Previous Next

CSC220 :: Lecture Note :: Week 11
Assignments | Handouts | Resources | Email Thurman {Twitter::@compufoo Facebook::CSzero}
{GDT::Bits:: Time  |  Weather  |  Populations  |  Special Dates}

Overview

Assignment(s):

Code: [from last week] wc-lc.c | [classes] friend.cpp | mkpasswd.cpp


The Unix Philosophy

Here is the Unix Philosophy in a nutshell.

GDT::Bit:: The Unix Philosophy

{TopOfPage} {Resources}


Unix User Accounts

In order to access a Unix system, you must be given an account by a system administrator (SysAdmin).

A Unix account consists of seven pieces of information. Access to a Unix system requires you to know the following two data items: your account name (LOGNAME) and your password.

LOGNAME

A LOGNAME must be at least two characters long, and if it is longer than eight, only the first eight characters are relevant. [Note: some Unix systems do not allow a LOGNAME to exceed eight characters.]

Each user of the system must have an unique LOGNAME.

A LOGNAME must start with a lowercase letter and it can contain any combination of lowercase letters and numbers.

A common LOGNAME naming convention is to use the first character of the user's first name followed by the first seven characters of their last name.

Password

Passwords are used to keep other users from accessing your account.

It is important that you select a good password.

You should never share your password with anybody.

CSC294::Bits::More About Unix LOGNAMEs

Group

The system administrator establishes a collection of groups to which users are assigned. Every user belongs to at least one group which is called your primary group. Users can be assigned to multiple groups.

User Identifier

Internally, each user is assigned a user identifier or uid. In most cases, users have unique uids but that is not required. You can usually use a Unix system without needing to know your uid.

Group Identifier

Internally, each group has a unique group identifier or gid assigned to it. When a user account is created, the user's gid is specified.

General Information

Each user account has a field allocated that is used to store general information about the user (e.g. the user's full name, extension, office number, and so forth).

HOME Directory

When a user account is established, a HOME directory is created. This becomes a user's current working directory when they successfully log onto the system.

Startup Program

This field allows the system administrator to instruct the system as to what to do when a user successfully logs in. In many cases, this instruction is to for the system to execute (i.e. run) a shell program (i.e. a command-line interpreter).

GDT::Bit:: The Unix Login Process

{TopOfPage} {Resources}


Logging Into a Unix System

After you connect to a Unix system, you are presented with a Login: prompt. [Note: the login prompt can vary from one system to another.] Upon receiving the login prompt, make sure your 'caps lock' key is 'off' and enter your LOGNAME and press ENTER (or RETURN). Next, you see a Password: prompt. At this time you should enter your password. Do not start typing your password until you have seen the prompt. When you type your password, echo is turned off (i.e. you do not see the characters that you type). This is done for security reasons. Press ENTER after the last character of your password.

If you enter the correct combination of LOGNAME and password, you will be awarded access to the Unix system; otherwise, the Login: prompt re-appears and you have to repeat the entire login procedure.

Upon successful login, you may see numerous messages. These messages might include the following.

After system messages have been displayed, the Unix system command prompt is displayed.

{TopOfPage} {Resources}


Frequently Used Commands (Part 0)

The following is a table of some frequently used commands.

UNIXhelp:: Using UNIX Commands
clear to clear your screen [clear]
man print a manual page about a command [man who; man date]
exit exit the shell program (logout) [exit]
who display who is logged in [or the w command]
who am i display information about yourself
date print current date and time [date]
cal print calendars [cal; cal 1999; cal 2 2000; cal 9 1752]
id prints user user and group identifiers [id]
passwd change your password [passwd]
echo print command-line arguments to standard out
write write a message to a logged in user [write LOGNAME]
wall write a message to all logged in users [wall]
mail to read mail [mail LOGNAME]

{TopOfPage} {Resources}


Unix Manual Pages

Unix commands are documented via a collection of manual pages (i.e. manpages).

A manpage is typically split into the following sections: NAME, SYNOPSIS, DESCRIPTION, FILES, SEE ALSO. Other sections may include: ENVIRONMENT, AUTHOR, BUGS, CAVEATS, HISTORY.

The SYNOPSIS presents the options and/or arguments, if any, that the command supports.

The DESCRIPTION details what the command does and provides help with respect to the effect of options and how arguments are used.

The FILES section, if present, usually lists the file(s) that the command uses.

The SEE ALSO section lists related commands and files.

Manpages are grouped into sections. Some of the more commonly used sections are:

Section#DescriptionExample
1 User commands man -s 1 man
1M System administration commands man -s 1M useradd
2 System calls (used by programmers) man -s 2 read
3 Library functions (used by programmers) man -s 3 fgets
4 File formats man -s 4 passwd
5 Headers, tables, and macros man -s 5 ascii
6 Games man -s 6 hangman
7 Special files man -s 7 intro
8 Networking commands/files man -s 8 intro
l Local commands man -s l thurmunit

By default, the output of the man command is filtered through the more program to control pagination.

{TopOfPage} {Resources}


The id Command

Each user belongs to a group. The group assignment is made by the SysAdmin when a user account is created.

Each group has a name and an internal identifier called a gid. A user can belong to multiple groups.

A user's group assignment(s) determines the files and directories that a user can and cannot access.

Each user in addition to having a LOGNAME also has an internal user id called a uid. Each user usually has a unique uid.

A user can find their respective uid and gid by using the id command.

   $ id
   uid=9705(jdoe) gid=100 groups=student, csc

      LOGNAME is  jdoe  and he has a  uid  of 9705.  
      His  gid  is 100 and he belongs to the groups 
      student  and  csc.  Group  student  has  gid
      of 100, and that is  jdoe's  primary group.
      csc  is a secondary group.

{TopOfPage} {Resources}


The passwd Command

The passwd command is used to change the password on your Unix account.

   $ passwd
   Old password:
   New password:
   Re-enter new password:

      The command prompts you to enter your existing (i.e. old)
      password.  If entered correctly, then it prompts you to
      enter your new password twice.  In all cases, echo is
      turned-off when responding to the command prompts.

The super-user (i.e. root account) can change the password for any user. This is accomplished using the passwd command with a LOGNAME argument.

   # passwd fnstyne
   New password:
   Re-enter new password:

      The super-user does not need to enter an old password.

{TopOfPage} {Resources}


The cal Command

The cal command displays a displays a simple calendar in traditional format and cal offers an alternative layout, more options and the date of easter. The new format is a little cramped but it makes a year fit on a 25x80 terminal. If arguments are not specified, the current month is displayed.

A single argument specifies the year (1 - 9999) to be displayed; note the year must be fully specified: cal 57 displays a calendar for 1957. Two parameters denote the month (1 - 12) and year. A year starts on Jan 1.

{TopOfPage} {Resources}


The write Command

The write command is used to write a message to another logged-in user.

Only super-user can write to users who have chosen to deny messages, or are using a program which automatically denies messages.

To deny messages:

   $ mesg n

To allow messages:

   $ mesg y

Example:

   $ write LOGNAME

   How about lunch?

   CTRL-D
   $ 

{TopOfPage} {Resources}


The wall Command

The wall (Write ALL) command is used to write a message to all logged in users.

Only super-user can write on the terminals of users who have chosen to deny messages or are using a program which automatically denies messages.

To deny messages:

   $ mesg n

To allow messages:

   $ mesg y

Example:

   $ wall

   System is going down in 5 minutes -- log off now.

   <CTRL-D>
   $ 

{TopOfPage} {Resources}


The mail Command

Unix provides access to email via the mail command. [Other Unix mail commands includes Mail and mailx]

You can read your email by typing the mail command without any arguments. When you do, the contents of your in-box are listed. If your in-box is not empty, but no messages display, then execute mail -f

$ mail -f
Mail version 8.1 6/6/93.  Type ? for help.
"/users/n-z/t/th/thurmunit/mbox": 8 messages
>   1 MAILER-DAEMON@user1.  Tue Sep  5 11:52  13/547   "DON'T DELETE THIS MES"
    2 schneier@counterpane  Mon Aug 14 19:00 969/44378 "CRYPTO-GRAM, August 1"
    3 ed@yourdon.com        Thu Aug 17 18:44 114/5668  "Introducing 'The YOUR"
    4 steve.boettcher@sccm  Fri Aug 18 14:39  43/1860  "survey"
    5 ubiquity@HQ.ACM.ORG   Tue Aug 22 10:36  88/3122  "New Issue Alert!"
    6 mapquest@list.mapque  Tue Aug 22 16:45 237/7463  "MapQuest Newsletter"
    7 ed@yourdon.com        Tue Aug 22 21:00  76/3510  "TYR: Vol 01, No. 13 -"
    8 tipuniverse@zdemail.  Tue Aug 22 21:27 100/3990  "Tip Universe: This We"
    & _

When the contents of your mail box are displayed, the mail command issues an & prompt and waits for you to enter a mail command. Some popular commands are:

   ?     Display menu of mail commands.
   #     Show message number.
   n     Display next message.
   p     Display current message.
   d     Delete message
   r     Reply to sender of current message.
   R     Reply to sender and other recipients of current message.
   s file  Save message to the named file.
   q     Quit the mail program.
   x     Exit the mail program, ignoring any changes.

You can send email, execute the mail command using email addresses as arguments.

   $ mail somebody@somewhere ...

   The command prompts for a subject line; type it in followed
   by pressing the ENTER key.  Type in your message.  When you
   are all done entering your message, type a <ctrl-d> 
   (i.e. press the dee key while holding down the CTRL key).
   The command may then prompt for carbon-copy addresses
   (press ENTER if not applicable).

Here is an example for sending mail.

   $ mail foo@bar.com bar@foo.net
   Subject: How About Lunch?

   I'm going to Harlow's Cafe for lunch at 12:00pm.
   Do you want to join me?

   <ctrl-d>
   Cc: <ENTER>

You can avoid the Subject by using the -s option. The -s option requires an argument: the text for the email subject line enclosed (typically) in double quotes.

   $ mail -s "How About Lunch?" x@y z@w
   How about lunch today?
   <ctrl-d>
   Cc: <ENTER>

{TopOfPage} {Resources}


Unix Command-Line

You interact with the Unix system by entering a command at the shell prompt.

Upon successful login, a shell program is started. The shell is a command-line interperted that does the following.

It issues a prompt (by default a $ dollar sign) and waits for you to enter a command. You enter a command by typing is the command name followed by pressing the ENTER key. After the command is entered, the shell finds the command's program file, reads that file into memory, and signals the CPU to run the program. After the program ends, the shell re-issues the prompt and waits for you to enter another command.

There are a variety of shells (Bourne (sh), Born-Again (bash), Korn ksh, C csh and so on).

Most Unix commands are executed by specifying two pieces of information: the command's name, and a collection of zero or more arguments.

Generally, command-line arguments are divided into three types.

Options usually begin with a dash - or slash / character and in many cases are single characters. Example.

   -h   display help on using the command
   -v   execute program in "verbose-mode"
   -D   debug mode
   -i   interactive mode
   -f   force it:  don't issue confirmation prompts

Generally, command-line syntax is as follows.

   command-name [options] [arguments]

      Brackets [] when used to describe the syntax of a command
      usually implies "optional."

By default, a command should execute its most commonly used function.

If you need help on the syntax of a command, then try using the man command. The man command displays a manual page for the specified command. [ Online Manpages]

To Be Completed: Long Option Names
To Be Completed: Learning About Reading Manpages

{TopOfPage} {Resources}


Introduction to the File System

The Unix file system is a logical method for organizing and storing large amounts of information in a way which makes it easy to manage. The file is the smallest unit in which information is stored.

Conceptually, a computer file is similar to a paper document. Technically, a file is a sequence of bytes that is stored somewhere on a storage device. [A byte is the smallest address-able unit. A byte is typically made up of eight bits. A bit is a binary digit (0 or 1).]

Every file has a name called its filename. Users access files using filenames.

A filename can be almost any sequence of characters; however, certain characters should be avoided:

	! # & @ $ ^ ( ) ; | < > TAB SPACE BACKSPACE \ ? * { } [ ] ' "

You should take care when naming a file: use meaningful, easy to remember filenames.

Adopt and follow a consistent file naming convention.

Remember that Unix is case sensitive. Use uppercase and lowercase letters to help make good filenames.

Many filenames use extensions to help indicate the type of file. For example, the file foo.c contains C source code, the file foo.html contains HTML code, and the file foo.au is an audio file. Extensions are not required.

On early Unix systems, filenames were limited to 14 characters in length; however, that restriction is no longer enforced. Maximum filename length is longer than what you want to use.

Over the course of time, you end up creating lots of files. A directory is a special type of file that can be used to group files together. [A directory is similar to a folder.]

Directories provide a way to categorize information.

A directory can contain other directories (often called sub-directories or child directories).

A directory is a file; therefore, it must have a name.

As with naming files, care should be taken when naming directories.

Because directories can contain other directories, which can in turn contain other directories, the Unix file system is called a hierarchical file system or tree-structured file system.

A collection of directories and files creates a directory structure.

The top directory of a the Unix file system is called root and is represented by the name / (slash).

UnixHelp.ed.ac.uk:: File System
PathName.com:: Filesystem Hierarchy Standard
To Be Completed in Unix II: ext2, ext3, rfs, nfs, tmpfs

{TopOfPage} {Resources}


Common Directory Structure

The standard system directories are shown below. Each one contains specific types of file. The details can vary between different Unix systems, but these directories should be common to many.

                                 / (root)
                                    |
    ----------------------------------------------------------------
    |         |       |       |        |       |       |           |
   bin       dev     etc     usr      tmp     lib     home     kernel file

On the 3.5 OpenBSD system, the following directory structure exists.

   /
   /home
   /usr
   /var
   /altroot
   /bin
   /dev
   /etc
   /mnt
   /root
   /sbin
   /stand
   /tmp

The root directory (/) has two executable files: boot and bsd.

{TopOfPage} {Resources}


Frequently Used File and Directory Commands

The following is a table of frequently used file and directory commands.

pwd print working directory [pwd]
cd change workding directory [cd; cd /tmp]
ls list the contents of a directory [ls -l; ls -al]
cat display the contents of a file [cat /etc/group]
more display the contents of a file [cat /etc/group]
head display the top of a file [head /etc/group]
tail display the bottom of a file [tail /etc/group]
mkdir create a directory [mkdir Personal]
rmdir remove a directory [rmdir Junkdir]
touch change the dates and times on a file [touch foo]
cp make a copy of a file [cp a b; cp a /tmp; cp a /tmp/b]
rm remove a file [rm a; rm /tmp/b]
mv move (or rename) a file [mv a b; mv a /tmp; mv a /tmp/b]
ln link files together [ln a b; ln -s a b]
wc word count [wc fname; wc -l fname; wc -cl fname]
file determine file content [file /bin/who]

UNIXhelp::Files

{TopOfPage} {Resources}


The pwd Command

When you log in, your HOME becomes your current working directory.

You move around the directory structure (i.e. change your working directory) by using the cd command.

The pwd (Print or Present Working Directory) command prints the absolute path of the current working directory.

{TopOfPage} {Resources}


The cd Command

The cd command is used to change your current working directory.

When executed with no argument, the command switches your current working directory to your HOME directory; otherwise, your current working directory is switched to the directory specified on the command-line.

   $ cd /tmp       # change working directory to /tmp
   $ cd /usr/bin   # change working directory to /usr/bin
   $ cd /temp      # invalid directory specified
   cd: /temp: No such file or directory
   $ cd            # change working directory to your HOME directory
   $ cd -          # return to previous current working directory
   $ cd ..         # change working directory to parent directory
   $ cd ../tmp     # change working directory to a directory that
                   # is named  tmp  that is a sub-directory of the
                   # current directory's parent directory

The cd command is a built-in shell command.

{TopOfPage} {Resources}


The ls Command

The ls command is used to list directory contents.

For each argument that names a file of a type other than directory, ls displays its name as well as any requested, associated information. For each argument that names a file of type directory, ls displays the names of files contained within that directory, as well as any requested, associated information. If no arguments are given, the contents of the current directory are displayed. If more than one argument is given, non-directory arguments are displayed first; directory and non-directory arguments are sorted separately and in lexicographical order.

To find out information about a file (e.g. its ownership, permissions, size), the -l option can be used.

By default, file names starting with a dot are not listed. The -a option causes all files to be listed.

When doing a simple listing, the -F option can be used to help indicate file types. The following symbols are appended to each name listed.

 
   /  -- is a directory
   *  -- is an executable file
   @  -- is a symbolic link
   [...there are others...]

The -t option results in a listing that is sorted by time modified (most recently modified first).

Note on date and time displayed when doing a long listing: If the modification time of the file is more than 6 months in the past or future, then the year of the last modification is displayed in place of the hour and minute fields.

{TopOfPage} {Resources}


The cat Command

The cat command is used to concatenate files and print their content to the standard output stream.

The cat command takes one or more file names as arguments. Files are printed in command-line order. If cat cannot open file, then an error message is printed, but the command does not terminate (i.e. it goes onto the next file). If no arguments are used, then cat prints the contents read from the standard input stream.

	$ cat /etc/group /etc/passwd
	...displays the contents of /etc/group...
	...displays the contents of /etc/passwd...

{TopOfPage} {Resources}


The more Command

The more command is a "pager" or "filter" program that is used to view the contents of a file on a CRT.

The command takes one or more file names as arguments. When contents are displayed, the more command issues a prompt and waits for a more command to be executed. The more h (or ? command) can used to get help about what commands more supports. As per the manpage, if you forget all the other commands, remember the h or ? commands.

{TopOfPage} {Resources}


The head Command

The head command is used to print the first few lines of a file. By default, the first 10 lines of each file specified on the command-line are printed to the standard output stream. If no filenames are specified on the command-line, then input is obtained from the standard input stream.

If the first command-line argument is a number followed by a dash, then that number of lines are printed.

   $ head -5 /etc/passwd
   ...displays the first 5 lines in the /etc/passwd file...

   $ head /etc/group
   ...displays the first 10 lines in the /etc/group file...

If a file has less that 10 lines (or less than the number specified as an argument), then the contents of the entire file is printed.

{TopOfPage} {Resources}


The tail Command

The tail command is used to print the last few lines of a file. By default, the last 10 lines of each file specified on the command-line are printed to the standard output stream. If no filenames are specified on the command-line, then input is obtained from the standard input stream.

If the first command-line argument is a number followed by a dash, then that number of lines are printed.

   $ tail -5 /etc/passwd
   ...displays the last 5 lines in the /etc/passwd file...

   $ tail /etc/group
   ...displays the last 10 lines in the /etc/group file...

If a file has less that 10 lines (or less than the number specified as an argument), then the contents of the entire file is printed.

{TopOfPage} {Resources}


The mkdir Command

The mkdir command creates the directories named as arguments, in the order specified,.

The -p can be used to create intermediate directories as required. If this option is not specified, the full path prefix of each argument must already exist.

The command mkdir foo will fail if there a file named foo already exist.

   $ mkdir a b c d

      # make the directory named  a  followed by
      # a directory named  b  and so on...

An existing directory can be renamed using the mv command.

   $ mkdir a
   $ ls -F
   a/
   $ mv a b
   $ ls -F
   b/

{TopOfPage} {Resources}


The rmdir Command

The rmdir command is used to remove empty directories.

At least one command-line argument is required: the name of the directory to remove. Multiple directories can be removed using a single invocation of the rmdir command by specifying more than one directory name on the command-line.

The rmdir command will fail if you do not have "permission" to remove the directory.

{TopOfPage} {Resources}


The touch Command

The touch command sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created.

By default, the modification date and time are changed, but there are options that allows the modification and access date and times to be changed.

An option is also supported that allows file related dates and times to be switched to a date and time different from the current date and time.

{TopOfPage} {Resources}


The cp Command

The cp command is used to make a copy of a file.

   $ cp foo bar

      The file named  foo  is copied to a file named  bar.
      The file named  bar  is created if it doesn't exist;
      otherwise, the content of file  bar  is replaced with
      the content of file  foo.  [Note: this works only if
      permissions are okay.]

      The file  foo  must exist and be located in the
      current working directory.  The file  bar  is
      created in the current working directory.

You have to be careful when doing a file copy to make sure you don't clobber an existing file.

The -i option is used a lot to help prevent the accidental clobbering of files. The option causes the cp command to issue a confirmation prompt if a file clobber is about to occur.

   $ ls
   foo   bar
   $ cp -i foo bar
   overwrite bar? (y/n [n])

      The  cp  command issues a confirmation prompt
      warning the user that file  bar  is going to
      be clobbered.  The command continues only if
      the user enters a 'Y' or 'y' character.

Many SysAdmins alias the cp command to always use the -i (interactive) option. The -f (force) option can be used to make sure the cp command never issues a confirmation prompt.

   $ ls
   foo    bar
   $ cp -f foo bar

      File  foo  is un-conditionally copied to file bar.

If the second argument to the cp command is directory, then the file is copied to that directory and the filename is preserved. The target directory must exist.

   $ ls -F
   foo     junk/            # foo is a file; junk is a directory
   $ ls junk                # the  junk  directory is empty
   $ cp foo junk            # file  foo  is copied to the  junk  directory
   $ ls junk                #     with the filename preserved
   foo
   $ cp foo junk/bar        # copy file  foo  to the  junk  directory
   $ ls junk                #     renaming the file  bar
   foo     bar

More than one file can be copied at a time; however, this works only if the last argument is a directory name. The copy preserves file names.

   $ ls                      # current working directory has two files
   foo    bar                #    named  foo  and  bar
   $ ls /tmp                 # the  /tmp  directory is empty
   $ cp foo bar /tmp         # copy both  foo  and  bar  to  /tmp
   $ ls /tmp                 # the  /tmp  directory now has two files
   foo    bar                #    named  foo  and  bar
   $ ls                      # foo  and  bar still exist in the current
   foo    bar                #    current working directory

Complete directories of files can be copied in a single execution of the cp command, but that is beyond the scope of this note. [think -R (recursive) option]

{TopOfPage} {Resources}


The mv Command

The mv command is used to move a file to either a new name, or a new location in the directory structure.

   $ mv foo bar

      The file named  foo  is moved (renamed) to a file named  bar.
      [Note: this works only if permissions are okay.]

      The file  foo  must exist and be located in the
      current working directory.  

You have to be careful when doing a file move to make sure you don't clobber an existing file.

The -i option is used a lot to help prevent the accidental clobbering of files. The option causes the mv command to issue a confirmation prompt if a file clobber is about to occur.

   $ ls
   foo   bar
   $ mv -i foo bar
   overwrite bar? (y/n [n])

      The  mv  command issues a confirmation prompt
      warning the user that file  bar  is going to
      be clobbered.  The command continues only if
      the user enters a 'Y' or 'y' character.

Many SysAdmins alias the mv command to always use the -i (interactive) option. The -f (force) option can be used to make sure the mv command never issues a confirmation prompt.

   $ ls
   foo    bar
   $ mv -f foo bar

      File  foo  is un-conditionally moved to file  bar.

If the second argument to the mv command is directory, then the file is moved to that directory and the filename is preserved. The target directory must exist.

   $ ls -F
   foo     junk/            # foo is a file; junk is a directory
   $ ls junk                # the  junk  directory is empty
   $ mv foo junk            # file  foo  is moved to the  junk  directory
   $ ls junk                #     with the filename preserved
   foo
   $ mv foo junk/bar        # move file  foo  to the  junk  directory
   $ ls junk                #     renaming the file  bar
   foo     bar

More than one file can be moved at a time; however, this works only if the last argument is a directory name. The move preserves file names.

   $ ls                      # current working directory has two files
   foo    bar                #    named  foo  and  bar
   $ ls /tmp                 # the  /tmp  directory is empty
   $ mv foo bar /tmp         # move both  foo  and  bar  to  /tmp
   $ ls /tmp                 # the  /tmp  directory now has two files
   foo    bar                #    named  foo  and  bar
   $ ls                      # foo  and  bar are gone from the current
                             #    current working directory

Complete directories of files can be copied in a single execution of the cp command, but that is beyond the scope of this note. [think -R (recursive) option]

{TopOfPage} {Resources}


The rm Command

The rm command is used to remove (delete) files (directory entries) from the system.

If the permissions of the file do not permit writing, and the standard input device is a terminal, the user is prompted (on the standard error output) for confirmation. This prompting can be disabled using the -f option.

The -i causes the command to issue a confirmation prompt prior to removing a file.

The -R option is the recursive option. The command attempt to remove the file hierarchy rooted in each file argument.

It is an error to attempt to remove the files . or .., or to a remove a file that doesn't exist.

Generally, you cannot remove a file unless you are the owner of the file.

{TopOfPage} {Resources}


The ln Command

The ln command creates a new directory entry, i.e. a new name in the filesystem. There are two types of links: hard and symbolic.

By default ln makes hard links. A hard link to a file is indistinguishable from the original file. This permits multiple names for a file without using up filesystem space for multiples copies of the file. Any changes made to the file are independent of the name used to reference the file. Removing the file (by any name) will not remove the file's contents until the removal of the last name for the file.

Hard links may only refer to existing files, may not refer to directories and may not span file systems.

A symbolic link contains the name of the file to which it is linked, and should be thought of as an indirection instead of as a copy. This indirection is accomplished automatically by the system. This permits multiple names for files as well as short-hand paths for long directory paths. Removing the symbolic link will remove only the link, and will not delete the file's contents.

Examples
   $ touch x               # create an empty file named  x
   $ ln x y                # link file x to file y; these files have the
                           # same i-node number 
   $ mkdir a               # create a directory named  a
   $ ln a b                # error:  cannot hard-link directories
   ln: b is a directory    # message given from the  ln  command
   $ rm x                  # file  x  is removed; but file  y  remains
   $ ln y /tmp             # works only if  /tmp  and the file  y  are
                           # on the same file system

{TopOfPage} {Resources}


The wc Command

The wc command is used to determine how many characters, words, and lines are contained in a file.

By default, wc prints all pieces of data; however, options can be used to get specific pieces.

   $ wc cd.html           # count of lines, words, characters
      31     148    1014 cd.html

   $ wc -l cd.html        # count of lines only
      31 cd.html

   $ wc -c cd.html        # count of characters only
    1014 cd.html

   $ wc -w cd.html        # count of words only
     148 cd.html

   $ wc -cw cd.html       # count of words and characters
     148    1014 cd.html

The following was extracted from a wc manpage:

A line is defined as a string of characters delimited by a <new-line> character, and a word is defined as a string of characters delimited by white space characters. White-space characters are the set of characters for which the isspace(3) function returns true.

{TopOfPage} {Resources}


The file Command

The file command is used to determine what the content-type of a file is.

   $ file cd.html
   cd.html: ASCII English text

   $ file /etc/group
   /etc/group: ASCII text, with very long lines

   $ file /usr/bin/who
   /usr/bin/who: ELF 32-bit LSB executable, Intel 80386, 
                 version 1 (FreeBSD), dynamically linked 
                 (uses shared libs), stripped

   $ file $c/code/HelloWorld.c
   .../HelloWorld.c: ASCII C program text

   $ file $c/code/HelloWorld.cpp     # this is a C++ source code file
   .../HelloWorld.cpp: ASCII C program text

   $ file $java/code/HelloWorld.java
   .../HelloWorld.java: ASCII English text

{TopOfPage} {Resources}


I-nodes

Internally, Unix keeps track of files using a structure that is called an i-node (index node).

File names are used to make Unix a "user-friendly" system. Internally, Unix refers to files by number; more specifically, their i-node number.

Each file within a file system has a unique i-node number assigned to when the file is created.

Executing the ls command with the -i option causes the i-node numbers to be displayed.

If two files have the same i-node number, then the files are linked (i.e. they are the same physical file, but the file has two names).

An i-node structure (record) may contain the following information about a file:

   + it's size (in bytes)
   + permissions
   + owner
   + group
   + date and time created, last modified, last accessed
   + link count
   + "pointers" to where the data of the file is stored

{TopOfPage} {Resources}


Directory and File Permissions

Recall, every file has an owner and belongs to a group. In addition, every file has a set of permissions on it that controls who has access to the file (e.g. who can read it, who can write it, and so on). The owner, group, and permissions are initialized when a file is created.

The chmod command is used to change the permissions on a file.

Typically, by default, when a file is created, it is initialized with the following file permissions.

   -rw-rw-rw-   =>  read/write by everybody
   || || || |
   || || |+---> other permissions
   || || |
   || |+---> group permissions
   || |
   |+---> owner permissions
   |
   +-> indicates a regular file 

[Sidebar] The default permissions are determined by the setting of the user's umask. Most users want the default permissions to be: -rw-r--r-- (read/write by owner, read-only for everybody else).

Numerically, permissions of -rw-r--r-- is represented by the octal (base 8) number 644.

read4 write2 execute1

The octal modes are specified by adding together a 4 for read, 2 for write and 1 for execute permission. The three digits specify, as in ls -l, permissions for the owner, group, and everyone else.

Examples
   444  =>  -r--r--r--
   777  =>  -rwxrwxrwx
   602  =>  -rw-----w-
   000  =>  ----------
   222  =>  --w--w--w-
   460  =>  -r--rw----
   541  =>  -r-xr----x
   644  =>  -rw-r--r--
   711  =>  -rwx--x--x
   711  =>  drwx--x--x       # this is a directory
   777  =>  drwxrwxrwx       # this is a directory

   $ chmod 644 foo
   $ chmod 777 foo bar junk
   $ chmod 400 /etc/shadow

   If only one octal digit 'N' is used, 
   then that is equivalent to 00N.

   If two octal digits 'MN' are used,
   then that is equivalent to 0MN.

   $ chmod 7 foo                # results in ------rwx
   $ chmod 24 foo               # results in ----w-r--

Only the owner of a file (or super-user) can change the permissions on a file.

Permissions can be set by symbolic description, but details of this are left to the reader. The following is a brief overview. The syntax of using the symbolic-mode of chmod is:

      [who] op permission

who is a combination of the letters.

      u ... user
      g ... group
      o ... others
      a ... all  (ugo)

op can be a + to add permission or - to remove or = to assign permissions.

permission is a combination of the letters r, w, and x. (note: there are additional letters).

   $ chmod a=rw foo    # set permissions to read/write for all
   $ chmod +x foo      # set execute permission
   $ chmod o-w foo     # remove write permission from other
Directory Permissions

Permissions have a different meaning when applied to directories. In a nutshell.

   r  =>  you can read the directory (e.g. do an 'ls' on it)
   w  =>  means you can add files to and remove files from the directory
   x  =>  allows you to 'cd' into the directory or use it as part 
          of a path

Recall, that a directory is a file: it contains information about other files.

About the umask Command

The umask command is used to set the default creation mask for files and diretories. It is kind of like the reverse of chmod: it tells the system which permissions should not be given when a file is created.

   $ umask 000
   $ >foo
   $ ls -l foo
   -rw-rw-rw- ... foo
   $ umask 022
   $ >bar
   $ ls -l bar
   -rw-r--r-- ... bar
   $ umask
   022

Many SysAdmins put the umask 022 command in the /etc/profile file. As a result, the default permissions on directories and files are drwxr-xr-x and -rw-r--r--, respectively.

{TopOfPage} {Resources}


Environment Variables

An environment variable is a variable that is made available to commands as part of the environment that the shell maintains.

Environment variables are useful for a variety of reasons:

Personal environment variables can be defined in your .profile (or .bash_profile) file stored in your HOME directory. The .profile file is executed every time you log into the system.

Prior to executing the .profile, the shell executes /etc/profile. This ensures that all users start with a common environment upon successful login.

On most systems, the following environment variables are automatically set for you.

HOME absolute path of your home directory
PATH used by your shell to find programs
PS1 your shell prompt
SHELL the name of the shell program you are using
LOGNAME your login account name
TERM the type of terminal you are using
MAIL absolute path of where your email is stored

An environment variable has a name and an associated value. Prefixing an environment variable with a dollar sign causes the shell to get the variable's value. The echo command can be used to display the value of a environment variables. Here are some examples.

   $ echo $HOME
   /export/home/gthurman

   $ echo $SHELL
   /usr/bin/ksh

   $ echo $MAIL
   /var/mail/gthurman

   $ echo TERM is $TERM and LOGNAME is $LOGNAME
   TERM is vt100 and LOGNAME is gthurman

One of the most useful environment variables is PATH. PATH is used by the shell to locate commands. The following is a common value assigned to the PATH environment variable:  /bin:/usr/bin:/usr/local/bin. When a command is entered, the shell (using the value of PATH) first looks in the /bin directory for the program and executes it if it finds it; otherwise, is searches /usr/bin for the command, followed by /usr/local/bin. If the shell searches all the components without finding the executable file, then it prints a "command not found" error message.

By default, the shell does not look in your current working directory for the command. This can be changed to modifying your PATH as follows.

   $ PATH=$PATH:.        or    PATH=$PATH::

      Important note:  If you do put the current working directory on
      the PATH, then it should be last the component.  You never want
      it to be the first component.

If you enjoy playing games, then you may alter the PATH as follows.

   PATH=$PATH:/usr/games

You can use your own environment variables for abbreviations. For example.

   $ letters=$HOME/personal/letters
   $ cd $letters

If you have an environment variable that you want available to other commands, then you need to export it.

   $ EDITOR=/usr/bin/vi export EDITOR

      Now the EDITOR environment variable can be used by email
      programs, pager programs, and so on.

Environment variables are often used to "hide" the complexities of the directory structure. In other words, you can access a specific directory by name without having to know its absolute path.

By convention, personal environment variables are spelled in lower case to help distinguish them from those setup by the system.

Environment variable names must begin with a letter.

The env program can be used to display all the environment variables you have defined.

A variable can be removed from the environment using the unset command. Syntax.

   $ unset variable_name

{TopOfPage} {Resources}


Tilde Expansion [minimal notes]

Shells such as ksh and bash support tilde expansion. The ~ expands to the operand's HOME environment variable setting.

   example 1
   ---------
   $ echo ~thurmunit
   /users/n-z/t/th/thurmunit

   example 2
   ---------
   $ pwd
   /users/n-z/t/th/thurmunit
   $ cd ~hmumford
   $ pwd
   /users/a-m/h/hm/hmumford

   example 3
   ---------
   $ who am i
   gdt   /dev/pts/14  Sep 25 05:08
   $ cd ~/tmp
   $ pwd
   /export/home/g/gdt/tmp

GNU.org:: Bash Reference Manual: Tilde Expansion

{TopOfPage} {Resources}


Standard Input/Output/Error

In Unix, a file is a sequence of bytes that is stored somewhere on a storage device (e.g. a disk). The content of a file does not have any significant meaning to the OS (it is simply a sequence of bytes). The "structure" of the bytes may have to conform to a particular format in order to be used by specific applications (i.e. programs or commands).

Every command, when invoked, has three I/O (input/output) streams opened for it: two for output and one for input. The output streams are called standard output and standard error. The input stream is called standard input. In "C" terminology, these I/O streams have the names stdout, stderr and stdin, respectively.

A command can get (read) input from the standard input stream, but it doesn't know where the input comes from (it could come from a file, the keyboard, or another command).

       file--->+
               |
   keyboard--->+ ---> standard input ---> command
               |
    command--->+

The output of a command (if any) can be written to either the standard output stream or the standard error stream. The command doesn't need to know where the output is going (it could go to a file, the screen, or another command).

                                     +---> file
                                     |
   command ---> standard output ---> +---> screen
                                     |
                                     +---> command

The shell manages the standard I/O streams for a command.

The shell assigns a file descriptor to each of the I/O streams: standard input is 0, standard output is 1, and standard error is 2.

By default, standard input is the keyboard, and standard output and standard error are both the screen. Re-directing these streams is accomplished on the command-line when the command is invoked using the re-direction operators. [Note: It can also be done internally by the command itself.]

The re-direction operators are as follows.

SymbolExampleFunction
< cmd < file take input for cmd from file
> cmd > file send output of cmd to file
>> cmd >> file append output of cmd to file
| cmd1 | cmd2 run cmd1 and send output to cmd2

{TopOfPage} {Resources}


Redirecting Standard Output and Standard Error

In some instances you want to save the output of a command into a file. This can be accomplished using the shell's redirection operators > and >>.

Here are examples.

  $ who > who.out

    Executes the command 'who' and redirects the output into
    a file named "who.out".  No output is seen on 
    the screen.  The file "who.out" is created in 
    your current working directory.

  $ date > /tmp/now

    Executes the command 'date' and redirects the output to a
    file named "now" stored in the "/tmp" 
    directory.  (Note: If the directory "/tmp" contains 
    a directory named "now", then the shell will not execute the
    command.

  $ ls -l>myfiles

    Use of whitespace around the > operator is optional.  
    Executes the 'ls -l' command with output redirected into a 
    file named "myfiles".

If you redirect the output of a command into a file that doesn't exist, then the shell creates the file for you (assuming permissions are not a problem). If you redirect the output of a command into a file that already exists, then the content of the existing file is replaced with the command's output (again, if allowed).

   $ ps > /etc/foo

     This command should fail on most Unix systems.  We are attempting
     to create a file named "foo" in the "/etc"
     directory.  "/etc" is a system-level directory and 
     regular users are not allowed to write to it.

If you want to redirect the output of a command and have it appended to the content of an existing file, then you must use the >> operator.

   $ who > cmd.log

     The 'who' command is executed and its output redirected into
     a file named "cmd.log".

   $ date >> cmd.log
      
     The 'date' command is executed and its output is appended to
     the file named "cmd.log".  (If file "cmd.log"
     doesn't exist, then the system creates it.)  Again, the use of 
     whitespace around the operator is optional.

   $ ps >> cmd.log

     The 'ps' command is executed and its output is appended to the
     file named "cmd.log".

   $ cat 'mygroup:*:40000:' >> /etc/group

     This command should fail on most Unix Systems when executed as 
     a regular (i.e. non-root) user.  We are attempting to add a
     group to file that should be read-only.
Re-directing Both Output and Error

The following are some examples of how you can re-direct the standard error stream. Note: these examples work with Bourne, Korn and Bash shells, but they do not work with "C" shell.

   Assume  cmd  is some Unix command.

   $ cmd >out 2>err
     
     'cmd' is executed.  Standard output is re-directed to a file
     named "out" and standard error is re-directed to a 
     file named "err".

   $ cmd >out 2>&1

     All output (both standard output and standard error) are
     redirected into a file named "out".

   $ cmd 2>foo

     The standard error is re-directed to a file named "foo".

{TopOfPage} {Resources}


Home Previous Next