Working with Files

 < Day Day Up > 

The following sections describe utilities that copy, move, and print files.

cp: Copies a File

The cp (copy) utility (Figure 3-2) makes a copy of a file. This utility can copy any file, including text and executable program (binary) files. You can use cp to make a backup copy of a file or a copy to experiment with.

The cp command line uses the following syntax to specify source and destination files:

 cp source-file destination-file 

The source-file is the name of the file that cp will copy. The destination-file is the name that cp assigns to the resulting (new) copy of the file.

caution: cp can destroy a file

If the destination-file exists before you give a cp command, cp overwrites it. Because cp overwrites (and destroys the contents of) an existing destination-file without warning, take care not to cause cp to overwrite a file that you need. The cp i (interactive) option (see page 31 for a tip on options) prompts you before it overwrites a file.

The following example assumes that the file named orange.2 exists before you give the cp command. The user answers y to overwrite the file:

 $ cp   i orange orange.2 cp: overwrite 'orange.2'? y 

The cp command line in Figure 3-2 copies the file named memo to memo.copy. The period is part of the filename just another character. The initial ls command shows that memo is the only file in the directory. After the cp command, a second ls shows two files in the directory, memo and memo.copy.

Sometimes it is useful to incorporate the date in the name of a copy of a file. The following example includes the date January 30 (0130) in the copied file:

 $ cp memo memo.0130 

Although it has no significance to Linux, the date can help you find a version of a file that you created on a certain date. It can also help you avoid overwriting existing files by providing a unique filename each day. Refer to "Filenames" on page 78.

Use scp (page 758) or ftp (page 671) when you need to copy a file from one system to another on a common network.

mv: Changes the Name of a File

The mv (move) utility can rename a file without making a copy of it. The mv command line specifies an existing file and a new filename using the same syntax as cp:

 mv existing-filename new-filename 

Figure 3-2. cp copies a file
 $ ls memo $ cp memo memo.copy $ ls memo memo.copy 

Figure 3-3. mv renames a file
 $ ls memo $ mv memo memo.0130 $ ls memo.0130 

The command line in Figure 3-3 changes the name of the file memo to memo.0130. The initial ls command shows that memo is the only file in the directory. After you give the mv command, memo.0130 is the only file in the directory. Compare this result to that of the earlier cp example.

The mv utility can be used for more than changing the name of a file. Refer to "mv, cp: Moves or Copies a File" on page 90.

caution: mv can destroy a file

Just as cp can destroy a file, so can mv. Also like cp, mv has a i (interactive) option. See the caution box labeled "cp can destroy a file" on page 46.

lpr: Prints a File

The lpr (line printer) utility places one or more files in a print queue for printing. Linux provides print queues so that only one job is printed on a given printer at a time. A queue allows several people or jobs to send output simultaneously to a single printer with the expected results. On machines with access to more than one printer, you can use the P option to instruct lpr to place the file in the queue for a specific printer, including one that is connected to another machine on the network. The following command prints the file named report:

 $ lpr report 

Because this command does not specify a printer, the output goes to the default printer, which is the printer when you have only one printer.

The next command line prints the same file on the printer named mailroom:

 $ lpr -Pmailroom report 

You can see what jobs are in the print queue by using the lpq utility:

 $ lpq lp is ready and printing Rank  Owner   Job Files                 Total Size active alex    86 (standard input)        954061 bytes 

In this example, Alex has one job that is being printed; no other jobs are in the queue. You can use the job number (86 in this case) with the lprm utility to remove the job from the print queue and stop it from printing:

 $ lprm 86 

You can send more than one file to the printer with a single command. The following command line prints three files on the printer named laser1:

 $ lpr -Plaser1 05.txt 108.txt 12.txt 

grep: Finds a String

The grep (global regular expression print[1] ) utility searches through one or more files to see whether any contain a specified string of characters. It does not change the file it searches but simply displays each line that contains the string.

[1] Originally this utility's name was a play on an ed an original UNIX editor, available on Linux command: g/re/p. In this command the g stands for global, re is a regular expression delimited by slashes, and p means print.

The grep command in Figure 3-4 searches through the file memo for lines that contain the string credit and displays a single line that meets this criterion. If memo contained such words as discredit, creditor, or accreditation, grep would have displayed those lines as well because they contain the string it was searching for. The w option causes grep to match only whole words. You do not need to enclose the string you are searching for in single quotation marks, but doing so allows you to put SPACEs and special characters in the search string.

The grep utility can do much more than search for a simple string in a single file. Refer to page 683 for more information on grep.

Figure 3-4. grep searches for a string
 $ cat memo Helen: In our meeting on June 6 we discussed the issue of credit. Have you had any further thoughts about it?                        Alex $ grep 'credit' memo discussed the issue of credit. 

head: Displays the Beginning of a File

