CSC 105

Handout #2


Changing Access Permissions "chmod":


You can change file permissions with the "chmod" command. In Unix, file permissions, which

specify who may have different types of access to a file, are specified by both access classes and

access types. Access classes determine which users (file owners, limited groups of users, other

users, or all users) may have access to a file. Access types (read, write, and execute) determine

what may be done with the file by those users.


There are two basic ways of using chmod to change file permissions. The first (and probably

easiest) way is the relative (or symbolic) method, which lets you specify access classes and types

with single letter abbreviations. A chmod command with this form of syntax consists of at least

three parts from the following lists:


Access Class: Operator: Access Type:


u (user) + (add access) r (read)

g (group) - (remove access) w (write)

o (other) = (set exact access) x (execute)

a (all)


For example, to add permission for everyone to read a file in the current directory named "myfile",

you would enter at the Unix prompt:


chmod a+r myfile


The "a" stands for "all", the "+" for "add", and the "r" for read. (Note that this assumes that

everyone already has access to the directory where "myfile" is located and its parent directories;

you must set the directory permissions separately). If you omit the access class, it's assumed to be

all, so the previous example could also be entered as:


chmod +r myfile


You can also specify multiple classes and types with a single command. For example, to remove

read and write permission for group and other users (leaving only yourself with read and write

permission) on a file named "myfile", you would enter:


chmod go-rw myfile


You can also specify that different permissions be added and removed in the same command. For

example, to remove write permission and add execute for all users on "myfile", you would enter:


chmod a-w+x myfile


Note that in each of these examples, the access types that aren't specified are unchanged. The

previous command, for example, doesn't changes any existing settings specifying whether users

besides yourself may have read "r" access to "myfile". You could also use the exact form to

explicitly state that group and other users' access is set only to read with the "=" operator:


chmod go=r myfile


The chmod command also operates on directories. For example, to remove write permission for

other users on a subdirectory named "mydir", you would enter:


chmod o-w mydir


To do the same for the current directory, you would enter:


chmod o-w .


Be careful when setting the permissions of directories, particularly your home directory! You don't

want to lock yourself out by removing your own access. Note also that you must have at least

execute permission on a directory to switch (cd) to it.


The other way to use the chmod command is the absolute form. In this case, you specify a set of

three numbers that together determine all the access classes and types. Rather than being able to

just change particular attributes, you must specify the entire state of the file's permissions.


The three numbers are specified in the order: user (or owner), group, other. Each number is the

sum of values that specify read (4), write (2), and execute (1) access, with 0 (zero) meaning no

access. For example, if you wanted to have read, write, and execute permission on "myfile", users

in your group to have read and execute permission, and others to only have execute permission,

the appropriate number would be calculated as (4+2+1)(4+0+1)(0+0+1) for the three digits 751.

You would then enter the command as:


chmod 751 myfile


As another example, to give only yourself read, write, and execute permission on the current

directory, you would calculate the digits as (4+2+1)(0+0+0)(0+0+0) for the sequence 700, and

enter the command:


chmod 700 .


If it seems clearer to you, you can also think of the three digit sequence as the sum of attributes you

select from the following table:


400 read by owner

200 write by owner

100 execute by owner


040 read by group

020 write by group

010 execute by group


004 read by others

002 write by others

001 execute by others


To create an access mode, sum all the accesses you wish to permit. For example, to give read

privileges to all, and write and execute privileges to the owner only for a file, you would sum:

400+200+100+040+004 = 744. Then, at the Unix prompt, you would enter:


chmod 744 myfile.ext


Some other frequently-used examples are:


777 - anyone can do anything (read, write, or execute)

755 - you can do anything; others can only read and execute

711 - you can do anything; others can only execute

644 - you can read and write; others can only read


For more information about chmod, consult the manual page. At the Unix prompt, enter:


man chmod


You can see the access permissions of all the files and directories in a particular directory by making use of the "ls l" option , each line that is displayed is of the form


-rwx------ 1 csc105xx 656 Mar 5 12:05 myfile


[Permissions] [Links] [Owner] [Size(bytes)] [Revision Date] [Name]


The words in [] above specify what each of the columns signifies .

The permissions have 10 spaces , going from the left ,the first space tells whether the entry is for a file or a directory , a directory has a d in this position a file has a - .The next 3 spaces are the access rights for the owner , the next 3 for the group , the last 3 for others .

In order to change the permissions for all the files and subdirectories in a given directory use the command


chmod -R mode_digits filename


Sorting the contents of a file "sort" :


The sort command will sort the contents of a file, in numeric or alphabetic order, and print the

results to standard output (usually the terminal screen). The original file is unaffected. For example,

if "filename" is a file containing a list of words, entering at the Unix prompt:


sort filename


will print them to the screen in alphabetical order (numbers first, then capital words, then lowercase

words). To eliminate any duplicate entries in the list, use:


sort -u filename


To sort case-insensitively, use:


sort -f filename


To sort them case-insensitively and in reverse order, use:


sort -fr filename


As with many Unix commands, you can redirect the output to a new file:


sort filename > newfilename


The output of the sort command will then be stored in a file named "newfilename" in the current



You can also pipe the output of sort into other Unix commands. For example,


sort filename | more


sends the output through the pager "more" for easy reading.

Displaying the first few lines of a file "head" :


Use the Unix command "head" to read the first few lines of an input file and send them to standard

output (i.e., your terminal screen). The format for the head command is:


head -lines file


where "lines" is an optional value specifying the number of lines to be read. If no number is given,

the default value of 10 is used. You may also specify a file for head to read. Otherwise, it will take

its input from "stdin" (standard input: the terminal, or whatever the shell feeds the process with,

usually pipe output).


For example, create a file "alphabetfile" containing the English alphabet with each letter on a separate line, then the



head -3 alphabetfile


would return:






You can also use head with pipes. For example, to see the sizes of the first few files in a large

directory, you could enter at the Unix prompt:


ls -l | head


To save this information into a file in the current directory , you could enter:


ls -l | head > filename


Displaying the last few lines of a file "tail" :


Use the Unix command "tail" to read from standard input or a file and send the result to standard

output (i.e., your terminal screen). The format for using the tail command is:


tail [ +-[number][lbcr] ] [file]


Everything in brackets is an optional argument. If no filename is given, standard input is used.


Tail begins at distance +number from the beginning, or -number from the end of the input. The

number is counted in units of lines, blocks, or characters, according to the appended options -l, -b,

or -c. When no units are specified, tail operates based on lines.


Specifying -r causes tail to print lines from the end of the file in reverse order. The default for -r is

to print the entire file this way. Specifying -f causes tail not to quit at end of file, but rather to reread

the file repeatedly (useful for watching a "growing" file such as a log file).


For example, using the alphabetfile:


tail -3 alphabetfile


would report:








Sample Exam Questions :

( You are required to do these questions in your Unix Account and also type in the answers in a text editor , you should then turn in the assigment as specified by me , most probabily electronically . All the files created in this assigment should be present in your directory hw3 )

  1. The cut command selects the field specified by the f option and distinguishes fields by the delimiter character specified by the -d option, a space in this example. The example given below will print only the first word of each line.

    sort filename | cut -f1 -d" "


    Can you use this knowledge of cut and sort to create a file "words" which contains a list of all the first words of lines in myfile sorted in alphabetic order ?


  3. For me to access your homework directories I need access to your homework subdirectories , you need to give me access to these subdirectories and all other files in your account in order to do this
  4. You need to grant yourself and others all access rights to all the files and directories in your account . Keep in mind that nobody in your group should have any access permissions to any of the files or subdirectories in your account .

    In order to this , first go to your parent directory by typing in

    nova% cd ..



    Now type in the command which will recursively grant the access permissions given above .

    NOTE : This has to be done before you leave the classroom so that I can grade your homework if you think you cant do it let me know so that I can help you with it . Before you leave the class be sure to use ls l to display the access rights for all the files and sub directories so that I can be sure of grading your work !



  5. Create a file mylist that contains the first 7 alphabets followed by the last 7 alphabets from the file alphabetfile . Which command did you use to do this ?
  6. I will send each of you an E_mail ( my userid is csc10502 ) with a line in it , you are required to read this this mail and then use this line to answer the question given below . Delete any other mails that you may be having in your mailbox after you read them ( these are most probabily left over from the previous semester , and dont belong to you anyway ) .
  7. Use Pine to send me an E-mail to me ( my userid is csc10502 ) , The body of this message should have the fillowing text , "This is the solution for question 5 in hw 3", followed by the line that you received in the E-mail from me . The subject of this E-mail should be " Homework 3" .
  8. Create a file junk , which contains some text in it , now set the access permissions of this file such that only you can read, write , and execute it , those in group and others can only read and execute it .
  9. In the last two assigments you have learnt quite a lot of Unix commands , In order to crystallize what we have already learnt , I would like you to make a file my_unix_commands which contains a list of all the unix commands that you have already learnt . Write a one line description of each of the unix commands , if you know any options that can be used with these commands list them also on a separate line each with a one line description of what they do .
  10. Sometime after Monday you will have to turnin the files "my_unix_commands" , "words" and "mylist" . You will not be able to this immediately in class as I have to write certain files to your standard system files , and I can only do this after you give me permission to do so .

You will be using the following command every time that you have to turn_in some homework to me.


nova% turnin filename


You can resubmit files as long as the deadline has not been reached , but once the deadline is over your files will be submitted to a directory which is meant for the next homework ,and hence will not be graded as late . Homeworks which are turned in more than a week late will not be accepted .


If you have any problems using the above means of homework submission , contact me in the next class . And instead hand in a printed copy of the above files . I am not responsible for when you take the print out in this case , and printing during class or lab hours is not permitted .