UNIX is an operating system available on many computers, from PC's to supercomputers. Originally developed by AT&T, it has been vastly improved upon over the years.
Remote Access is provided to the UNIX hosts via our ethernet network, and SSH software connects the local computer to the UNIX hosts.
Some Basic UNIX Concepts
Every user has a unique username. When they logon to the system, they are placed in a HOME directory, which is a portion of the disk space reserved just for them. When you log onto a UNIX system, your main interface to the system is called the UNIX SHELL. This is the program that presents you with the dollar sign ($) prompt. This prompt means that the shell is ready to accept your typed commands. There is more than one variety of shell that can be used on a UNIX system. For the purposes of this tutorial, we will assume you are using one of the most standard UNIX shells called the Bourne Shell. Other shells such as the Korn Shell or Bourne Again Shell work very similarly at this level. They all use the dollar sign as their prompt.
UNIX commands are strings of characters typed in at the keyboard. To run a command, you just type it in at the keyboard and press the ENTER key. We will look at several of the most common commands below.
UNIX extends the power of commands by using special flags or switches. These switches are one of the most powerful features of UNIX commands. Switches are usually preceded with a dash ( - ) and preceed any filenames or other arguments on the command line.
Unlike the DOS (or Windows) command line, UNIX systems are case sensitive (upper and lower case characters are considered different). Nearly all command names and most of their command line switches will be in lowercase.
All of the UNIX commands in this tutorial should be typed in lowercase characters.
Logging Into UNIX
Follow the appropriate instructions for your system to access our Unix servers. The tool needed to log into our unix login server is called The Secure Shell or "SSH" for short.
The dollar sign prompt (or a prompt ending with a dollar sign) means that UNIX is now ready to interpret and execute your commands as typed in from your keyboard.
Objectives of this Tutorial
The objective of the following exercises is to introduce you to basic UNIX commands. The commands and utilities you will use are the same as those on other UNIX systems. At the end of these exercises you will be able to do the following tasks:
- create, delete
- display contents
- move around in the UNIX file tree and look at file entries
- create, delete, append, concatenate, display, move
- count the number of lines, words, characters
- change permissions on files
- search for text strings within files
- display a list of users
- obtain help on command usage
- run programs in the background
UNIX systems have a hierarchical directory structure. This means that the the hard disk area is divided into directories, much like a book is sub-divided into chapters and paragraphs. The directories form an tree-like structure, which simplifies the organization of the files on the system.
When you first log into a UNIX system, you are placed in your own personal directory space called your HOME directory. On most UNIX systems, the user HOME directories are located under the /home directory. On the OSC staff systems, the user HOME directories are located under the /homeb directory.
The top most directory in UNIX is called the root directory, and consists of a number of standard subdirectories grouped according to function.
/ -- The root directory
bin/ -- system programs
sbin/ -- system administration binary executables
boot/ -- file for UNIX boot (operating system kernel) code
etc/ -- system administration files and system configuration
lib/ -- library routines
home/ -- typical location of user home directories
tmp/ -- scratch pad directory for temporary files
usr/ -- more application and system specific information
bin/ -- more user binary executables
lib/ -- even more system shared library files
local/ -- OSC local area for site specific programs and filesbin/ -- local user binary executables (/usr/local/bin)
As you can see from the previous directory tree, there are lots of sub-directories underneath root. There are usually even more on most UNIX systems.
When you use the ls -l command to list the file contained in a directory, those entries which are directories are preceded with a 'd' character. The sample screen below shows this.
total 19 files drwxr-sr-x 3 jmsith 512 Nov 24 12:05 . drwxr-sr-x 46 root 1024 Nov 23 16:46 .. -rw-r--r-- 1 jmsith 2501 Mar 3 1992 .profile drwxr-s--- 2 jmsith 512 Nov 24 12:05 datafiles -rw-r----- 1 jmsith 0 Nov 24 12:05 dirlist -rw-r----- 1 jmsith 24 Nov 5 08:59 temp
Note that datafiles is a sub-directory. There are two other special subdirectory entries in you may see in every UNIX directory:
. the current directory .. the parent directory (upwards towards /)
The common commands used for directory maintenance are:
pwd print current working directory cd change directory mkdir make a subdirectory rmdir remove a subdirectory
pwd (print working directory)
Prints the current working directory on your screen. This is the directory where you are currently located. When you manipulate files and sub-directories, this is where they will (by default) be.
Here is an example of using pwd:
cd (change directory)
This command is used to change the current working directory.
Here are some examples of using cd:
cd datafiles cd .. cd / cd $HOME
The last example shows the use of a UNIX environment variable. The $HOME variable always contains the location of your HOME directory. When an environment variable is used in a command, the contents of the variable and substituted for its name, so the above example would end up doing the same thing as if you typed in:
You can print all of your environment variables by running the "env" command.
mkdir (make directory)
This command makes a sub-directory under the current working directory.
rmdir (remove directory)
This command removes (deletes) a sub-directory under the current working directory. The directory to be removed must be empty of all files and sub-directories.
ls (list files)
This command is similar to DIR in DOS; it displays a list of all the files in the directory. New users don't have any files in their home directory.
Here is an example of using this command;
You will note that the shell prompt reappears, and there is no file listing of the directory contents. This does not mean that there are no files stored there. Just like DOS, UNIX systems support hidden files.
Here is an example of using the command with switches:
The switch l stands for long listing, and the a switch is for all files, including directories and hidden files. UNIX responds with the following listing of the directory
total 6 -rw-rw-r-- 1 jmsmith staff 526 Apr 15 11:03 myletter
Lets explain what all this means.
permission settings | number of links to file | | username of owner | | | group ownership | | | | size of file in bytes | | | | | date/time of last modification | | | | | | filename | | | | | | | v v v v v v v -rw-rw-r-- 1 jmsmith staff 526 Apr 15 11:03 myletter
The first part reveals the permission settings of the files (-rw-r--r--).
Each letter stands for a certain access permission. The letters stand for the following:
d this is a directory r read access w write access x execute access - no access
There are three sets of permission settings. One for the owner of the file, one for the group of the file, and one for everyone else (others). If we examine permission string in the above example, we see that the file, "myletter" is readable and writable by its owner ("jmsmith") and also readable and writable by anyone in the "staff" group. It is only readable by everyone else.
specifies whether this is a directory or a file | permissions for the owner of the file | | permissions for group members | | | permissions for others | | | | v v v v - rw- rw- r--
cat (concatenate files)
This command is used to combine files or to print files to the screen. By default, the cat command sends its output to your screen (in UNIX we call this standard-output or stdout for short).
The following command can be used to view the file, ".profile" on stdout:
The format of this command specifies that cat will use the file, ".profile" as its input, and send the output to your screen.
Note: if the file is very large or is not a plain text file, cat will try to print it to the screen anyway, sometimes with undesirable results. The less command is better suited for viewing files than the cat command.
The cat utility can also be used with the unix shell's output redirection operators to join many files together into a single file. The command:
cat temp .profile > temp1
copies the two files (temp and .profile) to a file called temp1.
The >> is used for appending to the end of an existing file, ie:
cat temp >> temp1
cp (copy files)
This command stands for copy, and is used for copying one file to another.
cp .profile temp2
This copies the file .profile to another called temp2. If temp2 had already existed, its previous contents would've been erased.
Files can also be copied to another directory. The command
cp * /usr/tmp
would copy all the files in the current directory to the directory /usr/tmp.
mv (move files)
The mv command is used for moving or renaming files.
mv temp temp2
This renames the file temp to temp2.
As an example (do not type this!), the command:
mv temp2 /tmp
would move the file temp2 into the directory /tmp (it would no longer appear in your home directory).
rm (remove files)
The rm utility is used for erasing files and directories.
This removes the file. Once a file is removed, it cannot be restored. To cover situations where mistakes might occur, a switch -i appended to this command will request a Yes or No response before deleting the file.
rm -i temp1
NOTE that switches are written before the filenames. Answer Y to the prompt so that temp1 is removed.
nano (simple text editor)
UNIX systems provide many ways to edit text files. There are text editors for every taste, style, and file type. Many of these editors either require a graphical user interface (gedit, kedit) or can be complex to use (emacs, vi). Nano is a somple text editor that works great in a single command line window and is very easy to use.
Nano's use should be pretty self-explanitory. Most every operation involves holding down the control (Ctrl) key and pressing another key. In the help notation within nano, this is signified using the "caret" character (ie: Control and G ==> "^G").
This is used to view the first few lines of a file. It accepts a switch specifying the number of lines to view. The command
head -2 temp
would list the first 2 lines of the file temp on your screen.
This is used to view the last few lines of a file. It accepts a switch specifying the number of lines to view. The command
tail -2 temp
lpr (line printer)
This command is used to obtain a hardcopy printout of a file. It will by default send the file to the system defined default printer.
There are several printers for our systems. To designate which printer to send your file to, use the destination (-d) switch with a printer name. With the switches, the lp command will send the printout to the system's default printer (usually, oscps1)
lpr -d oscpd2 temp
chmod (change access permissions)
This utility allows users to change the access permissions on files.
chmod g+w temp chmod g+rx temp chmod go+rw temp chmod o-rw temp chmod g-rwx temp
This changes the access permissions of the file, "temp" to allow its group to write to it. Warning: Be careful with this command as it can cause you to remove access to your own files! Luckily you can use the same command to restore those permissions. This command basically allows you to specify who can do what to any file that you own.
See the above ls command description for more details on UNIX file permissions.
The access permission can be specified in the following format. The three parts of the format are given with no spaces between them.
- [who] [operator] [permission]
- Who can be any combination of:
- u :user (the file's owner).
- g :group (the file's group members).
- o :others (everyone else).
- Operator can be one of:
- + :add the permissions to the file's existing set.
- - :remove the given permissions from the file's set.
- = :set the permissions to exactly this.
- Permission can be any combination of:
- r :read permission.
- w :write permission.
- x :execute permission for programs.
grep (search for text strings within files)
grep is one of many standard UNIX utilities. It searches files for specified words or patterns. First clear the screen, then type
grep science science.txt
This grep will print out each line containg the word science.
Or has it ????
If you were to type
grep Science science.txt
The grep command is case sensitive; it distinguishes between Science and science.
To ignore upper/lower case distinctions, use the -i switch, i.e. type
grep -i science science.txt
To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type
grep -i 'spinning top' science.txt
Some of the other switches of grep are:
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines
Try some of them and see the different results. Don't forget, you can use more than one switch at a time. For example, the number of lines without the words science or Science is
grep -ivc science science.txt
who (display a list of users)
This command will display users currently on the system.
help (obtain help on command usage)
Most commands provide a short summary of their switches and arguments when you give them the "--help" switch.
To obtain help for the command grep type:
bg (run programs in the background)
This command allows you to run a program in the background while you perform other tasks.
bg <name of job>
fg <name of job>
brings it back to the foreground.
less (page through a text file)
This command allows you to view a text file without fear of accidently modifying it, as you would with a text editor.