By default the head utility displays the first ten lines of a file. You can use head to help you remember what a particular file contains. For example, if you have a file named months that lists the 12 months of the year in order, one to a line, head displays Jan through Oct (Figure 3-5).

This utility can display any number of lines, so you can use it to look at only the first line of a file, at a full screen, or even more. To specify the number of lines head displays, include a hyphen followed by the number of lines in the head command. For example, the following command displays only the first line of months:

 $ head -1 months Jan 

The head utility can also display parts of a file based on a count of blocks or characters rather than lines. Refer to page 691 for more information on head.

tail: Displays the End of a File

The tail utility is similar to head but by default displays the last ten lines of a file. Depending on how you invoke it, this utility can display fewer or more than ten lines, use a count of blocks or characters rather than lines to display parts of a file, and display lines being added to a file that is changing. The following command causes tail to display the last five lines, Aug through Dec, of the months file shown in Figure 3-5:

Figure 3-5. head displays the first lines of a file
 $ cat months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec $ head months Jan Feb Mar Apr May Jun Jul Aug Sep Oct 

Figure 3-6. sort displays a file in order
 $ cat days Monday Tuesday Wednesday Thursday Friday Saturday Sunday $ sort days Friday Monday Saturday Sunday Thursday Tuesday Wednesday 

 $ tail -5 months Aug Sep Oct Nov Dec 

You can monitor lines as they are added to the end of the growing file named logfile with the following command:

 $ tail -f logfile 

Press the interrupt key (usually CONTROL-C) to stop tail and display the shell prompt. Refer to page 783 for more information on tail.

sort: Displays a File in Order

The sort utility displays the contents of a file in order by lines but does not change the original file. If you have a file named days that contains the name of each day of the week in order on a separate line, sort displays the file in alphabetical order (Figure 3-6).

The sort utility is useful for putting lists in order. The u option generates a sorted list in which each line is unique (no duplicates). The n option puts a list of numbers in order. Refer to page 762 for more information on sort.

Figure 3-7. uniq removes duplicate lines
 $ cat dups Cathy Fred Joe John Mary Mary Paula $ uniq dups Cathy Fred Joe John Mary Paula 

uniq: Removes Duplicate Lines from a File

The uniq (unique) utility displays a file, skipping adjacent duplicate lines, but does not change the original file. If a file contains a list of names and has two successive entries for the same person, uniq skips the extra line (Figure 3-7).

If a file is sorted before it is processed by uniq, this utility ensures that no two lines in the file are the same. (Of course, sort can do that all by itself with the u option.) Refer to page 812 for more information on uniq.

diff: Compares Two Files

The diff (difference) utility compares two files and displays a list of the differences between them. This utility does not change either file, so it is useful when you want to compare two versions of a letter or a report or two versions of the source code for a program.

The diff utility with the u (unified output format) option first displays two lines indicating which of the files you are comparing will be denoted by a plus sign (+) and which by a minus sign ( ). In Figure 3-8, a minus sign indicates the colors.1 file; a plus sign indicates the colors.2 file.

The diff u command breaks long, multiline text into hunks. Each hunk is preceded by a line starting and ending with two at signs (@@). This hunk identifier indicates the starting line number and the number of lines from each file for this hunk. In Figure 3-8, the 1,6 indicates that the hunk covers the section of the colors.1 file (indicated by a minus sign) from the first line and continuing for six lines (for a total of seven lines). Similarly the +1,5 indicates that the hunk covers colors.2 from the first line through five subsequent lines.

Figure 3-8. diff displaying the unified output format
 $ diff -u colors.1 colors.2 --- colors.1    Fri Nov 25 15:45:32 2005 +++ colors.2    Fri Nov 25 15:24:46 2005 @@ -1,6 +1,5 @@  red +blue  green  yellow -pink -purple  orange 

Following these header lines, diff u displays each line of text with a leading minus sign, plus sign, or nothing. The leading minus sign indicates that the line occurs only in the file denoted by the minus sign. The leading plus sign indicates that the line comes from the file denoted by the plus sign. A line that begins with neither a plus sign nor a minus sign occurs in both files in the same location. Refer to page 638 for more information on diff.

file: Tests the Contents of a File

You can use the file utility to learn about the contents of any file on a Linux system without having to open and examine the file yourself. In the following example, file reports that letter_e.bz2 contains data that was compressed by the bzip2 utility (page 56):

 $ file letter_e.bz2 letter_e.bz2: bzip2 compressed data, block size = 900k 

Next file reports on two more files:

 $ file memo zach.jpg memo:     ASCII text zach.jpg: JPEG image data, ... resolution (DPI), 72 x 72 

Refer to page 653 for more information on file.

     < Day Day Up > 

    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213 © 2008-2017.
    If you may any questions please contact us: