Files
Creating files
However, to put files in your directories, you will either have to edit and save them, or bring them in via
ftp , or via cat. cat is a multipurpose utility, depending on
the use of redirects. With a file argument and no redirect, it serves as a “type” command; with an
input redirect, it is the equivalent of DOS “copy con”, taking terminal input and putting it in a file.
Type
cat > whitman
I hear america singing;
its varied voices I hear.
^D
(where the ^D stands for Control-D: hold down the control key and type “D”. Case doesn’t
matter with control characters .)
Now type
more whitman
which will display the file. However, this is obviously awkward for long files, even with some primitive
editing facilities on the current line (see control characters ).
To invoke the vi or emacs editors, type
vi < filename> or emacs < filename>
See vi for details on vi commands, and emacs for details on emacs. For an easy example session, see the Glass
book, Chapter 2 (pages 60--68 for vi, and pages 69--76 for emacs).
Moving and Manipulating Files
- The mv command:
- A file in a directory can be renamed using mv
mv whitman voices
- It can also be used to move a file to a new directory. Assuming your directory test still
exists:
mv voices test
- Now list the files in your top-level directory ( ls ), and the files in your directory test
ls test
- Finally, we can do both at once:
mv test\voices whitman
- The mv command can use the ~, ., and .. path abbreviations.
- If the last argument is a directory, multiple file names (even using wildcards) can precede it.
- Using the -i option on mv makes the system ask when it is about to overwrite a file.
- The cp command:
- Works just like mv, but only copies the file(s) instead of moving it (them).
- The rm command:
- Removes/deletes the files that follow the command. Also can use wildcards.
- Using the -i option on rm makes the system ask when multiple files are deleted.
- Another application of the cat command lets you combine several files into one:
cat file1 file2 ... filen > target
- This concatenates the listed files into a single file target.
- filei
and target are filenames, and target is not one of the input files --- in
fact, ideally target should not exist when you execute the command.
File Protection
There are four levels of access to any file in a UNIX environment:
- Superusers
can access any file. Generally this includes only the systems staff.
- The others are the owner of the file, the group of the file, and the rest of the world
(everybody). On the RISC machine at Seton Hall, there are only a few groups, including student
and faculty.
The owner of a file can set and modify the permission bits of a file or
directory. The individual bits grant permission to read, write, or execute a file (execute = list for
directories) for the owner, the group, and others. (You might want to set important files which you have
finished working with to be read-only, or read-and-execute-only, to make sure you don’t erase them.)
- The initial file protection is set by a combination of the default permissions of the software that
creates the file, and the umask (if any) in your dot-files. (Seeconfiguration files.)
- The existing permissions are listed in the ls -lga command.
- There are two ways to manage permissions in UNIX. The display on the screen corresponds to the
second of these (modified as described below).
- The first way that file protections can be set is to use an explicit assignment of permissions to one of
the three groups. The syntax is
chmod [-R] < change ...> < filename ...>
- The optional -R flag applies chmod to each file in the directory, as well as to the directory
itself.
- Each change is of the form
< levels> < op> < permissions>
- level
is a (non-empty) subset of u, g, o, a (user, group, other, all).
- op is +, - or =.
- permissions
is a (non-empty) subset of r, w, x, s (read, write, execute, set/sticky)
- + adds the given permission(s) to each level, or if a is used, to all levels.
- - removes the given permission from each level, or if a is used, from all levels.
- = sets the permissions for the given level(s) to be exactly as specified.
- The s permission causes the id of the last user or group to touch the file to be stored in its owner and
group fields.
chmod u+x myfile otherfile
chmod o-rwx secretfile
chmod g=rx projectfile
- The second approach is to view the permissions for a level as an octal number specified by three bits,
1 if the level has the permission, and 0 if it does not. The permissions are always listed in order
(read,write,execute), so read and execute but not write permission correspond to 101, or 5. Permissions
now correspond (except for the sticky bits) to three octal numbers. Thus we can use
chmod < number> < filename ...>
- < number> is three octal digits, giving permissions for the user, group, and others.
chmod 744 myfile
- This gives the user full permission, and the group and others read permission only.
- Sticky bits are managed by including an initial digit.
- Of the ten-place display of permissions in the long-form file list, the last nine list the file
permissions, in the usual order (user,group,others). The first place is a file-type value: - for ordinary
files,. d for directories, l for links, and some other cases to be described later.
- On a related issue, the group and owner of a file can be changed by using chgrp and
chown, respectively. In general, you can change one of your files from one of your groups to
another; typically only superuser can use chown. To see your groups, use the utility
groups.
Print
The print command in UNIX is lpr.
lpr < filenames>
Wildcards are legal, but some systems and some printer software have trouble handling multiple files.
The flags on lpr tend to be system-specific, but, if there are multiple printers available, you can
usually count on lpr -P< printername> < filenames> . Other flags which may be
available allow the printing of multiple copies, printing of single-sided versus double-sided output,
printing in landscape mode, and so on. You should try man lpr if you need any of these
effects.
Some applications, in particular TeX and LaTeX, produce output which cannot be printed directly by
lpr. The output has to be processed through a filter, in this case called dvips or
dvitps to produce printable text. Neither of these are (as far as I know) currently available on the
RISC.
The current status of a print queue can be examined by lpq, using the -P< printername>
flag if you have to use it for lpr.
A pending print request can be cancelled by using lpq to find the request number, and then using
lprm -P< printername> < request_number> .