quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif The UNIX filesystem

The UNIX filesystem is laid out as a hierarchical tree structure which is anchored at a special top-level directory known as the root (designated by a slash '/'). Because of the tree structure, a directory can have many child directories, but only one parent directory. Fig. below illustrates this layout.

../images/scripting/unix_dir_tree.gif
   

space.gif

  ../images/main/bulllet_4dots_orange.gif UNIX directory structure
   

space.gif

Directory

Typical Contents

/

The "root" directory

/bin

Essential low-level system utilities

/usr/bin

Higher-level system utilities and application programs

/sbin

Superuser system utilities (for performing system administration tasks)

/lib

Program libraries (collections of system calls that can be included in programs by a compiler) for low-level system utilities

/usr/lib

Program libraries for higher-level user programs

/tmp

Temporary file storage space (can be used by any user)

/home or /homes

User home directories containing personal file space for each user. Each directory is named after the login of the user.

/etc

UNIX system configuration and information files

/dev

Hardware devices

/proc

A pseudo-filesystem which is used as an interface to the kernel. Includes a sub-directory for each active program (or process).

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Directory and file handling commands

This section describes some of the more important directory and file handling commands.

   

space.gif

pwd (print [current] working directory)

   

space.gif

pwd displays the full absolute path to the your current location in the filesystem. So

   

space.gif

$ pwd

/usr/bin

   

space.gif

implies that /usr/bin is the current working directory.

   

space.gif

ls (list directory)

   

space.gif

ls lists the contents of a directory. If no target directory is given, then the contents of the current working directory are displayed. So, if the current working directory is /,

   

space.gif

$ ls

bin dev home mnt share usr var

boot etc lib proc sbin tmp vol

   

space.gif

Actually, ls doesn't show you all the entries in a directory - files and directories that begin with a dot (.) are hidden (this includes the directories '.' and '..' which are always present). The reason for this is that files that begin with a . usually contain important configuration information and should not be changed under normal circumstances. If you want to see all files, ls supports the -a option:

   

space.gif

$ ls -a

   

space.gif

Even this listing is not that helpful - there are no hints to properties such as the size, type and ownership of files, just their names. To see more detailed information, use the -l option (long listing), which can be combined with the -a option as follows:

   

space.gif

$ ls -a -l

(or, equivalently,)

$ ls -al

   

space.gif

   

space.gif

Each line of the output looks like this:

   

space.gif

../images/scripting/unix_file_details.gif

where:

   

space.gif

Field

Description

type

type is a single character which is either 'd' (directory), '-' (ordinary file), 'l' (symbolic link), 'b' (block-oriented device) or 'c' (character-oriented device).

permission

permissions is a set of characters describing access rights. There are 9 permission characters, describing 3 access types given to 3 user categories. The three access types are read ('r'), write ('w') and execute ('x'), and the three users categories are the user who owns the file, users in the group that the file belongs to and other users (the general public). An 'r', 'w' or 'x' character means the corresponding permission is present; a '-' means it is absent.

links

links refers to the number of filesystem links pointing to the file/directory (see the discussion on hard/soft links in the next section).

owner

owner is usually the user who created the file or directory.

group

group denotes a collection of users who are allowed to access the file according to the group access rights specified in the permissions field.

size

size is the length of a file, or the number of bytes used by the operating system to store the list of files in a directory.

date

date is the date when the file or directory was last modified (written to). The -u option display the time when the file was last accessed (read).

name

name is the name of the file or directory.

   

space.gif

ls supports more options. To find out what they are, type:

   

space.gif

$ man ls

   

space.gif

man is the online UNIX user manual, and you can use it to get help with commands and find out about what options are supported. It has quite a terse style which is often not that helpful, so some users prefer to the use the (non-standard) info utility if it is installed:

   

space.gif

$ info ls

   

space.gif

cd (change [current working] directory)

   

space.gif

$ cd path

   

space.gif

changes your current working directory to path (which can be an absolute or a relative path). One of the most common relative paths to use is '..' (i.e. the parent directory of the current directory).

   

space.gif

Used without any target directory

   

space.gif

$ cd

   

space.gif

resets your current working directory to your home directory (useful if you get lost). If you change into a directory and you subsequently want to return to your original directory, use

   

space.gif

$ cd -

   

space.gif

mkdir (make directory)

   

space.gif

$ mkdir directory

   

space.gif

creates a subdirectory called directoryin the current working directory. You can only create subdirectories in a directory if you have write permission on that directory.

   

space.gif

rmdir (remove directory)

   

space.gif

$ rmdir directory

   

space.gif

removes the subdirectory directory from the current working directory. You can only remove subdirectories if they are completely empty (i.e. of all entries besides the '.' and '..' directories).

   

space.gif

cp (copy)

   

space.gif

cp is used to make copies of files or entire directories. To copy files, use:

   

space.gif

$ cp source-file(s) destination

   

space.gif

where source-file(s) and destination specify the source and destination of the copy respectively. The behaviour of cp depends on whether the destination is a file or a directory. If the destination is a file, only one source file is allowed and cp makes a new file called destination that has the same contents as the source file. If the destination is a directory, many source files can be specified, each of which will be copied into the destination directory. Section 2.6 will discuss efficient specification of source files using wildcard characters.

   

space.gif

To copy entire directories (including their contents), use a recursive copy:

   

space.gif

$ cp -rd source-directories destination-directory

   

space.gif

mv (move/rename)

   

space.gif

mv is used to rename files/directories and/or move them from one directory into another. Exactly one source and one destination must be specified:

   

space.gif

$ mv source destination

   

space.gif

If destination is an existing directory, the new name for source (whether it be a file or a directory) will be destination/source. If source and destination are both files, source is renamed destination. N.B.: if destination is an existing file it will be destroyed and overwritten by source (you can use the -i option if you would like to be asked for confirmation before a file is overwritten in this way).

   

space.gif

rm (remove/delete)

   

space.gif

$ rm target-file(s)

   

space.gif

removes the specified files. Unlike other operating systems, it is almost impossible to recover a deleted file unless you have a backup (there is no recycle bin!) so use this command with care. If you would like to be asked before files are deleted, use the -i option:

   

space.gif

$ rm -i myfile

rm: remove 'myfile'?

   

space.gif

