UNIX
Reference:
UNIX Concepts and Applications – 4th Edition
By
Basic File Attributes
• ls –l
• File ownership
• File permissions
• chmod
• Directory permission
ls –l (Listing file attribtes)
$ ls –l
ls –l (Listing file attribtes)
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
– File type and Permissions: the first column shows the type and permissions associated with each file. If the first character is a “-”, then it indicates that the file is an ordinary file. For the directories we will see a “d” at the same position.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
– File size: the fifth column shows the size of the file in bytes, i.e., the amount of data it contains.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
– The space occupied by a file on disk is usually larger than this.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
– The two directories show smaller file sizes (512 bytes each).
– The directory maintains a list of filenames along with an identification number (the inode number) for each file.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
– Last modification time: the sixth, seventh and eighth columns indicates the last modification time of the file.
– A file is said to be modified only if its contents have changed.
ls –l (Listing file attribtes)
• ls looks up the file’s inode to fetch its attributes.
File Ownership
• When you create a file, you are the owner of the file.
• Your group is the group owner of the file.
• If you copy someone else’s file, you are the owner of the copy.
File Ownership
• When the system administrator creates a user account, he has to assign these parameters to the users:
– The user-id (UID): both name and numeric representation
File Ownership
• The file /etc/passwd maintains the UID (both the number and name) and GID (only the number).
File Ownership
• To know your UID and GID without viewing /etc/passwd and /etc/group we use
$ id
File Permissions
• Every file and directory in your account can be protected from or made accessible to other users by changing its access permissions.
File Permissions
• To display the access permissions of a file or directory use the command:
ls -l filename (directory)
• This displays a one line summary for each file or directory. For example:
File Permissions
• There are three types of permissions:
r read the file or directory
w write to the file or directory
File Permissions
• Each of these permissions can be set for any one of three types of user:
u the user who owns the file (usually you)
g members of the group to which the owner
belongs
File Permissions
• The access permissions for all three types of user can be given as a string of nine characters:
File Permissions Examples
$ ls -l file1
File Permissions Examples
$ ls -l file2
-rw-r--r–
File Permissions Examples
$ ls -l myprog
-rwx--x—x
File Permissions Examples
$ ls -l
...
drwxr-x---
Summary File Permissions
Permission File Directory
r read read a file list files in ... w write write a file create file in ...
rename file in … delete file …
x execute execute a shell script
read a file in ...
write to a file in ... execute a file in ...
Changing File Permission
• The chmod (change mode) command is used to set permissions of one or more files for all three categories of user (user, group and others).
• It can be run only by the users (the owner) and the superuser.
• The command can be used in two ways: – Relative manner
Changing File Permission
Changing File Permission
• To change the access permissions for a file or directory use the command
chmod mode filename
chmod mode directory_name
Changing File Permission
Who the permissions apply to
• This is specified as one of:
o u (user) the owner of the file
o g (group) the group to which the owner belongs
o o (other) everyone else
Changing File Permission
How the permissions are to be set
This is given as one of:
o + add the specified permission
o - subtract the specified permission
o = assign the specified permission, ignoring
Changing File Permission
The permissions to be set
These are specified by one or more from:
o r read
o w write
o x execute
Changing access permissions using
the chmod command
• To give yourself permission to execute a file that you own:
$ chmod u+x file1
Changing access permissions using
the chmod command
• To give members of your group permission to read a file:
$ chmod g+r file2
Changing access permissions using
the chmod command
• To give read permission to everyone for a particular type of file:
$ chmod a+r *.pub
Changing access permissions using
the chmod command
• To give the group write and execute permission:
$ chmod g+wx $HOME/SCCS
Changing access permissions using
the chmod command
$ ls –l xstart
-rw-r--r--$ chmod u+x xstart $ ls –l xstart
-rwxr--r--Changing access permissions using
the chmod command
$ chmod ugo+x xstart; ls –l xstart -rwxr-xr-x
The previous sequence can be replaced by,
$chmod a+x xstart (a implies ugo)
Changing access permissions using
the chmod command
• chmod accepts multiple filenames in the
command line.
Changing access permissions using
the chmod command
• Permissions are removed with – operator.
$ ls –l xstart -rwxr-xr-x
Changing access permissions using
the chmod command
• chmod accepts multiple expressions delimited
by commas.
-rw-r--r--Changing access permissions using
the chmod command
• More than one permission can also be set;
• u+rwx is a valid chmod expression.
Changing access permissions using
the chmod command
• Absolute permissions: sometimes you don’t need to know what a file’s current permissions are, but want to see all nine permission bits explicitly.
Changing access permissions using
the chmod command
• There is a shorthand way of setting permissions by using octal numbers.
– Read permission is given the value 4,
– write permission the value 2
– Execute permission 1.
r w x
Changing access permissions using
the chmod command
• These values are added together for any one user category:
o 1 = execute only
o 2 = write only
o 3 = write and execute (1+2)
o 4 = read only
o 5 = read and execute (4+1)
o 6 = read and write (4+2)
Changing access permissions using
the chmod command
• So access permissions can be expressed as three digits. For example:
user group others chmod 640 file1 rw- r-- --- chmod 754 file1 rwx r-x r-- chmod 664 file1 rw- rw- r--chmod 000 file1 --- ---
Changing access permissions using
the chmod command
• Using chmod recursively (-R)
• It’s possible to make chmod descend a directory hierarchy and apply the expression to every file and subdirectories its find.
$ chmod –R a+x dir1
Changing access permissions using
the chmod command
• You can provide multiple directory and filenames, and if you want to use chmod on your home directory tree, then “cd” to it and use it in one of these ways:
Changing file ownership
• Changing file ownership means only updating the association between a Unix user and a file, and nothing else.
• When you're changing the owner of a file, no data contained in a file is changed.
Changing file ownership
$ chown nobody file1
In this command, nobody is the username of the new owner for a list of files. In my example, the only file we'd like to change ownership for is file1.
Changing group owner
• By default, the group owner of a file is the group to which the owner belongs.
• The chgrp command changes a file’s group owner.
Changing group owner
• Note: a user can belong to more than one
group, and the one shown in the /etc/passwd is the user’s main group.
• If the user is not the member of the group then only superuser can make the changes.