File Manipulation

The unix file system is
dendritic. Each branch of the tree is a
directory. I won't push the files are like leaves metaphor, but it's
there.

To move around the file system, you need to specify the path
to the point to which you wish to go. Look at the paths of
the directories on the file system schematic. Each is merely
the path of the last plus a slash "/" plus the name of the
current directory. So cd
/home/HEG/armd/TeX places you in David Armstrong's TeX directory
(cd means "change directory"). How do
you move back to /home/HEG/armd, also known as his "home
directory"? Well you could do it four ways:
- You could type cd /home/HEG/armd
- You could type cd ..
.. is the symbol that tells
cd to move back one directory. So
cd ../.. means move back two directories.
- You could type cd ~armd
The tilda is shorthand
for the path to your home directory (/home/HEG in this case).
This method will work for changing into any user's home
directory but wouldn't work for changing to one of the system
directories such as /usr/etc.
- You could type cd
This always takes you to your
home directory. So this way would only work for Dave himself.
File Manipulation Commands
- cd -- change directory.
- Usage: cd /path/to/desired/directory
See discussion of
the file system structure for how to
specify the path.
Example: cd /usr/local/bin --> switches to the /usr/local/bin directory.
- chmod -- change permissions of a
file.
- Usage (example): chmod ug+x file.name
There are three kinds of permission
that a file can have and three sets of users associated with the file each of whom can have a
different set of these permissions. The permissions are
- r -- read (the ability to see the contents of a file)
- w -- write (the ability to change the contents of a file -- for a directory,
this means creating or delete files in the directory)
- x -- execute (the ability to make the operating system follow the
commands contained in the file -- for a directory, this means the ability
to cd into that directory)
The sets of users associated with a file are
- u -- user (the person who owns the file. If you don't own it
you can't tinker with it's permissions.)
- g -- group (members of your group. Everyone in /home/HEG in
Dave Armstrong's case)
- o -- other (any other user on the computer).
So back at the usage line we gave the user and her group the ability
to execute file.name. By default, new files are created with read/write permissions
for the user and read only permissions for the group and others. This default can
be changed using your shell's setting and also may programs explicity change them
for new files (e.g. mail readers usually create new mail files are read only by
the user). Permissions can be taken away by using the minus sign in place of the
plus sign
Example: chmod o-rwx TeX --> takes away all permissions on our
TeX directory from general users.
A user may belong to more than one group. If this is the case, the chgrp
command will let you change a file's group.
- cp -- copy file.
- Usage: cp /path/to/file/existing.file /path/to/newfile/new.file
See discussion of the file system structure
for how to specify the path.
Example: cp /usr/local/share/home.html ~/public_html/home.html
--> copies the file home.html (a www homepage template) to your public_html
directory (where the homepage must reside to be seen by www viewers).
cp /usr/local/share/home.html ~/public_html --> does the same thing.
The command is clever enough to realize that this is a directory and it
assumes you want to keep the original name since you didn't specify
otherwise. To copy many files at once to another directory, just
type the names of all the files as arguments to cp making the
last argument the name of the destination directory. The
shell globbing feature is useful for this if the files have similar names.
- ln -- link files
- Usage: ln -s /path/to/file/existing.file /path/to/newfile/link.name
If you don't intend to modify a copy of file, for instance, you need the
same file in different directories or you the same file to be accessible
by more than one filename, then it is more space efficient to use symbolic links.
Symbolic links in Unix are similar to file aliases on the MAC or shortcuts
in Windows95. They simply serve as a placeholder in the directory to force
anything that tries to access it to get the actual file contents elsewhere.
Example:ln -s home.html index.html --> make it so that any access to
the file contents of home.html actual access the file contents of index.html.
That bit about "file contents" is important. Commands that manipulate the file
as a directory entity, such as cp or rm, operate on the link itself.
For instance, doing "rm index.html" remove the link, not the original file.
However, anything that modifies the contents of home.html, such as "emacs index.html"
modifies the contents of home.html. Note, that if you remove the original file
then the file contents will be gone and the link will be pointing to a non-existing
file. This is called an orphaned link.
The "-s" option tells ln to make a symbolic link. Without it, ln will
make a hard link. This is very much like a symbolic link except there
is no redirection involved. The new link is created such that it just
a regular file that happens to store its file contents at the same
place as the original file on the hard disk. If you remove the
original file, the file contents will still be there accessible from
the other hard link you made. The computer keeps track of all files
that point to contents on the hard disk and when the last file
pointing to those contents is deleted, it makes that area available
for new data to overwrite it. Hard links can only be made if the files
are on the same hard disk.
- ls -- list files in a directory.
- Usage: ls /path/to/directory
ls also takes several useful
flags that can be combined to become even more useful:
- -a List all entries, including those that begin with a
dot (.), which are normally not listed.
- -F Put a slash (/) after each filename if the file is a
directory, an asterisk (*) if the file is an execut-
able, and an at-sign (@) if the file is a symbolic
link.
- -l List in long format, giving mode, number of links,
owner, group, size in bytes, and time of last modifica-
tion for each file (see above). If the file is a spe-
cial file, the size field instead contains the major
and minor device numbers rather than a size. If the
file is a symbolic link, the filename is printed fol-
lowed by and the pathname of the referenced file.
- -t Sort by time stamp (latest first) instead of by name.
The default is the last modification time. (See -n and
-c.)
Example: ls -al --> produces the following:

Notice the drwxr-xr-x type stuff at the left. This specifies
the permissions for each file and directory.
- mv -- move or rename a file.
- Usage: mv /path/to/file/file.name /new/path/to/file/newfile.name
The mv command is used to move a file from one directory to another or
rename a file. Basically, renaming is just moving a file to the same
directory but with a new name.
Example: mv mbox mbox.old --> renames mbox to mbox.old. This assumes
mbox.old isn't a directory. If mbox.old already exists, it is overwritten (i.e.
the original file is removed).
Example: mv mbox /tmp --> moves mbox to the /tmp directory.
The mv command works very much like the cp command in that it can operate
on multiple files if the last argument is a directory and also accepts
shell globbing.
- rm -- delete a file.
- Usage: rm /path/to/file/file.name
The rm command should
ask you whether you really want to delete this file. The only restore
possible is off of a backup tape (backups are done roughly once every
two weeks). Be certain that you want to delete a file. If you're
not, leave it until you are.
See discussion of the file system structure
for how to specify the path. To delete multiple files, additional file
name arguments can be given and
shell globbing is also excepted.
Example: rm paper.log --> removes the file paper.log

Last modified 9/12/96
College of William and Mary, Dept. of Physics
raines@physics.wm.edu