rm can also be used to delete directories (along with all of their contents, including any subdirectories they contain). To do this, use the -r option. To avoid rm from asking any questions or giving errors (e.g. if the file doesn't exist) you used the -f (force) option. Extreme care needs to be taken when using this option - consider what would happen if a system administrator was trying to delete user will's home directory and accidentally typed:

   

space.gif

$ rm -rf / home/will

   

space.gif

(instead of rm -rf /home/will).

   

space.gif

cat (catenate/type)

   

space.gif

$ cat target-file(s)

   

space.gif

displays the contents of target-file(s) on the screen, one after the other. You can also use it to create files from keyboard input as follows (> is the output redirection operator, which will be discussed in the next chapter):

   

space.gif

$ cat > hello.txt

hello world!

[ctrl-d]

$ ls hello.txt

hello.txt

$ cat hello.txt

hello world!

$

   

space.gif

more and less (catenate with pause)

   

space.gif

$ more target-file(s)

   

space.gif

displays the contents of target-file(s) on the screen, pausing at the end of each screenful and asking the user to press a key (useful for long files). It also incorporates a searching facility (press '/' and then type a phrase that you want to look for).

   

space.gif

You can also use more to break up the output of commands that produce more than one screenful of output as follows (| is the pipe operator, which will be discussed in the next chapter):

   

space.gif

$ ls -l | more

   

space.gif

less is just like more, except that has a few extra features (such as allowing users to scroll backwards and forwards through the displayed file). less not a standard utility, however and may not be present on all UNIX systems.

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Specifying multiple filenames

Multiple filenames can be specified using special pattern-matching characters. The rules are:

   

space.gif

  • '?' matches any single character in that position in the filename.
  • '*' matches zero or more characters in the filename. A '*' on its own will match all files. '*.*' matches all files with containing a '.'.
  • Characters enclosed in square brackets ('[' and ']') will match any filename that has one of those characters in that position.
  • A list of comma separated strings enclosed in curly braces ("{" and "}") will be expanded as a Cartesian product with the surrounding characters.

For example:

   

space.gif

  • ??? matches all three-character filenames.
  • ?ell? matches any five-character filenames with 'ell' in the middle.
  • he* matches any filename beginning with 'he'.
  • [m-z]*[a-l] matches any filename that begins with a letter from 'm' to 'z' and ends in a letter from 'a' to 'l'.
  • {/usr,}{/bin,/lib}/file expands to /usr/bin/file /usr/lib/file /bin/file and /lib/file.
   

space.gif

Note that the UNIX shell performs these expansions (including any filename matching) on a command's arguments before the command is executed.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Quotes

As we have seen certain special characters (e.g. '*', '-','{' etc.) are interpreted in a special way by the shell. In order to pass arguments that use these characters to commands directly (i.e. without filename expansion etc.), we need to use special quoting characters. There are three levels of quoting that you can try:

   

space.gif

  • Try insert a '\' in front of the special character.
  • Use double quotes (") around arguments to prevent most expansions.
  • Use single forward quotes (') around arguments to prevent all expansions.

There is a fourth type of quoting in UNIX. Single backward quotes (`) are used to pass the output of some command as an input argument to another. For example:

   

space.gif

$ hostname

asic-world

$ echo this machine is called `hostname`

this machine is called asic-world

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Access Permissions

UNIX is a multi-user system. Every file and directory in your account can be protected from or made accessible to other users by changing its access permissions. Every user has responsibility for controlling access to their files.

   

space.gif

Permissions for a file or directory may be any or all of:

   

space.gif

r - read

w - write

x - execute = running a program

   

space.gif

Each permission (rwx) can be controlled at three levels:

   

space.gif

   

space.gif

u - user = yourself

g - group = can be people in the same project

o - other = everyone on the system

   

space.gif

File access permissions are displayed using the ls -l command. The output from the ls -l command shows all permissions for all levels as three groups of three according to the scheme:

   

space.gif

owner read (r)

owner write (w)

owner execute (x)

group read (r)

group write (w)

group execute (x)

public read (r)

public write (w)

public execute (x)

   

space.gif

which are displayed as: -rwxrwxrwx

   

space.gif

Example outputs from the ls -l command:

   

space.gif

 -rw-------  2 deepak  asic 3287 Apr  8 12:10 file1
    - User has read and write permission. Group and
      others have no permissions. 
 
 -rw-r--r--  2 deepak  asic 13297 Apr  8 12:11 file2
    - User has read and write permission.  Group and
      others can only read the file. 
 
 -rwxr-xr-x  2 deepak  asic 4133 Apr  8 12:10 myprog
    - User has read, write and execute permission.
      Group and others can read and execute the file. 
 
 drwxr-x---  2 deepak  asic 1024 Jun 17 10:00 SCCS
    - This is a directory. The user has read, write and 
      execute permission. Group has read and execute 
      permission on the directory. Nobody else can 
      access it. 
   

space.gif

Note: a directory must have both r and x permissions if the files it contains are to be accessed.

   

space.gif

The chmod command is used to change access permissions for files which you own. The syntax is:

   

space.gif

chmod permission_triads filename

[who][action][permissions]

   

space.gif

where:

   

space.gif

who action permissions

u = user + = add r = read

g = group - = remove w = write

o = other x = execute

a = all

   

space.gif

Examples:

  • chmod a+r sample.f
    • Adds read permission for all users to the file sample.f.
  • chmod o-r sample.f
    • Removes read permission for others to the file sample.f.
  • chmod og+rx prog*
    • Adds read and execute permissions for group and others to all files which contain "prog" as the first four characters of their name.
  • chmod +w *
    • Adds write permission for user to all files in current directory.
   

space.gif

File access permissions can also be changed by a numerical (octal) chmod specification. Read permission is given the value 4, write permission the value 2 and execute permission 1.

   

space.gif

r w x

4 2 1

   

space.gif

These values are added together for any one user category:

   

space.gif

Octal number

Access

permissions given

7

rwx

read, write and execute

6

rw-

read and write

5

r-x

read and execute

4

r--

read only

3

-wx

write and execute

2

-w-

write only

1

--x

execute only

0

---

no permissions

   

space.gif

  ../images/main/bullet_star_pink.gif Example umask commands:
   

space.gif

Umaks

Description

umask 077

Subtracts 077 from the system defaults for files (666) and directories (777). Results in default access permissions for your files of 600 (rw-------) and for directories of 700 (rwx------).

umask 002

Subtracts 002 from the sytem defaults to give a default access permission for your files of 664 (rw-rw-r--) and for your directories of 775 (rwxrwxr-x).

umask 022

Subtracts 022 from the system defaults to give a default access permission for your files of 644 (rw-r--r--) and for your directories of 755 (rwxr-xr-x).

   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com