URL.biz - where people find experts

 
HOME ARTICLES LIST NOW FOR FREE! ABOUT US CONTACT US LOG IN

Unix Fundamentals Course Module 3 Files/directory
3rd in series of Unix Fundamentals course modules by JayrConsulting Ltd. A FREE copy of this module in MsWord format (including missing diagrams) can be got from john.roberts@jayrconsulting.co.uk

Go to Web Site

UNIX Fundamentals

MODULE 3 – File and Directory Management



You will cover

* Access permissions and ownership of files

* Creating and removing directories

* Using wild cards in file selections

þ Moving, copying, linking and removing files

þ File system searches with the ‘find' command



























































MODULE 3 – CONTENTS

3.1 FILE ACCESS PERMISSIONS 4

3.2 CHANGING OWNERSHIP 5

3.3 CHANGING PERMISSIONS 5

3.3.1 OCTAL MODE NOTATION 5

3.3.2 SYMBOLIC MODE NOTATION 6

3.4 FILE AND DIRECTORY WILD CARDS 7

3.4.1 USE OF ECHO COMMAND 7

3.5 FILE AND DIRECTORY UTILITIES 8

3.5.1 DIRECTORY UTILITIES 8

3.5.2 REMOVING FILES 8

3.5.3 COPYING FILES 9

3.5.4 MOVING AND RENAMING FILES 9

3.5.5 LINKING FILES 10

3.5.6 HARD LINKS 10

3.5.7 SYMBOLIC LINKS 11

3.6 FILE SEARCHES USING FIND 12

3.6.1 EXTENDED USE OF FIND 13

























3.1 File Access Permissions



As far as files are concerned, anyone trying to access them is in one of three categories.

user person deemed to be the current owner

group a member of the same group as the owner

other anyone not in the first two categories

For each of the three categories of file user, a set of three access permission flags exist:- read, write and execute. The access they provide to files and directories is as follows.



Permission File action Directory action

read cat, pg, more etc. Can list contents with ls

write Updates allowed Files may be copied in, removed or renamed

execute Program files may be run Search allowed- i.e. directory may be cd'd to and through





