Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
3.  Working With Files and Directories Directories and Hierarchy Copying Directories  Previous   Contents   Next 
   
 

Removing Directories (rmdir)

To remove an empty directory, use the rmdir command as follows:

$ rmdir veggies3
$ 

If the directory still contains files or subdirectories, the rmdir command does not remove the directory.

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r.

$ rm -r veggies3
$

Caution - Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.


Viewing Differences Between Files (diff)

Use the diff command to view differences between similar files. The following command scans each line in leftfile and rightfile to check for differences.

$ diff leftfile rightfile

When the diff utility finds a line or lines that differ, diff determines if the difference is the result of an addition, a deletion, or a change to the line, and how many lines are affected. diff tells you the respective line number or numbers in each file, followed by the relevant text from each file.

If the difference is the result of an addition, diff displays a line with the following format.

   l[,l] a r[,r]

In the previous example, l is a line number in leftfile and r is a line number in rightfile.

If the difference is the result of a deletion, diff uses a d in place of a. If the difference is the result of a change on the line, diff uses a c.

The relevant text from both files immediately follow the line number information. Text from leftfile is preceded by a left angle bracket (<). Text from rightfile is preceded by a right angle bracket (>).

This example shows two sample files, followed by their diff output.

$ cat sched.7.15
Week of 7/15

Day:  Time:        Action Item:          Details:

T     10:00        Hardware mtg.         every other week
W     1:30         Software mtg.
T     3:00         Docs. mtg.
F     1:00         Interview
$ cat sched.7.22
Week of 7/22

Day:  Time:        Action Item:          Details:

M     8:30         Staff mtg.            all day
T     10:00        Hardware mtg.         every other week
W     1:30         Software mtg.
T     3:00         Docs. mtg.
$ diff sched.7.15 sched.7.22
1c1
< Week of 7/15
---
> Week of 7/22
4a5
> M     8:30         Staff mtg.            all day
8d8
< F     1:00         Interview

If the two files to be compared are identical, diff does not display output.

For more information on the diff(1) command, refer to the man Pages(1): User Commands.

Comparing Three Different Files (diff3)

To compare three different versions of a file, use the diff3 command.

$ diff3 file1 file2 file3

diff3 compares three versions of a file and publishes the differing ranges of text that are flagged with these codes:

==== all three files differ

====1 file1 is different

====2 file2 is different

====3 file3 is different

Using bdiff on Large Files

If you are comparing large files, use bdiff instead of diff. Use the diff command syntax with bdiff.

$ bdiff leftfile rightfile

Use bdiff instead of diff for files longer than 3500 lines.

Searching for Files (find)

The find command searches for files that meet conditions you specify, starting from a directory you name. For example, you might search for file names that match a certain pattern or that have been modified within a specified time frame.

Unlike most commands, find options are several characters long. You must specify the starting directory before your desired options.

$ find directory options

In the previous example, directory is the name of the starting directory and options represents the options for the find command.

Each option describes a criterion for selecting a file. A file must meet all criteria to be selected. The more options you apply, the narrower the field becomes. The -print option indicates that you want the system to display the results.

The -name filename option tells find to select files that match filename. Here filename is taken to be the rightmost component of a file's full path name. For example, the rightmost component of the file /usr/bin/calendar is calendar. This portion of a file's name is often called the base name.

For example, to see which files within the current directory and its subdirectories end in s, type the following command.

$ find . -name '*s' -print
./programs
./programs/graphics
./programs/graphics/gks
./src/gks
$

The following table describes other options of the find command.

Table 3-1 find Options

Option

Description

-name filename

Selects files with a rightmost component that matches filename. Surround filename with single quotes if it includes filename substitution patterns.

-user userid

Selects files that are owned by userid. userid can be either a login name or user ID number.

-group group

Selects files that belong to group.

-m -time n

Selects files that have been modified within n days.

-newer checkfile

Selects files that have been modified more recently than checkfile.

 

You can specify an order of options by combining options within escaped parentheses (for example, \(options\) ). Within escaped parentheses, you can use the -o flag between options to indicate that find should select files that qualify under either category, rather than just those files that qualify under both categories.

$ find . \( -name AAA -o -name BBB \) -print
./AAA
./BBB

In the previous example, the find command searches in the . directory for all files that are named AAA, then looks for all files named BBB. find then displays the results of both searches.

You can invert the sense of an option by including an escaped exclamation point before the option. find then selects files for which the option does not apply:

$ find . \!-name BBB -print
./AAA

You can also use find to apply commands to the files it selects with the following options.

-exec command '{}' \;

You terminate this option with an escaped semicolon (\;). The quoted braces are replaced with the file names that find selects.

You can use find to automatically remove temporary work files. If you name your temporary files consistently, you can use find to search for and remove these files. For example, if you name your temporary files junk or dummy, this command finds and removes the files.

$ find . \( -name junk -o -name dummy \) -exec rm '{}' \;

For more information on searching for files, see the man page find(1).

 
 
 
  Previous   Contents   Next