next up previous contents
Next: Printing from UNIX Up: UNIX Help and more Previous: UNIX Directory and Subdirectory

UNIX File Handling

  File Specification

File specifications provide the system with the needed information to uniquely identify a file or device. To avoid problems refrain from using characters that have other meanings. It is best to use only letters, numbers, and the period.

A full pathname consists of: /users/users3/grad/homedir

/ (leading) = Root of the file system when it is the first character in the path name,
users = System directory one level below root in the hierarchy
/ (subsequent) = Slash that delimits the directory names
users3, grad = Further subdirectories below /users
homedir = User's home directory.

The use of wildcards makes it seldom necessary to explicitly use the complete file specification. The question mark (?) matches any single character. The asterisk (*) matches any number of characters. The [...] means to match any of the characters inside the brackets. Numbers (e.g. [5-9]) or letters (e.g. [A-z]) can be used.


% lpr chap[1-3ad]*
Print all files that start with chap1, chap2, chap3, chapa, or chapd
% cat ?
Type all files that consist of a one character name

If you wish to suppress the special meanings of *, ?, etc., enclose the entire argument in single quotes (e.g. cat '?').

File Manipulation Commands

Typing the contents of files can be accomplished using:
% cat filename

This displays the file on standard output, and
% cat file1 file2 > file3

concatenates the first two files and places the result on the third, overwriting the current contents of file3.
The more command can also be used for typing out files, one screenful of text at a time. Hit space to see the next page or return to see the next line. Q or q may be used to exit from more.

Copying files may be done by typing :

% cp fromfile tofile

The cp command does not copy a file onto itself. You may also copy one or more files onto a directory by:

% cp f1 f2 ... directory

cp -i will prompt the user with the name of the file whenever the copy will cause an old file to be overwritten.
If cp -r is used and any of the source files are directories, cp copies each subtree rooted at that name; in this case the destination must be a directory.

The move file command (which essentially renames a file) is mv.

% mv good bad

renames the file good as bad. You can also use this command to move files or directories to a different place in the directory tree.

Deleting files may be accomplished by rm command. To remove file foo, type:

% rm foo

Once deleted the file is gone forever (if there are no links to the file). To remove a file, you must have write permission in its directory, but you do not need read or write permission on the file itself. The -i option with rm command is useful since it allows the interactive removal of a file.

To get a directory listing of files in the default directory, type the ls command. See the online manual for a complete list of its options. For example, the -a option lists all files in the directory, including those with names beginning with (.),which otherwise are not listed; -R option lists subdirectories recursively; -l lists the mode, number of links, owner, size in bytes and time of last modification for each file ; etc..

( the mode field needs a brief explanation - it represents the protection mode of the file - it consists of 11 characters : the first character indicates the type of entry :

The next 9 characters are interpreted as three sets of three characters each (read, write, execute). The first set corresponds to file-access permissions for the user, the next for the group and the last for all others. The last character of the mode (normally x or -) is t if the 1000 bit of the mode is on. See the man pages on chmod for the meaning of this mode.)

Note: The rm, cp, mv commands can cause files to be deleted. There is an optional switch -i which when used with any of these commands will query you before actually carrying out the operation. You can force the rm, cp, mv commands to query you by placing the following commands in your .bash_profile or .bashrc file :

% alias rm rm -i
% alias mv mv -i
% alias cp cp -i

The command grep reg-exp file(s) searches the files for a pattern specified by a limited regular expression. There are numerous optional switches such as -i for ignoring difference in upper and lower case; -v for printing the unmatched lines; etc.. It is safe to enclose the entire expression argument in single quotes to avoid any ambiguity with meaningful shell commands such as $,*,[,^,(,),|,\ . The rules for the expression are listed in the man page for grep. Example:
% grep -i 'Find this text' *.tex

To determine the existence of a particular file anywhere in the file hierarchy starting from a specified path list you may use the find command.
To find and list all files below the current directory, whose names end in .dat:
% find . -name '*.dat' -print

To find and list all files ending in .dat and containing the word homework :
% find . -name '*.dat' -exec fgrep -i homework
{} ; -print

Files can be compared by the diff or the cmp commands. The latter allows comparison of binary files and directories. Usage:
% cmp file1 file2

compares file1 and file2. If they differ it announces the byte and line number at which the difference occurred. For text files it is better to use diff which lists any differences.

next up previous contents
Next: Printing from UNIX Up: UNIX Help and more Previous: UNIX Directory and Subdirectory
Paul A. Sihvonen-Binder