0% found this document useful (0 votes)
4 views7 pages

Understanding UNIX File Access Rights

Uploaded by

renashadahal181
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Understanding UNIX File Access Rights

Uploaded by

renashadahal181
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial 4

Access Rights
In your myfiles directory, type
ls -l
(l for long listing!), This shows a lot more information presented to the user than the standard command.
You will see that you now get lots of details about the contents of your directory, similar to the example
below.

-rw-r--r-- 1 MHAldar1 1049089 58619 Oct 27 17:12 [Link]


File owner file size creation, creation file name
Date Time
Each file (and directory) has associated access rights, which may be found by typing ls -l.
Also, ls -lg gives additional information as to which group owns the file (beng14 in the following
example):

1
-rwxrw-r-- 1 ee51ab beng14 2450 Sept29 11:52 file1

10 symbols

In your case, you are unable to see the group because might you use an individual account.

In the left-hand column is a 10 symbol (drwxr-xr-x 1 / -rw-r--r-- 1) string consisting of the symbols d,
r, w, x, -, and, occasionally, s or S.
1. If d is present, it will be at the left-hand end (first symbol) of the string and indicates a directory:
otherwise - will be the starting symbol of the string.

2. The 9 remaining symbols indicate the permissions, or access rights, and are taken as three
groups of 3.
• The left group of 3 gives the file permissions for the user that owns the file (or
directory) (ee51ab in the above example);
• The middle group gives the permissions for the group of people to whom the file (or
directory) belongs (beng14 in the above example);
• The rightmost group gives the permissions for all others.

The symbols r, w, etc., have slightly different meanings depending on whether they refer to a simple file
or to a directory.
Access rights on files.
• r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to
read and copy the file
• w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to
change a file
• x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file,
where appropriate
Access rights on directories.
• r allows users to list files in the directory;
• w means that users may delete files from the directory or move files into it;
• x means the right to access files in the directory. This implies that you may read files in the
directory provided you have read permission on the individual files.
So, in order to read a file, you must have execute permission on the directory containing that file, and
hence on any directory containing that directory as a subdirectory, and so on, up the tree.
Some examples

-rwxrwxrwx a file that everyone can read, write and execute (and delete).

a file that only the owner can read and write - no-one else
-rw------- can read or write and no-one has execution rights (e.g. your
mailbox file).

2
Changing access rights
chmod (changing a file mode)
Only the owner of a file can use chmod to change the permissions of a file.

The options of chmod are as follows

Symbol Meaning

u user

g group

o other

a all

r read

w write (and delete)

x execute (and access directory)

+ add permission

- take away permission

For example, to remove read write and execute permissions on the file biglist for the group and others,
type
chmod go-rwx biglist
This will leave the other permissions unaffected.

To give read and write permissions on the file biglist to all,


chmod a+rw biglist

3
In my estimation, a simpler way is to assign read, write and execution, the values 4, 2 and 1
respectively. i.e

Owner Group Others

read write execute read write execute read write execute

4 2 1 4 2 1 4 2 1

Therefore, to read, write and execute = 4 + 2 + 1 =7


To read and execute = 4 + 1 = 5
To read only = 4
To read and write = 4 + 2 = 6

Hence the command for the owner to have read, write and execute permissions, the group to have
read and execute permissions and others to have only execute permissions is:
chmod 751 filename

command can be used to view file


permission in octal notation

stat -c "%a %n" [Link]

Exercise 1
Try changing access permissions on the file [Link] and on the directory backups
Use ls -l to check that the permissions have changed.

Other useful UNIX commands


quota
(All students are allocated a certain amount of disk space on the file system for their personal files,
usually about 100Mb. If you go over your quota, you are given 7 days to remove excess files.) if you are
using the lab computers.

4
To check your current quota and how much of it you have used, type
quota -v

df
The df command reports on the space left on the file system. For example, to find out how much space is
left on the fileserver, type
df .

To view disk space in human readable format pass the -h option.


df -h

du
The problem with just typing du -h (where -h is human readable), is that it prints every file on the
system. However, if you type:
du -h /users/brianthompson/myfiles
It will just list the contents of myfiles (providing you replace the path above with your own path).
The du command outputs the number of kilobytes used by each subdirectory. Useful if you have gone
over quota and you want to find out which directory has the most files. In your home-directory, type
du -s *
The -s flag will display only a summary (total size) and the * means all files and directories.
du –h dirname will give the size in megabytes.
If you want to know more about the file system in Linux you can look at fstab. The fstab (or file systems
table) file is a system configuration file commonly found at /etc/fstab on Linux. The fstab file typically
lists all available disk partitions and other types of file systems and data sources that are not necessarily
disk-based, and indicates how they are to be initialised or otherwise integrated into the larger file system
structure. Modern Linux systems use udev as an automounter to handle the hot swapping of devices
(such as MP3 players or digital cameras) instead of relying on fstab.
gzip
This reduces the size of a file, thus freeing valuable disk space. For example, type
ls -l [Link]
and note the size of the file using ls -l . Then to compress [Link], type
gzip [Link]
This will compress the file and place it in a file called [Link]
To see the change in size, type ls -l again.
To expand the file, use the gunzip command.
gunzip [Link]

5
zcat
zcat will read gzipped files without needing to uncompress them first.
zcat [Link]
If the text scrolls too fast for you, pipe the output though less .
zcat [Link] | less

file
file classifies the named files according to the type of data they contain, for example ascii (text), pictures,
compressed data, etc.. To report on all files in your home directory, type
file *

diff
This command compares the contents of two files and displays the differences. Suppose you have a
file called file1 and you edit some part of it and save it as file2. To see the differences type
diff file1 file2
Lines beginning with a < denotes file1, while lines beginning with a > denotes file2.

The output
find
This searches through the directories for files and directories with a given name, date, size, or any
other attribute you care to specify. It is a simple command but with many options - you can read the
manual by typing man find.
To search for all files with the extension .txt, starting at the current directory ( . ) and working through
all sub-directories, then printing the name of the file to the screen, type
find . -name "*.txt" -print
To find files over 1Mb in size, and display the result as a long listing, type
find . -size +1M -ls

UNIX Variables
Variables are a way of passing information from the shell to programs when you run them.
Programs look "in the environment" for particular variables and if they are found will use the values
stored. Some are set by the system, others by you, yet others by the shell, or any program that loads
another program.
Standard UNIX variables are split into two categories, environment variables and shell variables. In
broad terms, shell variables apply only to the current instance of the shell and are used to set short-
6
term working conditions; environment variables have a farther-reaching significance, and those set at
login are valid for the duration of the session. By convention, environment variables have UPPER
CASE and shell variables have lower case names.

Environment Variables
An example of an environment variable is the OSTYPE variable. The value of this is the current operating
system you are using. Type
echo $OSTYPE
More examples of environment variables are
• USER (your login name)
• HOME (the path name of your home directory)
• PATH (the directories the shell should search to find a command)
• TERM (X terminal type)
• PS1 (prompt)
• SECONDS (Defines current shell life in seconds

Finding out the current values of these variables.


ENVIRONMENT variables are set using the setenv command, displayed using the printenv or env
commands, and unset using the unsetenv command.
To show all values of these variables, type
printenv | less
Type Q to quit.

Shell Variables
An example of a shell variable is the history variable. The value of this is how many shell commands to
save, allow the user to scroll back through all the commands they have previously entered. Type
history
More examples of shell variables are
• cwd (your current working directory)
• home (the path name of your home directory)
• path (the directories the shell should search to find a command)
• prompt (the text string used to prompt for interactive commands shell your login shell)

You might also like