Lab 1—Unix
Objectives
- Use many common Unix commands
- Learn an editor: VIM or Emacs
Exercise
Step 1 - Find a partner
Buddy-up to help get through the answers faster. You will need to sit next to each other. If there ends up being an extra person, then you can make one group of three.
Step 2 - VM
Ensure you have the latest and greatest Virtual Machine from the CS department. You should have VirtualBox, VM, Dropbox. Open your VM and open a terminal window so we can play with Unix!
Step 3 - Text editors
Unix systems feature a lot of different text editors, such as pico, emacs (prounced “e-macks”, and vi (pronounced “vee-eye”, also known as vim pronounced sounds like “him”).
We do not require a specific text editor in this class, so use whichever program suits your working style best. However, you should experience each one so you are somewhat familiar with them, and you will need to be very familiar with either emacs or vim.
To get you started, you can learn more about these programs by accessing their on-line help and/or tutorials.
Program | Help | Tutorial |
---|---|---|
emacs | Launch emacs, type Ctrl-h | Type emacs, then Ctrl-h t |
vim | Launch vim, type :help | Type vimtutor |
pico | Launch pico, type Ctrl-G | N/A |
Of course, you can always get more information on the Web by performing a search in your favorite search engine using, e.g., a phrase like “pico tutorial”.
While pico is an editor you can use, you should learn either vim or emacs.
The TA will guide you through a brief introduction to vi as well as emacs. Then decide…
For the rest of this lab, use either vim or emacs to record your answers!
Step 4 - Unix commands
Unix commands enable you to do some powerful programming in very few characters. You can either enter the commands at the terminal, or put then in a file and run them as a script.
Nearly all Unix commands follow the same basic structure in how they are entered.
command -option argument --more-options
Using either vim or emacs, create a file with the following information:
What do the following commands do?
Command | Does what? |
---|---|
date |
|
ls -ltr |
|
cd .. |
|
pwd |
|
who |
|
whoami |
|
man man |
|
env |
How would you do the following? (Work with your buddy! Search the internet!) Some useful commands you may need: grep, cd, mkdir, rm, find, head, tail, cp, zip, unzip, rmdir, cat, sort, uniq, less, touch
Command | Goal |
---|---|
Make a directory named cs3308 and move into that directory. |
|
Rename your directory cs3308 to csci3308 . Change to the root directory. |
|
Make a copy of a file. | |
Delete the copy of your file (Careful!) | |
Make a directory named tmp . Then delete that directory. View the contents of a file. |
|
2nd way to view the contents of a file. | |
View just the beginning of a file. | |
View just the end of a file. | |
List all files that contain the word the in the file. |
|
List full path to all files named books.txt |
|
Zip the contents in your directory into a file named dir.zip Unzip your zipped file dir.zip into a new directory named tmp Tar the contents in your directory into a file named dir.tar Untar your zipped file dir.tar into a new directory named tmp |
|
Modify a file’s last modified timestamp to now. This also creates a new file if it doesn’t currently exist. |
Step 5 - Fancy Unix commands
For the next set of questions, use the following file:
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
harpo:x:12502:1000:Harpo Marx:/home/harpo:/bin/csh
chico:x:12501:1000:Chico Marx:/home/chico:/bin/bash
zeppo:x:12505:1000:Zeppo Marx:/home/zeppo:/bin/zsh
groucho:x:12503:2000:Grouch Marx:/home/groucho:/bin/sh
gummo:x:12504:3000:Gummo Marx:/home/gummo:/usr/local/bin/ksh
From Understanding /etc/passwwd File Format
Understanding fields in /etc/passwd
The /etc/passwd file contains one entry per line for each user (or user account) of the system. All fields are separated by a colon (:) symbol. Total seven fields as follows. Generally, passwd file entry looks as follows:
(Fig.01: /etc/passwd file format)
- Username: It is used when user logs in. It should be between 1 and 32 characters in length.
- Password: An x character indicates that encrypted password is stored in /etc/shadow file.
- User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
- Group ID (GID): The primary group ID (stored in /etc/group file)
- User ID Info: The comment field. It allow you to add extra information about the users such as user’s full name, phone number etc. This field use by finger command.
- Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
- Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell.
Command | Does what? |
---|---|
Sort the file based on the userid (first field) | |
Sort the file based on the UID. Since it is a number, be sure to specify that it is a number so it sorts the numbers correctly. | |
Sort first based on GID, then on UID. | |
Show all the lines in the file with `Marx in it. | |
Get the number of lines in the file (from a Unix command) | |
Use your answer from the previous question and now redirect the output to a file named tmp |
|
Now store your answer to the previous question in a separate file with an extension of .sh for example getUniqueGID.sh . Try to run your program. You should get command not found . Try to run ./getUniqueGID.sh You should get Permission denied . Change the permissions on the file to allow users to execute the file. Now, which method runs your program? And why doesn’t the other one execute? And what is a second way to have changed the permissions? And do you know a third way? |
Credit
To get credit for this lab exercise, show the TA your code and run your program.
Lab material by Liz Boese.