Unix Fundamentals - Novice Topics
Unix Fundamentals - Novice Topics
Home >> Computing >> Unix Fundamentals >> Novice Topics

Contents:


How do I change my UNIX password?
[Top] [Contents]

To change your login password, use the passwd command; type passwd and then hit enter. You will be prompted for your old password and then you will be asked to enter your new password twice. If both occurrences of the new password are not identical (remember that UNIX passwords are case sensitive) then the change will not be successful.


UNIX file system basics.
[Top] [Contents]

The most important part of any computer system is the data that is stored and manipulated by programs. It is the responsibility of the file system to maintain all of this data in an orderly and reliable fashion. As a user, you need not perform any maintenance of the UNIX file system; this is the responsibility of the system administrator(s). However, a basic understanding of the UNIX file system is important to enable an enriched understanding of other UNIX concepts.

The concept of a file in everyday life is fairly straight forward; a collection of papers bundled in a folder. Extending this metaphor to computers, a file is a collection of data stored on hard disk, floppy disk, CD or tape (or whatever the media du jour is). A computer file may be an executable program, a spreadsheet, a word processor document, a game, a picture or whatever it may be. In UNIX, the definition of a file is much broader; a file is any source from which data can be read or any target to which data can be written. Therefore, a UNIX file would include those entities described [as a computer file] above but also includes physical devices such as a keyboard or a hard disk drive. Basically, everything in UNIX is viewed as a file.

UNIX has three primary types of files; ordinary (or regular) files, directories and special files. An ordinary or regular file are those that contain programs or data; this conforms to the above description of computer files. For example, when you use a text editor to create a document and/or modify a document, the document is stored in an ordinary file. You may then manipulate this file as you choose (modify, save, copy, move, rename, delete and so on).

The next important file type is a directory. A directory is a container that is used to store (and organize) other files and directories. You may create, rename and delete directories as you wish with one caveat; a directory may not be deleted while it still contains any files (or directories). A directory does not really contain files but it contains the information UNIX needs to access those files.

