[Next] [Previous] [Up] [Top] [Contents] [Index]
Chapter 6: The UNIX File System
This section describes the basic file manipulation commands:
Section 6.5 describes the commands you can use to change and manipulate directories.
The ls command, which stands for list, is used to list the contents of a directory. ls has many options, some of which are system-dependent, so only a few of them are described here. For a complete description of the command, refer to the man pages for ls.
ls by itself lists the names of the files and subdirectories in the current directory (in multicolumn format on some platforms), sorted alphabetically.
% ls [options] [filenames]
where some of the options are:
-a List all entries, including those that begin with . (dot).
-l List in the long format, giving mode, number of links, owner, group, size in bytes, and (by default) time of last modification, by default sorted by filename.
-C List in columns (default on some platforms)
-F Put a / after the name of each file that is a directory, an * after the name of each file that is executable, and an @ after the name of each file that is a symbolic link.
-R Recursively list subdirectories encountered.
-t Sort by time stamp (latest first) instead of by name. The default time stamp is the last modification time (see -u).
-u Use the time of last access for sorting if used with the -t option or printed in the date column if used with the -l option. -ult both sorts by and displays last access date.
-d If the argument is a directory, list the directory itself, not its contents. Use with -l to get the status (e.g., permissions) of a directory.
If the argument is a directory, ls displays the contents of the directory. Note that this can happen unintentionally as a result of filename expansion. This behavior can be prevented with the -d option. The -t option is useful when looking at recent files:
% ls -lt
will result in the long output sorted by reverse modification date rather than by filename.
The following is a sample output of ls -l.
|
The first line indicates the number of blocks used. The rest of the lines report on (sub)directories or files in the directory being reported on. The first column of the output is called the mode. The character in this first column indicates the type of file, and for our purposes here, they are:
d directory
- ordinary file
The next 9 characters are interpreted as three fields of three characters each, indicating the read (r), write (w), and execute (x) permissions for owner (sometimes called user), group, and other, in that order (see section 6.6.1 for a discussion of permissions).
Next is the number of links to the file or directory. This refers to the number of different names established for it. Normally files have 1, and directories have 1 each for the directory itself, its parent directory, and each of its subdirectories. In the sample output above, notice that the directory Tools has 3 and bin has 2. Evidently, Tools has one subdirectory and bin has none.
The next fields are the login name of the owner, the group to which the owner belongs, the size of the file in bytes, the date and time the file was last modified, and, finally, the filename (which can be a directory name).
UNIX has a number of commands that can be used for displaying the contents of a file at the terminal.
cat, which stands for "concatenate and print," is the standard UNIX file display; it simply prints the file to the screen.[25] When piped to less (see section 5.4.4 which describes less as a filter), cat displays the file contents a screen at a time, and some simple commands may be executed at the supplied prompt.
% cat filename... [| less]
As its name suggests, cat is in fact quite useful for copying and concatenating files. Output is often piped to a file rather than to the screen, using standard output redirection (see section 5.4.2). The following example concatenates the three specified files and copies them sequentially to a single file called allthreefiles:
% cat fileone filetwo filethree > allthreefiles
A shortcut for cat filename |less is to use less as a file browser:
% less filename
And wherever you use less, you can alternatively use more, although it is not as functional as less. You cannot move backwards through the file with more.
head displays the first n lines of the specified file or files. If more than one file is specified, the filename is displayed before each set of file contents of n lines. n defaults to 10 lines.
% head [-n] [filename...]
tail displays the last lines of a file. Its syntax is a bit different:
% tail [+|-n lbc] [filename...]
The option +n displays the file starting n lines down from the beginning of the file, -n displays the last n lines. l, b, or c requests display of n lines, blocks, or characters (default is l lines). If more than one file is specified, the filename is displayed before each set of file contents. n defaults to 10 lines.
tail is useful when you want to see how far a process got. To display the last line of a log file, enter:
% tail -1 logfile
The command cp (stands for copy) can be used to make a copy of a file, leaving the original version intact. You can copy a single file to another one (in the same or a different directory), or you can copy one or more files to a different directory, retaining the same filenames.
The syntax for these two situations varies slightly:
% cp [options] file1 targetfile the file file1 is copied to targetfile, where targetfile may include a path
% cp [options] file1 [file2 ...] targetdirectoryone or more files (file1 file2 ...) are copied to targetdirectory
If the target is a file, its contents are overwritten unless -i is specified, in which case you are prompted for confirmation.
Some options are:
-i If the target filename exists, you are prompted for confirmation before overwriting.
-r Used only with the targetdirectory form. Recursively copy a directory, its files, and its subdirectories to targetdirectory.
The first example below copies myfile to anotherfile, both in my current directory, prompting for verification in case anotherfile already exists.:
% cp -i myfile anotherfile
[Missing image]New users may find it useful to define cpi as the alias for cp -i to use in place of cp so that prompting always occurs. Section 9.3 discusses aliases.
The second example copies files proj1 and proj2 to another directory named newproj which is parallel to the current directory (has same parent directory as current):
% cp proj1 proj2 ../newproj
The third example copies the file oldproj/proj1 to my current directory (.), which is a parallel directory to oldproj (has same parent directory). The file proj1 keeps its name.
% cp ../oldproj/proj1 .
The mv command (stands for move) allows you to rename a file in the same directory or move a file from one directory to another. If you move a file to a different directory, the file can be renamed or it can retain its original name. mv can also be used to move and rename directories.
% mv [options] source1 [source2 ...] target
Depending on whether the source(s) and target are files or directories, different actions are taken. These are described in the table below. If target is a filename, only one source file may be specified.
Source | Target | Result |
|---|---|---|
file | name | Rename file as name |
file | existing file | Overwrite existing file with source file |
directory | name | Rename directory as name |
directory | existing directory | Move directory to be a subdirectory of existing directory |
one or more files | existing directory | Move files to directory |
An important option is:
-i If target exists, the user is prompted for confirmation before overwriting.
The ln (link) command allows you to create a link in one directory to a file in the same or a different directory, or to a different directory. Via links, a file or directory can appear to exist in multiple places, but only actually exist in one, thus conserving disk space. Links are often used to easily reference files or directories that would otherwise require a long path name.
The syntax for ln is similar to that for cp and mv, and in fact they are all run by the same executable.
The most commonly used options for the ln command are:
-i You are prompted before overwriting an existing filename.
-s This makes a symbolic, as opposed to an ordinary or hard, link. A symbolic link can point to a file that is in a different file system, whereas a hard link cannot.
[Missing image]A symbolic link displays the link and the file to which it is linked when you run ls -l; this is the only way to know the name that a file is linked to.
[Missing image]Note that when using the AFS file system, hard links can only be made between files that are in the same directory (the same volume, see section 7.5), so use the -s option even if you're in the same directory tree.
The syntax differs slightly for files and directories:
% ln [options] /path/to/file_name /path/to/link_name Create the link link_name to reference the file file_name. If link_name already exists (as a link or as a file), it gets overwritten (unless you use option -i).
% ln -s [other options] /path/to/file_name /path/to/link_nameCreate symbolic link named link_name, that links to file_name which exists in the same or another directory.
% ln -s [other options] /path/to/file_name1 [/path/to/file_name2 ...] directory Create a symbolic link in directory to each of the listed files. The files may all exist in different directories since the -s option is used. The link names will be the same as the filenames they link to. If files of the same name but in different directories are specified, only the first link of that name will be created.
Let's look at an example:
% ln -s /e741/run1/e_mu2/mydata r5742
If r5742 is a directory, this creates a link called mydata in the directory r5742 that points to /e741/run1/e_mu2/mydata. You can now reference the data file as mydata (i.e. the same filename) as if it were in the directory r5742.
On the other hand, if r5742 is not an existing directory then it represents the name of the link being created. In this case, the command establishes a link called r5742 that points to the file /e741/run1/e_mu2/mydata. Running the command ls -l should display the following output:
lrw-r--r-- 1 aheavey g020 46 Aug 29 14:26 r5742 -> /e741/run1/e_mu2/mydata
The rm command (stands for remove) is used to remove the entries of one of more files.
% rm [options] file...
Some commonly used options are:
-i Confirmation of removal of write-protected file occurs interactively, whether the standard input is a terminal or not. If used with the -r option, you are prompted about each directory before it is examined.
-r Causes rm to delete the contents of the specified directory, including all its subdirectories, and the directory itself (recursive). This option should be used cautiously.
[Missing image]The file list can include ambiguous file references, so rm should be used cautiously. You can use the echo utility with the same ambiguous file reference to see the list generated.
Removal of a file requires write permission to its directory, but neither read nor write permission to the file itself. If the file has no write permission and the standard input is a terminal, the set of permissions is printed and you are prompted for confirmation. If the answer begins with a y, the file is deleted. If the standard input is not the terminal, the files are deleted without confirmation.
[Missing image]New users may find it useful to define rmi as the alias for rm -i to use in place of rm so that prompting always occurs. Section 9.3 discusses aliases.
The tar utility (tape archive) can be used to create, add to, list and retrieve files from an archive file. Archive files are often stored on tape. The action taken by the tar command depends on the key, which is essentially a function option. The key must be specified on the command line as if it were the first option. It may be followed by function modifiers, and then by options and/or arguments. The keys and function modifiers must be grouped together before any arguments are listed. tar does not require, but does allow, a dash (-) before the list of keys and function modifiers. The keys and functions are:
c create a new tar file
r append specified files to tar file
t list all files in the tar file, or all files in a specified file list
u append new or changed files to tar file
x unwind entire tar file or extract specified files from tar file and write each file to the directory as specified in the tar file relative to the current directory
The keys, function modifiers and options are discussed in the man pages. Be aware that they vary in some cases between UNIX flavors. The command syntax varies somewhat from key to key, so check the man pages for that information, too.
[Missing image]When creating a tar file, we have a few recommendations for avoiding problems:
First, when possible, create the tar file on a machine of the same flavor as the target platform. Occasionally a tar file doesn't unwind properly on a different platform.
Secondly, choose your working directory carefully. It is often convenient or desirable to be able to specify simple relative path names for the files to include in the tar file. For example:
% cd /path/to/dir % tar cvf /tmp/filename.tar .
creates a tar file with all pathnames relative to /path/to/dir. In general you should not specify the pathname explicitly on the command line, unless it will be valid on any other system where the tar file may be unwound and used.
Thirdly, be careful choosing your target directory for the new tar file. Make sure that the target directory is outside of the directory tree that you're including in the tar file. Otherwise the tar file tries to include itself, and can grow infinitely large.
[Missing image]Note to ex-VMS users: tar does not provide for extracting a named set of files from a backup file and placing them all in the current directory disregarding their directory specifications on the backup file.
Several utilities are available on UNIX systems for file compression. compress and pack are native UNIX utilities, and gzip is provided by FUE. We recommend you use gzip for file compression, and its associated utility gunzip for file expansion. gunzip recognizes and can expand files compressed with compress and pack as well as gzip.[26]
The file extensions gunzip recognizes include .gz, -gz, .z, -z, _z, and .Z. gunzip also recognizes the special extensions .tgz and .taz as shorthands for .tar.gz and .tar.Z, respectively. When compressing, gzip uses the .tgz extension if necessary instead of truncating a file with a .tar extension.
[Missing image]You will need to reference the man pages for details on syntax, options and usage. In their simplest forms gzip and gunzip can be used as follows, starting, for example with the original uncompressed file bigfile:
% gzip bigfile
The result is bigfile.gz, whose size is reduced with respect to bigfile according to Lempel-Ziv coding (LZ77), the same compression scheme used by compress. Whenever possible, gzip replaces each file by one with the extension .gz, while keeping the same ownership modes and access and modification times. gzip will only attempt to compress regular files. In particular, it will ignore symbolic links. If the compressed file name is too long for its file system, gzip truncates it.
Compressed files can be restored to their original form using gunzip, or equivalently by using the -d option with gzip. If the original name saved in the compressed file is not suitable for its file system, a new name is constructed from the original one to make it "legal".
To restore bigfile.gz to its original name and size, enter:
% gunzip bigfile.gz