|
|
|
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
UNIX directory structure
|
|
|
|
|
|
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).
|
|
|
|
|
|
|
Directory and file handling commands
|
|
|
This section describes some of the more important directory and file handling commands. |
|
|
|
|
|
pwd (print [current] working directory) |
|
|
|
|
|
pwd displays the full absolute path to the your current location in the filesystem. So |
|
|
|
|
|
$ pwd |
|
|
/usr/bin |
|
|
|
|
|
implies that /usr/bin is the current working directory. |
|
|
|
|
|
ls (list directory) |
|
|
|
|
|
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 /, |
|
|
|
|
|
$ ls |
|
|
bin dev home mnt share usr var |
|
|
boot etc lib proc sbin tmp vol |
|
|
|
|
|
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: |
|
|
|
|
|
$ ls -a |
|
|
|
|
|
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: |
|
|
|
|
|
$ ls -a -l |
|
|
(or, equivalently,) |
|
|
$ ls -al |
|
|
|
|
|
|
|
|
Each line of the output looks like this: |
|
|
|
|
|
|
|
|
where: |
|
|
|
|
|
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.
|
|
|
|
|
|
|
ls supports more options. To find out what they are, type: |
|
|
|
|
|
$ man ls |
|
|
|
|
|
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: |
|
|
|
|
|
$ info ls |
|
|
|
|
|
cd (change [current working] directory) |
|
|
|
|
|
$ cd path |
|
|
|
|
|
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). |
|
|
|
|
|
Used without any target directory |
|
|
|
|
|
$ cd |
|
|
|
|
|
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 |
|
|
|
|
|
$ cd - |
|
|
|
|
|
mkdir (make directory) |
|
|
|
|
|
$ mkdir directory |
|
|
|
|
|
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. |
|
|
|
|
|
rmdir (remove directory) |
|
|
|
|
|
$ rmdir directory |
|
|
|
|
|
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). |
|
|
|
|
|
cp (copy) |
|
|
|
|
|
cp is used to make copies of files or entire directories. To copy files, use: |
|
|
|
|
|
$ cp source-file(s) destination |
|
|
|
|
|
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. |
|
|
|
|
|
To copy entire directories (including their contents), use a recursive copy: |
|
|
|
|
|
$ cp -rd source-directories destination-directory |
|
|
|
|
|
mv (move/rename) |
|
|
|
|
|
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: |
|
|
|
|
|
$ mv source destination |
|
|
|
|
|
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). |
|
|
|
|
|
rm (remove/delete) |
|
|
|
|
|
$ rm target-file(s) |
|
|
|
|
|
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: |
|
|
|
|
|
$ rm -i myfile |
|
|
rm: remove 'myfile'? |
|
|
|
|
|
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: |
|
|
|
|
|
$ rm -rf / home/will |
|
|
|
|
|
(instead of rm -rf /home/will). |
|
|
|
|
|
cat (catenate/type) |
|
|
|
|
|
$ cat target-file(s) |
|
|
|
|
|
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): |
|
|
|
|
|
$ cat > hello.txt |
|
|
hello world! |
|
|
[ctrl-d] |
|
|
$ ls hello.txt |
|
|
hello.txt |
|
|
$ cat hello.txt |
|
|
hello world! |
|
|
$ |
|
|
|
|
|
more and less (catenate with pause) |
|
|
|
|
|
$ more target-file(s) |
|
|
|
|
|
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). |
|
|
|
|
|
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): |
|
|
|
|
|
$ ls -l | more |
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
|
|
|
Specifying multiple filenames
|
|
|
Multiple filenames can be specified using special pattern-matching characters. The rules are: |
|
|
|
|
|
- '?' 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: |
|
|
|
|
|
- ??? 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.
|
|
|
|
|
|
Note that the UNIX shell performs these expansions (including any filename matching) on a command's arguments before the command is executed. |
|
|
|
|
|
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: |
|
|
|
|
|
- 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: |
|
|
|
|
|
$ hostname |
|
|
asic-world |
|
|
$ echo this machine is called `hostname` |
|
|
this machine is called asic-world |
|
|
|
|
|
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. |
|
|
|
|
|
Permissions for a file or directory may be any or all of: |
|
|
|
|
|
r - read |
|
|
w - write |
|
|
x - execute = running a program |
|
|
|
|
|
Each permission (rwx) can be controlled at three levels: |
|
|
|
|
|
|
|
|
u - user = yourself |
|
|
g - group = can be people in the same project |
|
|
o - other = everyone on the system |
|
|
|
|
|
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: |
|
|
|
|
|
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) |
|
|
|
|
|
which are displayed as: -rwxrwxrwx |
|
|
|
|
|
Example outputs from the ls -l command: |
|
|
|
|
|
-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.
|
|
|
|
|
|
Note: a directory must have both r and x permissions if the files it contains are to be accessed. |
|
|
|
|
|
The chmod command is used to change access permissions for files which you own. The syntax is: |
|
|
|
|
|
chmod permission_triads filename |
|
|
[who][action][permissions] |
|
|
|
|
|
where: |
|
|
|
|
|
who action permissions |
|
|
u = user + = add r = read |
|
|
g = group - = remove w = write |
|
|
o = other x = execute |
|
|
a = all |
|
|
|
|
|
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.
|
|
|
|
|
|
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. |
|
|
|
|
|
r w x |
|
|
4 2 1 |
|
|
|
|
|
These values are added together for any one user category: |
|
|
|
|
|
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
|
|
|
|
|
|
|
Example umask commands:
|
|
|
|
|
|
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).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|