4.0 Files and Directories


At this point you are ready to get the tutorial files. If you are in the EE teaching labs type:
% go_tutorial
this will create a directory called tutorial in your home directory.

4.1 Print Working Directory

The pwd (print working directory) command will display your current location in the file system.

When you log into the system you are place in your home directory. In the following examples we will assume that the home directory is /home/huron2/admin/scot

% pwd
/home/huron2/admin/scot
%
If you are using the using a shell that allows you to set the prompt you can change the prompt to be the current location in the directory structure.

4.2 Changing Directories

The cd(change directory) command will move the user to a different working directory. Lets change to the /usr/bin directory.

% cd /usr/bin
% pwd
/usr/bin
%

When we changed to the /usr/bin directory we specified its full pathname, since it starts from the root. If we were located in the /usr directory and we wanted to change to /usr/bin we could use cd bin instead of specify the path from the top of the filesystem, this is known as the relative pathname since it is relative to your current directory.

There are special characters that are used as directory abbreviations. If we were in the /home/huron2/admin/scot directory and we wanted to go to the parent directory or /home/huron2/admin we would use the comand cd .. to change to the parent directory.

If you know the username of another user you can change to their directory without specifying the full pathname. The T-shell will recognize the ~username to mean the home directory for that particular user. The ~ by itself refers to your home directory.

Table4.2:  T-Shell Directory Abbreviations
-----------------------------------------------------------------------------
.          Current directory                                                   
..         Next directory "up" in the hierarchy. Also called parent directory  
~          Your home directory                                                 
~username  The usernames's home directory                                      
-----------------------------------------------------------------------------
If you want to return to your home directory from any location in the directory hierarchy type in cd with no options.

% cd
% pwd
/home/huron2/admin/scot
%
Change to the tutorial directory, if you are following interactively.
% cd tutorial
%

4.3 Listing file and directories

The ls(list) command is used to list the contents of a directory.

% ls
Makefile     friends      friends.pl   hello.cc     postscript
% 

Files starting with a . (dot) are hidden files. This means that ls won't normally display them. To display these files we must use the -a (all) option. Generally files are hidden because they are system files, there is no reason to display them under most circumstances. Most system dot files reside in your home directory.

When we do an ls it is difficult to tell which files are directories and which are plain files. The -F (full) option to ls will append a slash (/) to files that are directories, a asterisk(*) to executable files, and an at sign(@) to hard links .

% ls -F
Makefile     friends      friends.pl*  hello.cc     postscript/
%
This tells us that we have one executable and one directories in the current directory.

One of the most common ways to list the files in a directory is with the -l (long) option.

% ls -l
total 10
-rw-r--r--   1 scot     admin        242 Mar  7 17:00 Makefile
-rw-r--r--   1 scot     admin        416 Mar 23 12:20 friends
-rwxr--r--   1 scot     admin        868 Mar 15 14:24 friends.pl
-rw-r--r--   1 scot     admin         70 Mar  7 17:00 hello.cc
drwxr-xr-x   2 scot     admin       1024 Mar 24 15:43 postscript
%
The -l option gives information on the file permissions, number of hard links , file owner, file group, file size, time last modified and the file name.

An argument to ls is the file or directory you want to list, if no argument is given ls lists the current directory. However, if you use ls with the directory name as the argument you will get a listing of the files and directories located in that directory. If you wanted to see the permission of the directory use the -d (directory) option.

% ls -l postscript
total 656
-rw-r--r--   1 scot   admin    326291 Mar 18 13:02 mosaic.ps
% ls -ld postscript/
drwxr-xr-x   2 scot   admin    1024 Mar 24 16:17 postscript/
%

4.4 Changing file permissions, owner and group

To chmod (change mode) command is used to change the permissions of a file or directory. A file has three sets of permissions for user, group and other, which can be represented by the letters u, g and o, repectively. You also know that each of these groups can have different combinations of read, write and execute, which can be represented by r, w, and x, respectively.

The chmod command uses the plus(+) sign to add permissions and a minus(-) sign to remove permissions. Therefore the final syntax is the user class(u, g, or o) followed by the action to take(+ or -), followed by the permission to change(r,w, or x).

% ls -l friends
-rw-r--r--   1 scot     admin        416 Mar 23 12:20 friends
% chmod g+w friends
% ls -l friends
-rw-rw-r--   1 scot     admin        416 Mar 23 12:20 friends
%
To change the permissions of all classes of users we could do the following.

% chmod ugo+x friends
% ls -l friends
-rwxrwxr-x   1 scot     admin        416 Mar 23 12:20 friends
%
Instead of specifying the three groups we can use the a(all) option, for all users.
% chmod a-x friends
% ls -l friends
-rw-rw-r--   1 scot     admin        416 Mar 23 12:20 friends
%