The third type of file is a special file or a device file which is an internal representation of a physical device. For example, your hard drive has an associated special file that the operating system uses to access that device. To access the hard drive, the OS opens the special file associated with that device and then reads and writes data to the device as if it were a regular file (it's really a little more complicated than that but at a high level this explanation is adequate).

Directories are used to organize files into an orderly hierarchical system. Imagine the confusion that would reign if all of the files on the network were stored in one flat location (there are tens of thousands of files)! To avoid this confusion, we group related files into directories. Directories may themselves contain other directories. A parent directory is one that contains other directories while a subdirectory is a directory that resides inside some other directory; a subdirectory is often referred to as a child directory.

The UNIX file system can be conceptually viewed as an upside-down tree (or simply a tree for those of you versant in computational theory). The file system has a root directory from which all other directories branch; this root directory is name '/' or slash. Figure 1 illustrates the basic layout of the UNIX filesystem.

UNIX directory tree
Figure 1: UNIX file system skeleton

The root or '/' directory is the parent of all of the other directories. The /usr directory is a subdirectory or child directory of '/'. At the same time, /usr is itself a parent directory for other subdirectories (such as bin local and admin). Each directory (including '/') may contain files as well as other directories.


What is meant by the term pathname?
[Top] [Contents]

One term you will often see bandied about any discussion of file systems is pathname. The term pathname refers to the fully qualified name of a file wich contains location information.

The root or '/' directory is very important when specifying pathnames. For example, the explicit path of a file may be /usr/bin/echo. The '/' character has two different meanings here. At the beginning of the file pathname, '/' refers to the root directory while the '/' character within a pathname acts as a field delimiter. In this case, the file's name is echo and it can be found in the bin directory which is a subdirectory of the usr directory which is itself a subdirectory of the '/' (root) directory. An absolute pathname always contains a leading '/'; an absolute pathname is therefore relative to the '/' directory. The pathname /usr/bin/echo is an example of an absolute pathname. A relative pathname specifies a path relative to the current working directory. For example, if our current working directory was /usr/bin then the pathname echo would sufficiently identify this file.

When working with directories, a very important concept is the distinction between a relative and an absolute pathname. To illustrate the difference, refer to the sample directory tree in figure 2.

sample UNIX tree
Figure 2: sample UNIX file tree

An absolute pathname specifies the file's path relative to '/'. For example, the absolute pathname of the file resume.shtml (from figure 2) would be /home/jhacker/public_html/resume.shtml. The pathname is absolute with respect to the root of the file system (in fact the leading '/' differentiates an absolute path from a relative path).

A relative path is the location of a file or directory relative to the current working directory. For example, if your current working directory was /home/jhacker and you wanted to refer to the file hello_world.c in the projects/new directory, you could use the relative pathname projects/new/hello_world.c (note the absence of the leading '/'). If your current working directory was /home/jhacker/projects/new, you could refer to the hello_world.c file using its name alone.


What is my current working directory and how do I change it?
[Top] [Contents]

UNIX allows you to designate one directory at a time as your current working directory. The current working directory is the directory in which you currently reside. To determine your current working directory, use the pwd (print working directory) command (advanced UNIX users will recognize the merrits of echo $cwd). To access a file that is in your current working directory you need only specify the filename; the whole path need not be specified (this is of course assuming that '.' is in your search path - more on this later).

sample current directory tree
Figure 3: sample UNIX file tree revisited


Referring to figure 3, if our current working directory was /home/jhacker, then we would be able to refer to the file doit.c by it's name alone (a fullpathname would not be required here). However, to refer to the file index.shtml in the public_html directory, we would need to use either the relative path public_html/index.html or the absolute path /home/jhacker/public_html/index.html.

To change your current working directory, use the cd (change directory) command. The general syntax of this command is:

    cd directory_name
The directory_name may be either a relative or an absolute pathname. Referring to figure 2 above, if our current working directory is /home/jhacker, we could change our working directory to projects in the following manner:
    ~ >>cd public_html
    ~/public_html >> pwd
    /home/jhacker/public_html
UNIX provides some handy pathname abbreiviations. The first is '..' which refers to the parent directory. If our current directory is /home/jhacker/public_html, typing cd .. would change our working directory to /home/jhacker; it would take us up one level in the file system tree. We can use the '..' abbreiviation twice. Revisiting our previous example, if our current directory was /home/jhacker/public_html, typing cd ../.. would change our current directory to /home. Other directory names can be used with this mechanism as well. If our working directory is /home/jhacker/projects/new and we want to change to the /home/jhacker/bin directory, we could type cd ../../bin.

The second handy abbreviation is '.' which refers to the current working directory. For example, if our current directory was /home/jhacker/public_html, the following three specifications would refer to the same file:

    /home/jhacker/public_html/resume.shtml
    resume.shtml
    ./resume.shtml
The '.' abbreviation is quite helpfull when your current directory is one that is not in your executable path and you want to refer to a file in the working directory (but as most people have '.' in their path this is not an issue).

The last of the three handy abbreviations is '~' which refers to your home directory. For example, if you are logged in as jhacker, the following commands would both take you to your home directory:

    cd /home/jhacker
    cd ~
This last abbreviation is particularly usefull when your current directory is not your home directory and you need to make reference to a file from your home directory. For example, to refer to the file fone.pl in jhacker's bin directory we could use ~/bin/fone.pl.


How do I list my files?
[Top] [Contents]

Listing files can be done using the built in ls command. Without options, ls simply lists the entries in your current directory. For example, running ls in my public_html directory provides the following output:

    ~/public_html >>ls
    altavist.shtml    hotline.shtml     make_luser_list   weborg.shtml
    cbenson.shtml     lusers.shtml      unix_faq.shtml
    ~/public_html >>
Using the -a option lists hidden files (files whose names begin with a '.').
    ~/public_html >>ls -a
    ./                altavist.shtml    lusers.shtml      weborg.shtml
    ../               cbenson.shtml     make_luser_list
    .atlvista.shtml   hotline.shtml     unix_faq.shtml
    ~/public_html >>
Note the two entries './' and '../'. The './' entry refers to the current directory while the '../' entry refers to the parent directory. Also note the entry .atlvista.shtml which was displayed when ls -a was used by not when ls was used; ls [without options] will not display this entry as the item's name begins with a dot ('.').

Using the -l option provides a long listing which includes file permissions, ownership, size and last modified time along with the file name.

    ~/public_html >>ls -l
    total 84
    -rw-r--r--   1 dhay     sysadmin     367 Sep 24 09:27 altavist.shtml
    -rw-r--r--   1 dhay     sysadmin     504 Sep 15 10:05 cbenson.shtml
    -rw-r--r--   1 dhay     sysadmin    1571 Sep 23 13:16 hotline.shtml
    -rw-r--r--   1 dhay     sysadmin   17274 Oct  7 09:12 lusers.shtml
    -rwxr-x---   1 dhay     sysadmin    2115 Sep  9 15:30 make_luser_list
    -rw-r--r--   1 dhay     sysadmin    6841 Oct  7 12:31 unix_faq.shtml
    -rw-r--r--   1 dhay     sysadmin   10560 Oct  6 16:28 weborg.shtml
    ~/public_html >>
The options can be combined. Typing ls -al would result in a long listing including hidden files (try it). I like to use ls -alF. The F option appends a '/' to entries that are directories or symbolic links to directories, appends a '*' to entries that are executable files or it appends a '@' to entries that are a symbolic link to another file. This may seem like quite a mouthful but having all of that information available at a glance can be quite useful.

A neat trick is to pipe the output of ls -alF through more (more advanced UNIX users will want to use less instead). The more utility gives you one screen of output at a time. This is handy if you are listing a directory that contains a large number of items. To do this type ls -alF | more and then hit any key to advance the input.

The options discussed here represent a small subset of those available for use with ls. The man pages on the ls command are actually fairly readable (which is unusual for the man pages). I recommend that you have a look at them to determine the full range of options available for use with ls (RTFM).


How do I copy files?
[Top] [Contents]

To copy a file, us the cp command. The general syntax is:

    cp [-i|-f] source_file  new_file
The source_file is the file you are copying from while the new_file is the file you are copying to. The -i option prompts the user for confirmation in the event that the copy operation would overwrite an existing file. The -f option forces any existing destination pathnames to be removed before copying; this option forces the destruction and replacement of any file that conflicts with the new_file specified in the copy operation. The -f option can be fairly dangerous; use it with extreme caution!

One can copy a file [or files] to a different directory. The general syntax of this usage is:

    cp [-i|-f] source_file(s)  directory
The -i and -f options perform as described above.

For example, the command

    cp parse.pl /usr/local/lib/www/cgi-bin/DES/.
copies the file parse.pl to its new home. Note the use of the '.' in the destination file/path; this denotes that the file will have the same name (as that of the source file). The use of the '.' in this case is optional; we could have typed
    cp parse.pl /usr/local/lib/www/cgi-bin/DES/
and the effect would have been exactly the same. To copy the files junk.pl templist2 and parse.pl to the directory ~/stuff use the following syntax:
    cp junk.pl templist2 parse.pl ~/stuff

The wildcard option can be used when copying files; use '*' for the source_file specification and specifiy a directory path for the new_file. For example, to copy the contents of my perl directory to the scripts directory, I would type:

    ~/perl >>ls -al
    total 66
    drwx------   2 dhay     www         1024 Nov  7 10:20 ./
    drwxr-xr-x  36 dhay     sysadmin    2048 Nov  7 12:16 ../
    -rwxr-x---   1 dhay     www         2874 Oct 24 15:27 hinfo
    -rwxr-x---   1 dhay     www         1619 Aug 29 11:17 junk.pl
    -rw-rw----   1 dhay     www        10766 Sep  9 10:03 name_list.shtml
    -rwxr-x---   1 dhay     www         2107 Sep  3 15:54 parse
    -rw-rw----   1 dhay     www        10766 Sep  9 10:03 templist2
    ~/perl >>cp * ~/scripts
    ~/perl >>cd ../scripts
    /net/nwdch014/lv_disk1/users/dhay/scripts
    ~/scripts >>ls -alF
    total 66
    drwxrwx---   3 dhay     sysadmin    1024 Nov  7 12:18 ./
    drwxr-xr-x  36 dhay     sysadmin    2048 Nov  7 12:16 ../
    -rwxr-x---   1 dhay     sysadmin    2874 Nov  7 12:18 hinfo*
    -rwxr-x---   1 dhay     sysadmin    1619 Nov  7 12:18 junk.pl*
    -rw-r-----   1 dhay     sysadmin   10766 Nov  7 12:18 name_list.shtml
    -rwxr-x---   1 dhay     sysadmin    2107 Nov  7 12:18 parse*
    drwxrwx---   2 dhay     sysadmin      96 Oct 15 09:35 sh/
    -rw-r-----   1 dhay     sysadmin   10766 Nov  7 12:18 templist2
One may use the cp command to copy a directory and all of its files to another directory using the -r or recursive option. The general syntax is:
    cp -r source_dir  destination_dir
This will copy all of the files (and directories) found in the source_dir (or source directory) to the destination_dir (or destination directory). This basically copies the entire subtree (specified by source_dir) to the specified destination_dir. There is one functional quirk when using the -r option though. If the destination_dir exists, a directory will be created inside the destination_dir with the same name as the source_dir and the contents of the source_dir will then be recursively copied into this newly created subdirectory. To illustrate this point, lets say we typed
    cp -r ~/stuff ~/public_html
but the directory public_html already exists. At this point, a new directory with the path ~/public_html/stuff would be created and the contents of ~/stuff would then be copied to the directory ~/public_html/stuff. If the ~/public_html directory did not exist, it would be created and the contents of ~/stuff would be copied into it.


How do I move and/or rename a file?
[Top] [Contents]

Moving and renaming a file are really the same thing. Both actions are performed using the mv command. The general syntax of this command is as follows:

    mv old_name  new_name
If the file specified by new_name already exists, it will be replaced. Therefore, exercise extreme caution when using this command. For example, if we wanted to rename a file called hinfo to hp_node_info.pl, we would type the following commands:
    ~/perl >>ls -al
    total 66
    drwx------   2 dhay     www         1024 Nov  7 10:20 ./
    drwxr-xr-x  36 dhay     sysadmin    2048 Nov  7 12:16 ../
    -rwxr-x---   1 dhay     www         2874 Oct 24 15:27 hinfo
    -rwxr-x---   1 dhay     www         1619 Aug 29 11:17 junk.pl
    -rw-rw----   1 dhay     www        10766 Sep  9 10:03 name_list.shtml
    -rwxr-x---   1 dhay     www         2107 Sep  3 15:54 parse
    -rw-rw----   1 dhay     www        10766 Sep  9 10:03 templist2
    ~/perl >>mv hinfo hp_node_info.pl
    ~/perl >>ls -al
    total 66
    drwx------   2 dhay     www         1024 Nov  7 10:20 ./
    drwxr-xr-x  36 dhay     sysadmin    2048 Nov  7 12:16 ../
    -rwxr-x---   1 dhay     www         2874 Oct 24 15:27 hp_node_info.pl
    -rwxr-x---   1 dhay     www         1619 Aug 29 11:17 junk.pl
    -rw-rw----   1 dhay     www        10766 Sep  9 10:03 name_list.shtml
    -rwxr-x---   1 dhay     www         2107 Sep  3 15:54 parse
    -rw-rw----   1 dhay     www        10766 Sep  9 10:03 templist2
If the target (the file specified by new_name) already exists, it will be replaced. The use of the -i or interactive option will cause mv to prompt you in the event that such a collision will occur. Revisiting the previous example, if the file hp_node_info.pl already existed, then using the -i option would look like:
    ~/perl >>mv -i hinfo hp_node_info.pl
    remove hp_node_info.pl? (y/n) n
    ~/perl >>
By answering n, the existing file will not be replaced.

To move a file or a group of files to another directory, use the following syntax:

    mv file(s)  dest_dir
where file(s) is the source file or files and the dest_dir is the destination directory to which the file(s) will be moved. For example, to move the files foo, bar and fugazi to the target directory /home/jhacker/junk, we would type:
    mv foo bar fugazi /home/jhacker/junk
It should be noted that if any of the files exist in the target directory, they would be replaced (unless the -i option is used).


How do I delete files?
[Top] [Contents]

When deleting files, always exercise extreme caution!! Think before you type!! When a file is deleted (removed) in UNIX it is gone; you cannot undelete the file like in DOS. The only way to recover an accidentally deleted file is to talk to your friendly system administrators and convince them that a.) the file really is important (it is critical to the performance of your job - we do not restore gif's of binky the cat) b.) you really do need to have it restored from the backup tapes c.) you promise not to do it again. Generally, sys admin types are not fond of doing tape restorations so please be extremely careful. As most of the backup tapes are stored off site, and as it usually takes at least a day to get tapes sent in, an accidentally deleted file will be inaccessable for a little while. Can you afford to have potentially critical files vanish for a few days?
 
Remember, there are two types of lusers: those who have lost data and those who will lose data!
Be carefull!!
 

To delete a file, use the rm or remove command. The general syntax is as follows:

    rm [-fir] file(s)
where file(s) is the name of the file or files that you wish to punt. Use of the -i option is highly recommended. This has the effect of asking you if you really want to delete a particular file. If you develop the habit of using this option (it might even be a good idea to alias rm to rm -i ...more on this later...) you could save yourself a lot of trouble. An example of the usage of rm to remove a few files is as follows:
    ~/faq >>ls -al
    total 6
    drwxr-x---   2 dhay     sysadmin    1024 Nov 20 13:03 ./
    drwxr-xr-x  35 dhay     sysadmin    2048 Nov 20 14:45 ../
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 bar
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 foo
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 fugazi
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 that
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 thing
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 this
    ~/faq >>rm -i foo bar fugazi
    foo: ? (y/n) y
    bar: ? (y/n) y
    fugazi: ? (y/n) no
    ~/faq >>ls -al
    total 6
    drwxr-x---   2 dhay     sysadmin    1024 Nov 20 15:11 ./
    drwxr-xr-x  35 dhay     sysadmin    2048 Nov 20 14:45 ../
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 fugazi
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 that
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 thing
    -rw-r-----   1 dhay     sysadmin       0 Nov 20 13:03 this
    ~/faq >>
Notice that the file fugazi was not deleted as when prompted [for deletion] I answered no. It should be noted that this prompt is your last chance; if you answer yes when prompted, the file in question is vapourware!! Always think about what you are doing when deleting files.

As with all file operations, wildcard characters can be used with the rm command. For example, if our current working directory is ~/faq (refer to the above example), typing the command

    rm -i *
will remove all of the files in that directory. Note the use of the -i option; this will prevent us from accidentally removing important files.

The rm command also has a -r option which recursively deletes an entire subtree. I will not discuss this option here; I encourage users to RTFM for more information on the use of this option. I would not advise novice UNIX users to use the -r option at all (if you screw up with this one you can blow away a lot of data). If you must use the -r option, be sure to use it with the -i option as well!!