This chapter describes how to manage files on your system. After completing this chapter, you will be able to:
To learn about managing files, follow the examples in this chapter. Do each example in order so that the information on your screen is consistent with the information in this book.
Before you can work through the examples, you must
be logged in and your login directory must contain the
following three files
created in
Chapter 2:
file1
,
file2
,
and
file3
.
To produce a
listing of the files in your login directory, enter the
ls
command,
which is explained in the following section.
If you are using files with different names, make the
appropriate substitutions as you work through the examples.
In the following examples, when you are asked to return to your
login directory, enter the
cd
(change directory) command as follows:
$
cd
$
In the preceding example, the dollar sign ($) represents the shell prompt. Your shell prompt may vary.
In addition, before working on the examples in this chapter, create
a subdirectory called
project
in your login directory.
To do so, enter the following
mkdir
(make directory) command from your login directory:
$
mkdir project
$
For more information on the
cd
and
mkdir
commands, see
Section 4.2
and
Section 4.1
respectively.
You can display a listing of the contents of one or more
directories with the
ls
(list directory) command.
This command produces a list of the files
and subdirectories (if any) in your current directory.
You can also
display other types of information, such as
the contents of directories other than your current directory.
The format of the
ls
command is:
ls
The
ls
command has a number of options, called
flags
that enable you to display different types of information about the
contents of a directory. Refer to
Section 3.1.3
for information about these flags.
To list the contents of your current directory, enter:
$
ls
Used without flags in this format, the
ls
command lists
the names of the files and directories in your current directory:
$
ls
file1 file2 file3 project $
You may also list portions of your current directory's contents by using the command format:
ls
filename
The
filename
entry can be the name of the file or a list of filenames
separated by spaces. You may also use pattern-matching characters
to specify files. See
Chapter 2
for information on pattern matching.
For example, to list the files whose names begin with the characters
file
,
you would enter the following command:
$
ls file*
file1 file2 file3 $
To display a listing of the contents of a directory other than your current directory, use the following format:
ls
dirname
The
dirname
entry is the pathname of the directory whose contents you want to display.
In the following example, the current directory is your login directory, and
you want to display the
/users
directory. Your system may contain another directory with a name
similar to the
/users
directory. The name of the
/users
directory is
preceded by a slash (/),
which indicates that the system should begin
searching from the root directory.
$
ls /users
amy beth chang george jerry larry mark monique ron $
The
ls
command
lists directory and filenames in collated order as determined by the
current locale. For more information about locales (as used
with internationalization), see
Appendix C.
In its simplest form, the
ls
command displays only the names of files
and directories contained in the specified directory. However,
ls
has several flags that provide additional information about the
listed items or change the way in which the system displays the listing.
When you want to include flags with the
ls
command, use the following format:
ls
-flagname(s)
The
-flagname(s)
entry specifies one or more flags (options)
that you are using with the command.
For example, the
-l
flag produces a
long
listing of the directory contents. Note also
that all
ls
flags are preceded by the dash character
(-).
If you want to use multiple flags with the command, enter the flag names together in one string:
$
ls -lta
Table 3-1
lists some of the most useful
ls
command flags.
Flag | Action |
-l
|
Lists in
long
format. An
-l
listing
provides the type, permissions, number of links, owner, group, size,
and time of last modification for each file or directory listed.
|
-t
|
Sorts the files and directories by the time they were last modified (latest first), rather than collated by name. |
-r
|
Reverses the order of the sort to get
reverse collated
order
(ls -r ),
or
reverse time
order
(ls -tr ).
|
-a
|
Lists
all
entries including "hidden files". Without this flag, the
ls
command does not list the names of entries that begin
with a
dot (.), such as
.profile ,
.login ,
and relative pathnames.
|
The following example shows a long
(-l
)
listing of a current
directory. The name
larry
shows the owner of the files. Your
username and group name will replace
larry
and
system
on the screen.
$
ls -l
total 4 -rw-r--r-- 1 larry system 101 Jun 5 10:03 file1 -rw-r--r-- 1 larry system 75 Jun 5 10:03 file2 -rw-r--r-- 1 larry system 65 Jun 5 10:06 file3 drwxr-xr-x 2 larry system 32 Jun 5 10:07 project $
Table 3-2
explains the information displayed on your screen after you
enter the
ls -l
command.
Field | Information |
total 4
|
Number of 512-byte blocks taken up by files in this directory. |
drwxr-xr-x
|
File type and permissions set for each file or directory. The
first character in this field indicates file type:
-
(dash) for ordinary files
b
for block-special files
c
for character-special files
d
for directories
l
for symbolic links
p
for pipe-special files (first in, first out)
s
for local sockets
The remaining characters indicate what read ( r ),
write
(w ),
and
execute
(x )
permissions are set for the
owner, group, and others. In addition, other permission information
may also be displayed. |
1
|
Number of links to each file. |
larry
|
Username of the file's owner. |
system
|
Group to which the file belongs. |
101
|
Number of bytes in the file. |
Jun 5 10:03
|
Date and time the file was created or last modified in the format defined by your current locale. |
file1
|
Name of the file or directory. |
Table notes:
There are other
ls
command flags that you may find useful as you
gain experience with the operating system. For detailed information
about the
ls
command flags, see the
ls
(1)
reference page.
You can view any text file stored on your system with a text editor. However, if you want to just look at a file without making any changes, you may view it (with or without screen formatting) using a variety of operating system commands. The following sections describe these commands.
The following commands display a file just as it is, without adding any special characteristics that govern the appearance of the contents:
pg
cat
more
For information on displaying files with formatting, see Section 3.2.2.
To display a file without formatting, the general format is:
command
filename
The
command
entry is one of the following command names:
pg
,
more
,
or
cat
.
The
filename
entry can be the name of one file, or a series of filenames
separated by spaces. You may also use pattern-matching characters
to specify your files. See
Chapter 2
for information on using pattern-matching characters.
The
pg
command allows you to view one or more files.
In the following example, the
pg
command displays the contents of
file1
in your login directory:
$
pg file1
You start the vi program by entering the command vi, optionally followed by the name of a new or existing file. $
To view the contents of both
file1
and
file2
,
enter both file names on the command line.
When you display files that contain more lines than will fit on the
screen, the
pg
command pauses as it displays each screen. To view the next screen
of information in a file, press the Return key until you reach the
end of the current file. When you press the Return key at the end
of the current file, the start of the next file is displayed.
The
pg
command always displays multiple files in the order in which
you listed them on the command line.
In the example shown below,
(EOF):
(end of file) means that you are at the end of the current file.
$
pg file1 file2
You start the vi program by entering
the command vi, optionally followed by the name
of a new or existing file.
(EOF):[Return]
(Next file: file2)[Return]
If you have created a new file, you will find
that it is easy to add text.
(EOF):[Return]
$
At the
Next file:
filename
prompt, you can enter the
-n
option to go back to the previous file instead of
displaying the next file.
When you display files that contain more lines than will fit on the
screen, the
pg
command pauses as it displays each
screen. To see the next screen of information in a file, press
the Return key.
The
more
command also allows you to enter multiple file names on the
command line and
is very much like the
pg
command in the way that it handles long files. If the
file(s) contains more lines than can fit on your screen,
more
pauses and displays a message telling you what percentage of the file
you have
viewed thus far. At this point, you can do one of the following:
q
to quit viewing the file(s)
The
cat
command also displays text. However, it is less useful for viewing
long files because it does not paginate files.
When viewing a file that is larger than one screen, the contents
will display too quickly to be read. When this happens, press
Ctrl/S
to halt the display. You can then read the text. When you want
to display the remainder of the file, press
Ctrl/Q.
Because
cat
is not useful for viewing long files, you may prefer using
the
pg
or
more
command in these cases.
The
pg
,
more
,
and
cat
commands all have additional options that you may find useful. For more
information, refer to the reference pages for these commands.
Formatting is the process of controlling the way the
contents of your files appear when you display or print them. The
pr
command displays a file in a simple but useful style.
Note
The
pr
command does not interpret any text formatting macros that may reside in your file. Thepr
command does not format files the same way asnroff
ortroff
, for example.
To display a file with simple formatting, the format of the command is:
pr
filename
The
filename
entry can be the name of the file, the relative
pathname of the file, the full pathname of the file, or a list of
filenames separated by spaces.
The format you use
depends on where the file is located in relation to your current directory.
You may also use pattern-matching characters to specify files. See
Chapter 2
for information on pattern matching.
Used
without any options, the
pr
command does the following:
When you use the
pr
command to display a file,
its contents may scroll off your screen too
quickly for you to read them. When this happens, you can view the
formatted file by using the
pr
command along with the
more
command.
The
more
command instructs the system to pause at the end of each
screenful of text.
For example, to display a long file called
report
so that it pauses when
the screen is full, enter the following command:
$
pr report | more
When the system pauses at the first screen of text,
press the Space bar
to display the next screen. The previous command uses the
pipe symbol (|) to take the output from the
pr
command
and use it as input to the
more
command. For more information on
pipes, see
Section 7.4.2.
Sometimes you may prefer to display a file in a more sophisticated format. You can use a number of flags in the command format to specify additional formatting features. Table 3-3 explains several of these flags.
Flag | Action |
+
|
Begins formatting on page number
page .
Otherwise,
formatting begins on
page 1.
For example, the
|
-
|
Formats page into
columns. Otherwise,
pr
formats pages with one column.
For example, the
|
-m
|
Formats all specified files at the same time, side-by-side,
one per column.
For example, the
|
-d
|
Formats double-spaced output. Otherwise, output is
single-spaced.
For example, the
|
-f
|
Uses a formfeed character to advance to a new page. (Otherwise,
pr
issues a sequence of linefeed characters.) Pauses before
beginning the first page if the standard output is a terminal.
|
-F
|
Uses a formfeed character to advance to a new page. (Otherwise, issues a sequence of linefeed characters.) Does not pause before beginning the first page if the standard output is a terminal. |
-w
|
Sets line width to
num
columns. Otherwise, line
width is 72 columns.
For example, the
|
-o
|
Offsets (indents) each line by
num
column
positions. Otherwise, offset is 0 (zero) column positions.
For example, the
|
-l
|
Sets page length to
num
lines. Otherwise, page length is 66 lines.
For example, the
|
-h
|
Uses the specified string of characters, rather than the filename, in
the header (title) that is displayed at the top of every page.
If string includes blanks or special characters, it
must be enclosed in
' '
(single quotes).
For example, the
|
-t
|
Prevents
pr
from formatting headings and the blank
lines at the end of each page.
For example, the
|
-s
|
Separates columns with the character
char
rather than
with blank spaces. You must enclose special characters in single
quotes.
For example, the
|
You can use more than one flag at a time with the
pr
command. In
the following example, you instruct
pr
to format
file1
with these characteristics:
d
)
$
pr -dh 'My Novel' file1
For detailed information about
pr
and its flags, refer to the
pr
(1)
reference page.
Use the
lpr
command to send one or more files to the system
printer. The
lpr
command actually places files in a
print queue,
which is a list of files waiting to be printed. Once the
lpr
command places your files in the queue, you can
continue to do other work on your system while you wait for the files
to print.
The general format of the
lpr
command is:
lpr
filename
The
filename
entry can be the name of the file, the relative
pathname of the file, the full pathname of the file, or a list
of filenames
separated by spaces.
The format you use
depends on where the file is located in relation to your current directory.
You may also use pattern-matching characters to specify files. See
Chapter 2
for information on pattern matching.
If your system has more than one printer, use the following format to specify where you want the file to print:
lpr -P
printername filename
The
-P
flag indicates that you want to specify a printer. The
printername
entry is the name of a printer. Printers often have names such
as
lp0
,
lp1
,
and
lpn
.
Ask your system administrator for the printer names at your
facility.
If your system has more than one printer, one of them is the
default
printer. When you do not enter a specific
printername
,
your print
request goes to the default printer.
The following example shows how to use the
lpr
command to
print one or more files on a printer named
lp0
:
$
lpr -Plp0 file1
$
lpr -Plp0 file2 file3
$
The first
lpr
command sends
file1
to the
lp0
printer and then displays the
shell prompt: a dollar sign ($).
The second
lpr
command sends
file2
and
file3
to the same print queue, and then displays the shell
prompt before the files finish printing.
Several
lpr
command flags enable you to control the way in which
your file prints. Following is the general format for using a
flag with this command:
lpr
flag
filename
Table 3-4
explains some of the most useful
lpr
command flags.
Flag | Action |
-#
|
Prints
num
copies of the file. Otherwise,
lpr
prints one copy.
For example, the
lpr -#2 file1
command prints
two copies of
file1 .
|
-w
|
Sets line width to
num
columns. Otherwise, line
width is 72 columns. For example, the
lpr -w40 file1
command prints
file1
with lines that are 40 columns long.
|
-i
|
Offsets (indents) each line by
num
space
positions. Otherwise, offset is 8 spaces. For example, the
lpr -i5 file1
command prints
file1
with lines that are indented five spaces.
|
-p
|
Formats the file using
pr
as a filter.
|
-T
|
Uses the specified string of characters, rather than the filename, in
the header used by
pr .
Requires the
-p
option.
If the string includes blanks or special characters, it
must be enclosed in
' '
(single quotes). For example, the
lpr -p -T 'My Novel' file1
command specifies "My Novel"
as the title.
|
-m
|
Sends mail when the file completes printing. For example, the command
lpr -m file1
specifies that you want mail to be sent to you once
file1
prints.
|
Once you have entered the
lpr
command, your print request is entered into the print queue.
To see the position of the request
in the print queue, use the
lpq
command. To look at the print queue, enter:
$
lpq
If your request has already been printed, or if there are no requests in the print queue, the system responds with the following message:
no entries
If there are entries in the print queue, the system lists them and indicates which request is currently being printed. Following is a typical listing of print queue entries (your listing will vary):
Rank Owner Job Files Total Size active marilyn 489 report 8470 bytes 1st sue 135 letter 5444 bytes 2nd juan 360 (standard input) 969 bytes 3rd larry 490 travel 1492 bytes
The
lpq
command
displays the following for each print queue entry:
For example, Marilyn's report (job number 489) is currently being printed, and the requests of Sue, Juan, and Larry are pending.
When you print files, the position of the request in the queue as well as its size may help you estimate when your request may be finished. Generally, the higher the priority number in the queue and the larger the print request, the more time it will take.
If your system has more than one printer, use the following format to specify which print queue you want to see:
lpq -P
printername
filename
The
-P
flag indicates that you want to specify a print queue. The
printername
entry is the name of a particular printer. Use the
lpstat -s
command to learn the names of all the printers.
If you decide not to print your request, you can delete it
from the print queue by using the
lprm
command.
The general format of the
lprm
command is the following:
lprm
jobnumber
The
jobnumber
entry specifies the job number that the system has assigned to
your print request. You can see the job number by entering the
lpq
command.
For example, if Larry wants to cancel his print request, he can enter:
$
lprm 490
$
The
travel
file will be removed from the print queue.
A
link
is a connection between a filename and the
file itself. Usually, a file has one link - a
connection to its original filename.
However, you can use the
ln
(link) command to
connect a file to more than one filename at the same time.
Links are convenient whenever you need to work with the same data in more than one place. For example, suppose you have a file containing assembly-line production statistics. You use the data in this file in two different documents - in a monthly report prepared for management, and in a monthly synopsis prepared for the line workers.
You can link the statistics file to two different filenames, for
example,
mgmt.stat
and
line.stat
,
and place these filenames in two different directories. In this way,
you save storage space because you have only one copy
of the file. More importantly, you do not have to update multiple
files. Because
mgmt.stat
and
line.stat
are linked, editing one automatically
updates the other, and both filenames always refer to the same data.
There are two kinds of links available for your use: hard links and soft (symbolic) links.
With both hard and soft links, changes made to a file through one name appear in the file as seen through another name.
A major difference between hard and soft links occurs when removing them. A file with hard-linked names persists until all its names have been removed. A file with soft-linked names vanishes when its original name has been removed; any remaining soft links then point to a nonexistent file. See Section 3.4.5.
The term file system as used in this discussion of links differs from its earlier usage in this book. Previously, a file system was defined as a useful arrangement of files into a directory structure. Here, the same term acquires a more precise meaning: the files and directories contained within a single disk partition. A disk partition is a physical disk, or a portion of one, that has been prepared to contain file directories.
You can use the
df
command to discover the name of the disk
partition that holds any particular directory on your operating system.
Here is an example in which
df
shows that the directories
/u1/info
and
/etc
are in different file systems, but that
/etc
and
/tmp
are in the same file system:
$
df /u1/info
Filesystem 512-blks used avail capacity Mounted on /dev/rz2c 196990 163124 14166 92% /u1
$
df /etc
Filesystem 512-blks used avail capacity Mounted on /dev/rz3a 30686 19252 8364 70% /
$
df /tmp
Filesystem 512-blks used avail capacity Mounted on /dev/rz3a 30686 19252 8364 70% / $
To link files in the same file system, use the following command format:
ln
/dirname1/filename1
/dirname2/filename2
The
/dirname1/filename1
entry is the pathname of an existing file.
The
/dirname2/filename2
entry is the pathname of a new filename to be linked to
the existing
/dirname1/filename1
.
The
dirname1
and
dirname2
arguments
are optional if you are linking files in the same directory.
If you want to link files and directories across file systems,
you can create
symbolic links.
To create
a symbolic link, add an
-s
flag to the
ln
command sequence and specify the full pathnames of
both files.
The
ln
command for symbolic links takes the following form:
ln -s
/dirname1/filename1
/dirname2/filename2
The
/dirname1/filename1
entry is the pathname of an existing file.
The
/dirname2/filename2
entry is a pathname of a new filename in a different file system.
In the following example, the
ln
command links the new
filename
checkfile
to the existing file named
file3
:
$
ln file3 checkfile
$
Now use the
more
command to verify that
file3
and
checkfile
are two names for the same file:
$
more file3
The system displays the following:
You will find that vi is a useful editor that has many features. $
Now display the text of
checkfile
:
$
more checkfile
You will find that vi is a useful editor that has many features. $
Notice that both
file3
and
checkfile
contain
the same information. Any change that you make to the file under one
name will show up when you access the file by its other name.
Updating
file3
,
for example, will also update
checkfile
.
If your two files were located in directories that are in two
different file systems, you need to create a symbolic link
between them.
For example, to link a file called
newfile
that is in
the
/reports
directory to the file called
mtgfile
in the
/summary
directory, you can create a symbolic link
by using the following:
$
ln -s /reports/newfile /summary/mtgfile
$
The information in both files is still updated in the same manner as previously explained.
Each file has a unique identification number, called an i-number. The i-number refers to the file itself - data stored at a particular location - rather than to the filename. The i-number distinguishes the file from other files within the same file system.
A directory entry is a link between an i-number that represents a physical file and a filename. It is this relationship between files and filenames that enables you to link multiple filenames to the same physical file - that is, to the same i-number.
To display the i-numbers of files in your current directory, use the
ls
command with the
-i
(print i-number) flag in the following format:
ls -i
Examine the identification numbers of the files in your login directory. The number preceding each filename in the listing is the i-number for that file.
$
ls -i
1079 checkfile 1077 file1 1078 file2 1079 file3 $
The i-numbers in your listing will differ from those shown in
this example. However, the important thing to note is the identical
i-numbers for
file3
and
checkfile
,
the two
files linked in the previous example. In this case, the i-number is
1079.
Because an i-number represents a file within a particular filesystem, hard links cannot exist between separate file systems.
The situation is entirely different with symbolic links, where the link becomes a new file with its own, new i-number. The symbolic link is not another filename on the original file's i-number, but instead is a separate file with its own i-number. Because the symbolic link refers to the original file by name, rather than by i-number, symbolic links work correctly between separate file systems.
The
rm
(remove file) command does not always remove a
file. For example, suppose that a file
is linked to more than one filename; that is, several names refer to
the same i-number. In this case,
the
rm
command removes the link between the i-number and
that filename, but leaves the physical file intact. The
rm
command actually removes a physical file only after it has removed the
last link between that file and a filename, as shown in
Figure 3-1.
When a symbolic link is removed, the filename specifying the pointer
to the destination file or directory is removed.
For detailed information about the
rm
command, refer to
Section 3.9.
To display both the i-numbers and the number of filenames linked to a
particular i-number,
use the
ls
command with the
-i
(print i-number)
and the
-l
(long listing) flags in the following format:
ls -il
Examine the links in your login directory. Remember that the
i-numbers displayed on your screen will differ from those shown in
the example and that your username and your group's name will replace
the
larry
and
system
entries.
$
ls -il
total 3 1079 -rw-r--r-- 2 larry system 65 Jun 5 10:06 checkfile 1077 -rw-r--r-- 1 larry system 101 Jun 5 10:03 file1 1078 -rw-r--r-- 1 larry system 75 Jun 5 10:03 file2 1079 -rw-r--r-- 2 larry system 65 Jun 5 10:06 file3 1080 drwxr-xr-x 2 larry system 32 Jun 5 10:07 project $
Again, the first number in each entry shows the i-number for that filename. The second element in each line shows the file permissions, described in detail in Chapter 5.
The third field for each entry, the number to the left of the username,
represents the number of links to that i-number. Notice that
file3
and
checkfile
have the same i-number,
1079, and that both show two links. Each time the
rm
command removes a filename, it reduces the number of links to that
i-number by one.
In the following example, use the
rm
command to remove the
filename
checkfile
.
$
rm checkfile
$
List the contents of the directory with the
ls -il
command. Notice that the
rm
command has reduced the
number of links to i-number 1079, which is the same
i-number to which
file3
is linked, by one.
$
ls -il
total 3 1077 -rw-r--r-- 1 larry system 101 Jun 5 10:03 file1 1078 -rw-r--r-- 1 larry system 75 Jun 5 10:03 file2 1079 -rw-r--r-- 1 larry system 65 Jun 5 10:06 file3 1080 drwxr-xr-x 2 larry system 32 Jun 5 10:07 project $
This section provides information about how to copy files on a local system. For information about copying files to and from remote systems see Chapter 12 and Chapter 14.
The
cp
(copy) command copies a file from one filename to another filename
in your current directory or copies the file from one directory
to another directory.
The
cp
command is especially useful to make backup copies of
important files. Because the backup and the original are two
distinct files,
you can make changes to the original while still maintaining an
unchanged copy in
the backup file. This is helpful in case something happens to
the original
version. Also, if you decide you do not want to save your most recent
changes to the original file, you can begin again with the backup file.
Compare the
cp
command, which actually copies files, with the
ln
command, which creates multiple names for the same file.
Section 3.4
explains the
ln
command in detail.
Refer also to the
cp
(1)
and
ln
(1)
reference pages.
The format of the
cp
command is:
cp
source
destination
The
source
entry is the name of the file to be copied. The
destination
entry is the name of the file to which you want to copy
source
.
The
source
and
destination
entries can be filenames in
your current directory or pathnames to different directories.
This statement is true when you are copying files from one
directory to another. To copy the contents of an entire directory
to another directory (recursively, using the
-r
option), see
Section 4.4.
To copy files to a different directory, use the general format
of the
cp
command.
In this case,
source
is a series of one or more filenames and
destination
is a pathname that ends with the name of the target
directory. In the
source
entry you may also use pattern-matching
characters.
The
cp
command creates the destination file if it does not already
exist. However,
if a file with the same name as the destination file does exist,
cp
copies the source file over the existing destination
file.
Caution
If the destination file exists, your shell may allow the
cp
command to erase the contents of that file before it copies the source file. As a result, be certain that you do not need the contents of the destination file, or that you have a backup copy of the file, before you use it as the destination file for thecp
command. If you use the C shell, see Table 8-6 for thenoclobber
variable that can be set to prevent the erasure of the destination file.
In the following example, the destination file does not exist, so the
cp
command creates it. First, list the contents of your login directory:
$
ls
file1 file2 file3 project $
Copy the source file,
file2
,
into the new destination file,
file2x
:
$
cp file2 file2x
$
List the contents of the directory to verify that the copying process was successful:
$
ls
file1 file2 file2x file3 project $
You need a subdirectory to work through the following
example, so create one called
reports
with the
mkdir
command:
$
mkdir reports
$
To copy the
file2
file into the
reports
directory, enter:
$
cp file2 reports
$
List the contents of
reports
to verify that it contains a copy of
file2
:
$
ls reports
file2 $
You can also use the
cp
command to copy multiple files from one
directory into another directory. The format of the command is:
cp
filename1
filename2
dirname
In the following example, enter the
cp
command to copy both
file2
and
file3
into the
reports
directory, and
then list the contents of that directory:
$
cp file2 file3 reports
$
ls reports
file2 file3 $
In the above example, you do not have to specify
file2
and
file3
as part of the
dirname
entry because
the files being copied are retaining their original filenames.
You may also use pattern-matching characters to copy files. For example,
to copy
file2
and
file3
into
reports
,
enter:
$
cp file* reports
$
To change the name of a file when you copy it into another directory,
enter the
name of the source file (the original file), the directory name, a slash
(/), and then the new filename.
In the following example, copy
file3
into the
reports
directory under the new name
notes
,
and list the contents of the
reports
directory:
$
cp file3 reports/notes
$
ls reports
file2 file3 notes $
You can use the
mv
(move) command to perform the following
actions:
The format of the
mv
command is:
mv
oldfilename
newfilename
The
oldfilename
entry is the name of the file you want to move or
rename. The
newfilename
entry is the new name you want to assign to
the original file. Both
entries can be names of
files in the current directory, or pathnames to files in a different
directory. You may also use pattern-matching characters.
The
mv
command links a new name to an existing i-number and breaks
the link between the old name and that i-number.
It is useful to compare the
mv
command with the
ln
and
cp
commands, which
are explained in
Section 3.4
and
Section 3.5.
Refer also to the reference pages for these commands.
In the following example, first list the i-number of each file in your current
directory with the
ls -i
command. Next, enter the
mv
command to change the name of file
file2x
to
newfile
.
The i-numbers displayed on your screen
will differ from the numbers in the example:
$
ls -i
1077 file1 1088 file2x 1080 project 1078 file2 1079 file3 1085 reports
$
mv file2x newfile
$
Again, list the contents of the directory:
$
ls -i
1077 file1 1079 file3 1080 project 1078 file2 1088 newfile 1085 reports $
Note two things in this example:
mv
command changes the name of file
file2x
to
newfile
.
file2x
)
and
newfile
is the same - 1088.
The
mv
command removes the connection between i-number 1088 and filename
file2x
,
replacing it with a connection between i-number 1088 and
filename
newfile
.
However, the command does not change the file itself.
You can also use the
mv
command to move one or more files from your
current directory into a different directory.
Note
Type the target directory name carefully because the
mv
command does not distinguish between filenames and directory names. If you enter an invalid directory name, themv
command takes that name as a new filename. The result is that the file is renamed rather than moved.
In the following example, the
ls
command lists the contents of your
login directory. The
mv
command moves
file2
from
your current directory into the
reports
directory. The
ls
command then verifies that the file has been removed:
$
ls
file1 file2 file3 newfile project reports
$
mv file2 reports
$
ls
file1 file3 newfile project reports $
List the contents of the
reports
directory to verify that the
command has moved the file:
$
ls reports
file2 file3 notes $
You may also use pattern-matching characters to move files. For example,
to move
file1
and
file3
into
reports
,
you could enter the following command:
$
mv file* reports
$
Now list the contents of your login directory to verify that
file1
and
file3
have been moved:
$
ls
newfile project reports $
Copy
file1
,
file2
,
and
file3
back into your login directory. The
dot (.) in the following command line
specifies the current directory, which in this case is your login
directory:
$
cp reports/file* .
$
Verify that the files are back in your login directory:
$
ls
file1 file2 file3 newfile project reports $
Lastly, verify that
file1
,
file2
,
and
file3
are
still in the
reports
directory:
$
ls reports
file1 file2 file3 newfile project reports $
You can compare the contents of text files with the
diff
command. Use the
diff
command when you want to pinpoint the differences in the contents of
two files that are expected to be
somewhat different.
The format of the
diff
command is:
diff
file1
file2
The
diff
command scans each line in both files looking for differences.
When the
diff
command finds a line (or lines) that differ, for each line that is
different the following information is reported:
If the change is caused by an addition,
diff
displays:
l[,l] a r[,r]
where
l
is a line number in
file1
and
r
is a line number in
file2
.
The
a
indicates an addition.
If the difference was a deletion,
diff
would
specify a
d
;
if the difference was a change to a line,
diff
would
specify a
c
.
The actual differing lines follow. In the leftmost column, a
left angle bracket (<)
indicates lines from
file1
,
and a right angle bracket
(>) indicates
lines from
file2
.
For example, suppose that you want to quickly compare the following
meeting rosters in the files
jan15mtg
and
jan22mtg
:
jan15mtg
|
jan22mtg
|
alice | alice |
colleen | brent |
daniel | carol |
david | colleen |
emily | daniel |
frank | david |
grace | emily |
helmut | frank |
howard | grace |
jack | helmut |
jane | jack |
juan | jane |
lawrence | juan |
rusty | lawrence |
soshanna | rusty |
sue | soshanna |
tom | sue |
tom |
Instead of tediously comparing the list by sight, you can use the
diff
command to compare
jan15mtg
with
jan22mtg
as follows:
$
diff jan15mtg jan22mtg
2a3,4 > brent > carol 10d11 < howard $
Here we find that
Brent and Carol attended the meeting on January 22, and
Howard did not. We know this because the
line number and text output indicate that
brent
and
carol
are additions to file
jan22mtg
and that
howard
is a deletion.
In cases where there are no differences between files, the system will
merely return your prompt. For more information, see the
diff
(1)
reference page.
You can sort the contents of text files with the
sort
command. You can use this command to sort a single file or multiple files.
The format of the
sort
command is:
sort
filename
The
filename
entry can be the name of the file, the relative
pathname of the file, the full pathname of the file, or a list of filenames
separated by spaces.
You may also use pattern-matching characters to specify files. See
Chapter 2
for information about pattern matching.
A good example of what the
sort
command can do for you is to sort a list of names and put them in
collated order as defined by your current locale. For example, assume
that you have lists of names that are
contained in three files,
list1
,
list2
,
and
list3
:
list1
|
list2
|
list3
|
Zenith, Andre | Rocca, Carol | Hamilton, Abe |
Dikson, Barry | Shepard, Louis | Anastio, William |
D'Ambrose, Jeanette | Hillary, Mimi | Saluccio, William |
Julio, Annette | Chung, Jean | Hsaio, Peter |
To sort the names in all three files, enter:
$
sort list*
Anastio, William Chung, Jean D'Ambrose, Jeanette Dickson, Barry Hamilton, Abe Hillary, Mimi Hsaio, Peter Julio, Annette Rocca, Carol Saluccio, Julius Shepard, Louis Zenith, Andrew $
You can also capture the sorted list by redirecting the screen output to a file that you name by entering:
$
sort list* >newlist
$
For more information about redirecting output, see
Chapter 6.
For a detailed
description of the
sort
command and its many options, see the
sort
(1)
reference page.
When you no longer need a file, you can remove it with the
rm
(remove file) command. Use this command to remove a single file
or multiple files.
The format of the
rm
command is:
rm
filename
The
filename
entry can be the name of the file, the relative
pathname of the file, the full pathname of the file, or a list
of filenames. The format you use
depends on where the file is located in relation to your
current directory.
In the following example, you remove the file called
file1
from your
login directory.
First, return to your login directory with the
cd
(change directory)
command. Next, enter the
pwd
(print working directory) command to
verify that your login directory is your current directory, and
then list its contents.
Remember that the system substitutes the name of your login directory
for the notation
/u/uname
in the example.
$
cd
$
pwd
/u/uname
$
ls
file1 file2 file3 newfile project reports $
Enter the
rm
command to remove
newfile
,
and then list
the contents of the directory to verify that the system has removed the file.
$
rm newfile
$
ls
file1 file2 file3 project reports $
You must have permission to access a directory before you can remove files from it. For information about directory permissions, see Chapter 5.
Note
In addition to removing one or more files,
rm
also removes the links between files and filenames. Therm
command actually removes the file itself only when it removes the last link to that file. For information about using therm
command to remove links, see Section 3.4.5.
You can remove more than one file at a time with the
rm
command by
using pattern-matching characters. See
Chapter 2
for a
description of pattern-matching characters.
For example, suppose your current directory contains the
following files:
receivable.jun
,
payable.jun
,
payroll.jun
,
and
expenses.jun
.
You can remove all four of these files with the
rm *.jun
command.
Caution
Be certain that you understand how the
*
pattern-matching character works before you use it. For example, therm *
command removes every file in your current directory. Be especially careful with*
at the beginning or end of a filename. If you mistakenly enterrm *
name
instead ofrm
*name
, you will remove all your files, rather than just those ending withname
. (If your system is backed up on a regular basis, your system administrator can help you recover lost files.)
You may prefer to use the
-i
flag
with the
rm
command, which prompts you for verification before
deleting a file or files. See the end of this section for details.
You can also use the pattern-matching question mark (?) character
with the
rm
command to remove files whose names are the same, except for a
single character. For example, if your current directory
contains the files
record1
,
record2
,
record3
,
and
record4
,
you can remove all four files with the
rm record?
command.
For detailed information about pattern-matching characters, see Chapter 2.
When using pattern-matching characters, you may find the
-i
(interactive) flag
of the
rm
command particularly useful.
The
rm -i
command allows you to selectively delete files.
For each file selected by the command, the operating system
asks whether or not you want to delete or retain the file.
For example, suppose that your directory contains the files
record1
,
record2
,
record3
,
record4
,
record5
,
and
record6
.
Create those files now in your login directory by using the
touch
command as follows:
$
touch record1 record2 record3 record4 record5 record6
$
The
touch
command is useful when you want to create empty files, as you are now.
For complete information on the
touch
command, see the
touch
(1)
reference page.
If you want to remove four of the six files that
begin with the characters
record
,
enter:
$
rm -i record?
rm: remove record1?
n
rm: remove record2?
y
rm: remove record3?
y
rm: remove record4?
y
rm: remove record5?
y
rm: remove record6?
n
$
In the preceding example, you have deleted all files except for
record1
and
record6
.
Note
In addition to removing one or more files, the
rm
command also provides an option, the-r
flag, that removes files and directories at the same time. See Chapter 4 for more information.
Use the
file
command when you want to see what kind of data a file contains without
having to display its contents.
The
file
command displays whether the file is one of the following:
troff
,
nroff
,
or
eqn
input text
The
file
command is especially useful when you suspect that a file contains a
compiled program. Displaying the contents of a compiled
program can produce disconcerting results on your screen.
The format of the
file
command is:
file
filename
The
filename
entry can be the name of the file, the relative
pathname of the file, the full pathname of the file, or a list
of filenames. The format you use
depends on where the file is located in relation to your current
directory.
You may also use pattern-matching characters to specify files. See
Chapter 2
for information on pattern matching.
For example, to determine the file type of entries in your login directory, enter the following:
$
cd
$
pwd
/u/uname
$
file *
file1: ascii text file2: ascii text file3: ascii text project: directory record1: empty record6: empty reports: directory $
The
file
command has identified
file1
,
file2
,
and
file3
as ASCII text files,
project
and
reports
as directories,
and
record1
and
record6
as empty files.
For more information on the
file
command, see the
file
(1)
reference page.