This is not a test. Try to do the exercises on your own, but if you get stuck, ask for help.
There are 3 levels of file protection in UNIX: owner, group, other
You can set 3 types of permission for each level: read, write, execute
read == can read the fileAn single integer is used to describe each level of protection. Thus file permissions can be described by three integers
write == can write the file
execute == if it is a directory, can cd to the directory
if it is a file, can execute a file (not all files can be executed)
one integer for the owner
one integer for the group (each file has an attribute which names which group it belongs to)
one integer for the other
Since there are 3 levels of permission (read, write, execute) described by each of these integers, we need an encoding method. That method is as follows
if the file is executable, add a 1 to the numberHere are the most common file protections:
if the file is writable, add a 2 to the number
if the file is readable, add a 4 to the number
$ chmod 644 the owner can read/write, the group and world can only readIf you want to make sure no one can read ANY of your files, do the following:
$ chmod 755 the owner can read/write/execute, the group and world can read and execute
$ chmod 700 the owner can read/write/execute, the group and world have no access
$ chmod 600 the owner can read/write, the group and world have no access
$ cdThe first command takes you to your home directory.
$ chmod 700 .
The second command sets the protection on the current directory (.) so that only you (the owner) have access.
(1) Create a directory for your 112 labs. You may choose the name and structure. Here are some suggestions (remember that the ~ stands for your home directory).
Hint: use mkdir to create a directory. You have to create each directory along the way. For example,
- ~/classes/112/labs/lab1 ~/classes/112/labs/lab2 ...
- ~/classes/112/labs/1 ~/classes/112/labs/2 ...
- ~/112/labs/lab1 ~/112/labs/lab2 ...
- ~/1 ~/2 ... (this is a joke, these names in your home directory would not be very meaningful)
if you choose ~/classes/112/labs/lab1 as your naming convention, you have to call mkdir 4 times.
In your directory for lab 2, create the file hello.cpp (using any text editor, pico is the easiest to use). Edit hello.cpp so it contains the following text.
#include <iostream>Don't use cut & paste, I want you to practice using the editor.
using namespace std;
int
main()
{
cout << "hello world" << endl;}
return 0;
Compile this file using the command:
$ g++ hello.cppThis should have created the file a.out, use "$ ls -l" to find out if a.out is in your directory. Notice that a.out automatically has executable protection.
if you got an error, use the editor to fix the error
Run your hello program. You can execute the program by typing a.out at the UNIX prompt.
$ a.outIf a.out doesn't work, try ./a.out and ask for help fixing your environment so you don't need the "./".
Run your hello program and redirect the output to the file hello.out using the shell's redirection symbol > (see lecture notes from lab1 for the redirect systax)
You can view hello.out using an editor or the cat command (cat simply writes the files passed to it to standard output).
$ cat hello.out
Modify your program to prompt for and read a user's name as follows.
#include <iostream>Compile and run this program (using the same g++ command above).
#include <string>
using namespace std;
int
main()
{
string name;
cout << "What is your name? ";
cin >> name;
cout << "hello " << name << endl;
return 0;
}
Now create a file with a name in it (call it name_file). You can do this using an editor such as pico.
Using redirection, read from this file and write to another
$ a.out < name_file > hello.out
Check the hello.out to make sure it is correct.
Q1: What UNIX command deletes a file? answer
Delete the file: hello.out
Execute the following command:
$ man -k removeWhat happened? Too fast? try:
$ man -k remove | lessThere are 88 entries, sometimes this trick doesn't work?
How do you think I figured out there were 88? There is a program that counts lines, words, and characters called wc.
Q2: How could you take the output from man and pass it to wc? answer
How many entries are there for permission
$ man -k permission