Handout # 1
UNIX is a very powerful multi-user operating system that runs on systems which range from ordinary personal computers (pc) to high-end computers. By operating system, we mean that software program that translates a user's command into a series of binary codes that a computer understands. Some other common operating systems are MS DOS, and the Macintosh OS. The operating system is not to be mistaken for application software, such as WordPerfect or MS Excel.
Applications run over an operating system, translating user commands into codes that the operating system understands. The operating system then translates those commands into codes that the computer understands. UNIX is multi-user, meaning that more then one user can be using the same operating system at one time. This is why you have to login and logout of a UNIX machine.
The file structure of UNIX is very similar to MS DOS. This is because both operating systems use a text-based means of making, moving and deleting directories and files. For DOS users, most of what will be covered in this section will seem like DOS, with different commands. For Macintosh users, this material may seem odd, but just follow along, it's all the same things, except you have to type in your commands instead of pointing and clicking with your mouse.
The UNIX operating system is case-sensitive, unlike the popular MS-DOS operating system for PCs. This means that FILE, File and file refer to three separate items within the structure. Make sure that you are using the correct names and case for commands and files. Most UNIX commands are all lowercase, while user files may be upper or lowercase.
The basic form of any Unix command is:
command_name options argument(s)
Most descriptions for commands such as those given in the On-line Manual use a much more precise syntax. For example:
cp [-iprR] filename ... directory
To understand the command syntax apply the following simple rules:
1.Any options or arguments enclosed in [ ] square brackets are optional.
2.Anything not enclosed in [ ] square brackets must be entered.
3.Arguments like "filename" and "directoryname" must be replaced by whatever it is that they represent. This is usually the name of a file or directory.
4.Ellipses '...' mean that the previous argument can be repeated any number of times.
nova % lpr –Php4si filename
nova% lpr –Php3 filename
For printing out on the printers , you are required to provide your own paper !
Changing your Password "passwd":
1.From the UNIX system prompt, type the following:
nova % passwd
2.You will be prompted with the following commands:
Repeat new password:
3.If you incorrectly type the password when prompted to repeat the new password, the system will return an error message, and your password will remain unchanged.
Choosing a Secure Password
It is imperative that each user has a password that cannot be easily guessed by others. The following list provides guidelines that should be considered when choosing a password:
Passwords should have at least six characters, containing both upper/lower case letters, numbers and/or punctuation. For example: aiw963-bru or
Choose words that are nonsensical, and not dictionary words.
Avoid these common mistakes when choosing your password - any of the following criteria can be easily guessed by persistent hackers or password cracking programs:
Passwords based on user's account name, or the name of a friend, family or spouse.
Purely numeric passwords, such as telephone numbers, house address, birthdays, etc.
Passwords based on common keyboard sequences like qwerty, aaaaa, asdfjkl, etc.
Listing your Files and Directories "ls":
The first command to try and use is the ls command. This command lists all the files and sub-directories in your current directory (in this case your home directory.) There are several options you can use along with the ls command:
The -l option gives complete information on available files.
The -a option lists files that are generally not shown to the user.
The -s option provides the block sizes for the files listed.
The -F option tells you if items are files, directories, or executable.
Any combination of these can be combined into one option string
The command ls –lasF will list all the information about the files in the directory.
However, the most important one is the F option. This option will help you identify the contents of your directory by placing an* after executables, / after directories, and nothing after a regular file. Try this command by typing the following at your prompt, followed by the enter key:
Your screen should look something like this:
nova% ls -F
This indicates that there is a file called "myfile" ( you must have created this file using vi as part of your first assigment ) and a directory called "mail" in your home directory. This directory "mail" is used by PINE to store all your saved mail. If you don't have this directory, it's because you have never launched PINE before. There may be some other files in your directory, the contents of a user's directory differs from person to person.
Using Wild Cards :
Wild cards could be used to find a set of files with the same extension. For example to list all files beginning with "my" type ls my*
Your screen should look like this
nova% ls my*
Typing in ls * fi* would display a list of all files which had the phrase "fi" in them.
Wild cards can be used not just with ls but with any Unix command that takes a file or a directory as an argument and can be used to denote a set of files .
Displaying the contents of your files "more" and "cat" :
In order to view a file on the screen without entering the editor we use more.
more displays the content of a file on your screen.
The syntax is:
When the screen fill with information, the computer will prompt you with --More--(xx%) at the bottom of the screen. Press the space bar to read the next screen of information. The xx% indicates how much of the file you have read so far.
Another command which can be used for displaying the contents of a file is the cat command , however it can also be used for other functions as shown below.
nova% cat .profile
nova% cat > catfile
nova% cat .profile .Xdefaults .Xauthority > .mybakup
nova% cat .profile >> test.123
Piping and redirection :
This constitutes one of the great advantages of Unix . In order to understand piping and redirection we first need to understand the following terms , stdinput – This refers to the primary input source to the system , in general this refers to your keyboard , stdoutput – This refers to the primary output of the system , in general this refers to the screen or the monitor , stderr - This refers to the file where the errors if any which are encountered are logged to .
Unix Commands in general take in the input from the stdinput and display the output to the stdoutput .However they can be made to take input from a particular file by making use of "<" , for example cat could be used to display the contents of a file in the following manner also in addition that given above
nova% cat < myfile
Also instead of displaying the output to the screen the system can be told to save it to a file like
nova% cat > catfile
Now what ever is typed on the screen is saved to the file catfile . When you have finished typing press "^d" to save the file and end the cat command .
Note : Once an enter key has been pressed for a particular line that line cannot be modified , you will have to use vi to do this .
If you want to send both the stdoutput and the stderr to the same file then you could have written
nova% cat >& catfile
Now both stdoutput and stderr ( if any errors are encountered ) are saved to catfile .
Piping can also be used to send the output of one command to the input of another command by using the symbol "|" between successive commands .
This can be used for example when you want to see which files are there in your directory and the ls command causes the screen to scroll past too quickly for you to see the result . what you can do in this case is to pipe the result of the ls command to more so that it can be displayed in a more user friendly way .
This is done as given below
nova% ls –l | more
Creating and Manipulating Files and Directories :
Creating a subdirectory "mkdir" :
Subdirectories are useful tools for organizing one's work. It is common, for example, to use separate subdirectories for separate homework assignments.
Since you have already created a file, the next step will be to create a directory. The syntax for creating a directory in UNIX is:
At your prompt, type mkdir mydir followed by enter. You will now see the prompt again. At the prompt, type in ls -F to check the contents of your home directory. You should now see the following:
mail/ mydir/ myfile
Moving between directories "mv" :
You can move into your new sub-directory by issuing the cd command. The syntax for this command is:
Go ahead and change into mydir by typing in cd mydir at your UNIX prompt. If you check the contents in this directory, you will see that it is empty. You can move back up into your home directory by typing cd .. at your prompt. Go ahead and do this. If you are not sure where you are, you can always issue the pwd command. Typing "pwd" tells you where you are in the directory tree. This is handy because it is easy to get lost. "pwd" prints out the complete "path" to where you are, starting from the root. For example if you are a student, and you type pwd while you are in your home directory your screen should look like:
If you issue the pwd command and notice that you are lost, just type in cd ~yourID. The ~ means home directory of. This command will bring you back into your home directory. You could also do the same thing by typing in cd as given below and the screen will look like :
Copying a file or a directory "cp":
Now the next command to try is the cp command. This command is the copy command, and has the following syntax:
cp file1 file2
cp directory1 directory2
Where cp will copy file1 into file2. If file2 already exists, then cp will overwrite it, so be careful.
A useful option of cp is cp –i file1 file2 , If file2 already exists then the message "file2 already exists is displayed and the file is not copied .
To test this command, lets copy myfile into myfile1. In fact, let's copy myfile into the directory mydir and call it myfile1.
At your UNIX prompt, type in cp –i myfile mydir/myfile1 followed by enter. If you change into your mydir directory, and issue the ls -F command, you will see that myfile1 has been created. If you open up myfile1 with vi , you see that its contents are the same as myfile.
When copying directories
cp –r directory1 directory2
can be used to copy directory1 to directory2 recursively , ie all the subdirectories and files in each one of the subdirectories of directory1 are also copied into directory2.
Deleting a file or Directory "rm" and "rmdir" :
A word of caution before we go further recovering deleted files in Unix can only be done by the system administrator if at all it can be done unlike on a pc , hence be certain that you really want to delete a certain file before remove it . Also as with any other Unix command that takes a file or a directory as an argument when deleting files, so also can wild cards be used with rm and rmdir, however this is strongly discouraged as you may accidentally delete more files than you initially anticipated. If you do want to use wild cards then at least use the same wild card with ls to at least see what files are going to be deleted before you delete them .
The next command to learn now is the delete commands. To delete a file, you use the rm command ; to delete a directory, you use the rmdir command. When deleting a directory, you have to make sure that it is empty. Their syntax is as follows:
Execute the cd ~ command to get back to your home directory. At your prompt, type rm mydir/myfile1 followed by enter.
At your next prompt, type in rmdir mydir followed by enter. You have now deleted a file and a directory.
Note that you don't ever have to switch into the directory before you delete a file from there. This is true for all UNIX commands. You can create, edit, and delete files by stating the path to a file from where you current are. For example, when we deleted myfile1, the mydir/ part was the path to myfile1 from your home directory.
Since each directory in Unix can contain further subdirectories , deleting a directory which has a lot of subdirectories each containing a lot of files can be really tedious, hence an option has been provided in Unix to make this task simple .
rmdir –r directory name
This will recursively delete all the files in each of the subdirectories , delete the now empty sub directories and finally delete the directory to be deleted .
NOTE Extreme caution must be exercised before this command is executed since entire branches of the directory tree are chopped of.
Moving a file "mv" :
There is one more command in UNIX that may be useful. It is the mv command, the move command. The syntax is as follows:
mv file1 file2
This command copies file1 to file2, and then deletes file1. Like any other UNIX command, you can specify a path in front of file1 or file2 instead of changing directories.
Getting Online Help "man" :
There is an extensive on-line help for the commands in UNIX. To get help on a specific command use the man command.
The syntax is as follows:
For example, man ls would tell you about the ls command.
If you are unsure of a command name, you sometimes can find the command by using a search by keyword with man –k keyword
Sample Exam Questions : ( you are required to solve these questions in your account , if you need to write something down , try doing it with vi and turn in the printout .)
1) Create a file named "output_cat" which contains all the information about all the files and subdirectories in your main directory by pressing as few keys as possible ? Which command did you use to achieve this result ?
-l count the number of lines in the input
-w count the number of words in the input
-c count the number of characters in the input
Can you use this command to count the number of lines , words , characters which are displayed when you type in the command "ls –l" in your directory hw2 . Do you think this value is correct?
Can you verify that it is indeed correct , how did you do this ?