Only the owner of a file (or the ‘superuser') is allowed to :

(1) change file permissions

(2) change the group ownership of a file

(3) give away ownership of a file

On more recent variants of UNIX 5.4, ownership can only be altered by the superuser. This was introduced as an added security restriction.









3.2 Changing Ownership



Providing that current ownership criteria allow the correct access, the ownership of a file can be changed as follows.

chown username filename gives away ownership of file ‘filename' to new user ‘username'

chgrp groupname filename changes group identity of ‘filename' to new group ‘groupname'



3.3 Changing Permissions



Files and directories are created with a default set of permissions, controlled by the system parameter ‘umask'. The owner of a file (or superuser) may change file permissions at any time, using the command ‘chmod'. Permissions may be annotated in one of two ways, using either ‘symbolic' notation or ‘octal' notation.



3.3.1 Octal Mode Notation



Consider a file that is to be given read, write and execute permission for the user, read and execute permission for those in the same group and execute permission only for others. The ‘ls' command would show this as ‘rwxr-x—x'.



If each permission granted is written as a ‘binary one' and each permission not granted is written as a ‘binary zero', this would be shown as ‘111101001' or in octal ‘751'. These permissions could be set as in the following example.



chmod 751 filename













3.3.2 Symbolic Mode Notation

File permissions can also be set using alphanumeric symbols as follows.



u user r read permission

g group w write permission

o others x execute permission

a all the above (u,g,o)

+ add stated permissions, remaining permissions unchanged

- remove stated permissions, remaining permissions unchanged

= set stated permissions, remaining permissions removed

, separator in complex commands





chmod u+x filename add execute permission for the user



chmod ug+x,o-w filename add execute permission for the user and group. Remove write permission for others





3.4 File and Directory Wild Cards

UNIX recognises wild cards when dealing with file and directory names. These match patterns and follow three rules.



* matches any number of any characters

? matches any single character

[chars] matches any single character among those listed between the brackets. A range of characters may also be specified.



3.4.1 Use of echo Command



The echo command repeats command line arguments to the screen and is useful for matching file names using wild cards. For example:-



echo ???? would display all 4 character files from current directory

echo [a-k]* would display all files starting with any letter between ‘a' and ‘k'

echo ?a? would display all 3 character files with a middle letter ‘a'

echo [a-c][Mm]* would display all files where the first letter is ‘a', ‘b' or ‘c', second letter is upper or lower case ‘M', followed by anything else.





Note : The use of the ‘ls' command for this purpose may cause confusion. If the pattern happens to match a sub-directory name, the contents of that directory would also be listed !





3.5 File and Directory Utilities





3.5.1 Directory Utilities



pwd prints full pathname of working (current) directory



cd dirname changes current directory to nominated directory

cd changes current directory to default login directory



mkdir dirname create new directory at designated location



rmdir dirname remove directory, providing it is empty and not in use



3.5.2 Removing Files



rm filename remove designated file



rm –i filename remove file, first checking for confirmation



rm –r dirname remove directory, after first removing all files contained in the directory



Note : New users on UNIX systems are advised to always include the ‘-i' option when using the ‘rm' command.( or replace the rm command with an ‘alias' ) This will ensure that files are not removed from the system without first giving confirmation.





3.5.3 Copying Files



cp filea fileb makes a copy of ‘filea', creating new file called fileb



cp –i filea fileb prompts for approval if ‘fileb' already exists



cp filea /tmp creates new file /tmp/filea



3.5.4 Moving and Renaming Files



mv filea fileb moves ‘filea' to ‘fileb'. In effect a ‘rename'



mv filea /tmp/newfile move and rename ‘filea' to a new file ‘/tmp/newfile'

















3.5.5 Linking Files

Linking is a technique by which two or more file names can be set up to point to the same physical file. This means that instead of several users having their own copy of a file, they can instead have a ‘linked' name in their own directory, which points to the actual file, which may be physically located in a different directory. On most modern UNIX systems there are two types of ‘link' available. These are ‘hard' links and ‘symbolic' links

3.5.6 Hard links

Hard links can only be set up within the same file system or disk partition . Each linked name that is created uses the same inode number and therefore there is only one inode number and one physical set of data, regardless of the number of links set up.



ln /home/jayr/payfile /home/anna/payfile



sets up a ‘link', /home/anna/payfile, which points to the same physical file as /home/jayr/payfile..As both file names share the same inode, they will both have the same file characteristics ( permissions etc ) and share the same set of data. Hard links can not be set to a directory.

















With ‘hard' links, removing any one of the linked names will have no effect on the other linked names or the physical data, other than reducing the ‘link count' in an ‘ls –l' listing of any of the remaining file names. The system does not recognise which was the original file name and therefore ALL of the linked names must be removed in order to remove the physical data.

3.5.7 Symbolic links

Symbolic links, a more recent innovation, are much more flexible in use than hard links. They can be set up to work across file system or disk partition boundaries and can also be set to directories .They are set up with the ‘-s' option to the ‘ln' command and are slightly different in their operation to the hard links described previously.



ln –s /home/jayr/payfile /usr/local/payfile



would create a ‘symbolic' link between ‘payfile' in ‘/home/jayr' and a new file called ‘payfile' in /usr/local, which is in a different file system on the system. However, in this case the new file has its own inode ( because it is in a different file system on the disk ) . In this case, the ‘link' produced is a separate physical file on disk, which contains the name of the file it is linked to and acts as a ‘pointer' to it.

























In this case the original file and the ‘link' both have their own inode and therefore their own set of characteristics ( permissions etc.) .

Also it is possible to remove the original name and thus the physical data, leaving the ‘link' pointing at nothing, causing an error when the link is next accessed. Good housekeeping is therefore essential.



3.6 File searches using find

The find command searches the file systems looking for files which conform to the set of rules given as arguments. Further arguments specify what to do with the files when found.

This may be summarised as follows:



find where to start what to what to do the search look for with files found



Every file hierarchically below the start point will be examined. The remaining arguments are built from a set of objects known as primaries, each distinguished by a leading minus sign. Primaries are used to classify the arguments which follow them. The following examples illustrate typical uses of the find command.



find /usr –print find and list all files below /usr



find / -name test –print find all files below / called ‘test'



find /home –user jo –print find all files below /home owned by ‘jo'

find /home –mtime +30 –print find all files below /home that have not been modified for more than 30 days

find / -size +10 !-user root –print find all files below / that are larger than 10 blocks and do not belong to root



Note: if wildcards are used in ‘find' statements, they must be enclosed in quotes

3.6.1 Extended use of find

-exec

A second command can be invoked by use of the ‘-exec' function. The second command will be executed on each file in turn, as it is located by the ‘find' command. The example below would automatically find and remove all files on the system that belong to ‘bill'.



find / -user bill –exec rm –rf {} ;



As each file is found, it will effectively be placed in the position of the paired braces and acted upon by the rm command running in a sub shell. After each removal, the escaped semicolon returns control to the find command and the search continues.



-ok

The ‘-ok' extension performs the same function as ‘-exec', but additionally makes any following command act interactively. This can be useful when using commands that do not have an interactive option of their own.



find /home/fred !-user fred –ok cat {} ;



The above command would search for all files beneath /home/fred that do not belong to fred, and would then offer to display each one as it is found.



There are extensive options that can be used with ‘find' and the ‘man' pages should be consulted for further information.




 
Other Articles Written By This User


Copyright © 2003 - 2012 URL.biz. All rights reserved.