File Manipulation

colored bar

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.

filesys schematic

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:

  1. You could type cd /home/HEG/armd
  2. You could type cd ..
    .. is the symbol that tells cd to move back one directory. So cd ../.. means move back two directories.
  3. 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.
  4. 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 The sets of users associated with a file are 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: Example: ls -al --> produces the following:

listing example

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 colored bar

Last modified 9/12/96
College of William and Mary, Dept. of Physics

raines@physics.wm.edu