When you first create a file it is given a set of permissions, that are determined by the umask (user mask) which is a built-in shell command.

% umask
22
%
The umask command without any arguments will display the current values. Actually the umask is 022 but leading zeros are ignored. The number is called a mask because each digit is subtracted from a system default value. The default value is usually rw-rw-rw- or 666 in octal notation. Because the umask is subtracted from the default value you cannot turn on permissions that are already turned off in the default value. Use the chmod command to turn on permissions.
Table4.4:  Determining the umask
-----------------------------------------------------------------
Octal Value				Corresponding permissions
-----------------------------------------------------------------
 666 (current default permissions)	rw-rw-rw-         
-600 (permissions you want)		rw-------
----
 066 (umask)
----------------------------------------------------------------
The T-shell uses a default umask of 022 which will create files with rw-r--r-- permissions. The only arguments to umask is to set the umask.
% umask 066
% touch new
% ls -l new
-rw-------   1 scot     admin          0 Mar 29 09:51 new
%

The touch command will create a new file if no arguments are given or update the modification time of a file that has already been created. With a umask of 066 only the creator of the file will be able to read or write to the file, therefore you might want to change the umask back to 022.

% umask 022
The chmod command supports the -R (recursive) option to change the permissions in a directory and all its sub-directories.

4.5 Copying files and directories

The cp (copy) command duplicates one or more files or directories.

% cp friends friends.copy
% ls f*
friends       friends.copy  friends.pl
%
The -r (recursive) allows you to copy whole directories, including subdirectories.

% cp -r postscript post
% ls -d p*
post        postscript
%

4.6 Moving files and directories

The mv (move) command will change the name of a file or directory.

% mv friends.copy friends.old
% ls f*
friends      friends.old  friends.pl
%
The mv command can also be used to rename or move entire directories.
% mv post posters
% ls -d p*
posters     postscript
%

4.7 Creating directories

You can consider directories to be filing cabinets into which you categorize your work. Just as with the filing cabinet analogy, the better you organize your work into directories the easier it is to retrieve the file that you want.

The mkdir (make directory) command is used to create one or more new directories.

% mkdir temp
% ls -ld temp
drwx--x--x   2 scot     admin         24 Apr  4 13:23 temp
%

4.8 Removing files and directories

The rm (remove) command removes one or more unwanted files.

% rm friends.old
% ls f*
friends     friends.pl
%
If you use the -i (interactive) option, rm checks with you before removing each file that you specify. Sometimes files are write protected. If you try to remove these files the rm command prompts you on whether to remove a write protected file. The -f (force) option forces the removal of files even if they are write protected.

The -r (recursive) option in used to remove entire directories and all sub-directories below thedirectory.

% rm -r posters
%
Another command to remove directories is the rmdir (remove directory) command. Unlike the rm -r command the rmdir command requires that the directory be empty.

4.9 Linking files and directories

The ln (link) command is used to create a link to a directory or a file in the directory structure. Unlike a real file a link has no contents, it simply acts as a pointer to something else. Therefore links can be used instead of having multiple copies of the same file.

There are two types of links hard links and symbolic links or soft links. Hard links cannot point to directories or to files in a different file system, whereas symbolic links can. If you use the ln command with no options you will get a hard link. The -s (symbolic) option is used to get a symbolic link. The format of the ln command is:

ln [options] existing_file link_name

Lets create a symbolic link to the temp directory.

% ln -s temp temp2
% ls -ld t*
drwxr-xr-x   2 scot     admin         24 Mar 25 12:07 temp
lrwxr-xr-x   1 scot     admin          4 Mar 25 13:11 temp2 -> temp
% 
A symbolic link to a directory is not a directory, so to remove it you don't need the -r(recursive) option.
% rm temp2
% ls -ld t*
drwxr-xr-x   2 scot     admin         24 Mar 25 12:07 temp
%
A common symbolic link for programmers is to have a link named core that points to /dev/null. Core files are generated when a program "core dumps" or produces debugging output. These core files take a tremendous amount of storage, and are used infrequently. The /dev/null is the Unix equivalent of a trash can. In effect, what you are doing is telling the system to toss this file away whenever it is created.
% ln -s /dev/null core
% ls -l c*
lrwxr-xr-x   1 scot     admin          9 Mar 25 13:34 core -> /dev/null
%
Caveat: with symbolic links you can remove the "real" file and the symbolic link will still exist.

4.10 Determing file type

The file command gives you detailed information about the specific type of a file. In the section on the ls command we saw that the ls -F command will tell you general information about a file.

Lets get specific information about the mosaic.ps file, that is located in our postscript directory

% file postscript/mosaic.ps
mosaic.ps:      postscript file -version 2.0
%
Now we know that the file is an postscript file.



Last Modified: 4/4/94 by Scott Miller