[ Team LiB ] Previous Section Next Section

Creating and Editing Files (cat, touch, cp, mv, vi)

This section describes how to create and edit files with the cat, touch, cp, mv, Text Editor, and vi commands.

Using the cat Command

Use the cat command to create short files or to append a small amount of text to an existing file. Use the following steps to create files with the cat command.

  1. Type cat > filename and press Return.

  2. Type one or more lines of text into the new file.

  3. When you've completed the text, press Return to start a new line.

  4. Press Control-D on a line that contains no text.

    The text is saved and the shell prompt is redisplayed.

Use the following steps to append text to an existing file.

  1. Type cat >> filename and press Return.

  2. Type one or more lines of text into the new file.

  3. When you've completed the text, press Return to start a new line.

  4. Press Control-D on a line that contains no text.

    The text is saved and the shell prompt is redisplayed.

To view the contents of the file, type cat filename and press Return. The contents of the file are displayed. If the file is too long to fit in the terminal window, it flies by and shows you the lines at the end of the file that fit in the window or on the screen.

The following example creates a file named kookaburra with the first verse of the kookaburra song, displays the contents of the file, appends the second verse to the file, and displays the contents again.


castle% cat > kookaburra
Kookaburra sits in the old gum tree
Merry merry king of the bush is he
Laugh kookaburra, laugh kookaburra
Gay your life must be.
^D
castle% cat kookaburra
Kookaburra sits in the old gum tree
Merry merry king of the bush is he
Laugh kookaburra, laugh kookaburra
Gay your life must be.

castle% cat >> kookaburra
Kookaburra sits in the old gum tree
Eating all the gumdrops he can see
Stop kookaburra, stop kookaburra
Leave some there for me.
^D
castle% cat kookaburra
Kookaburra sits in the old gum tree
Merry merry king of the bush is he
Laugh kookaburra, laugh kookaburra
Gay your life must be.

Kookaburra sits in the old gum tree
Eating all the gumdrops he can see
Stop kookaburra, stop kookaburra
Leave some there for me.
castle%



Using the touch Command

The touch command sets the access and modification times for each file to the current time. If a file does not exist, an empty one is created. You can use the touch command to create an empty file to check the default permissions and ownership or to create a file to which you add text at a later time.

To create an empty file, type touch filename and press Return. A new, empty file is created. If the file exists, then its modification time is updated to the current date and time.

The following example uses the ls command to determine that there is not a file named junk, creates the file, and uses the ls command to verify that the empty file is created.


oak% ls -l junk
junk:  No such file or directory
oak% touch junk
oak% ls -l junk
-rw-r--r-- 1 irving      staff 0 Sep 11 15:06 junk
oak%



Copying (cp) or Renaming (mv) an Existing File

graphics/new.gif

With the cp command, you can perform the following actions.

  • Make copies of individual files and assign new names to them.

  • Copy one or more files into a different existing directory, keeping the existing file names.

  • Recursively copy an entire directory structure to another directory.

  • Copy extended file attributes.

    In the Solaris 9 Operating Environment, the UFS, NFS, and TMPFS file systems are enhanced to include extended file attributes. These file attributes enable application developers to associate specific attributes with a file. For example, a developer of a file management application for a windowing system might choose to associate a display icon with a file.

graphics/new.gif

The options for both /usr/bin/cp and /usr/xpg4/bin/cp are listed in Table 22.

Table 22. Options for /usr/bin/cp and /usr/xpg4/bin/cp

Option

Description

@

Preserve extended attributes.cp tries to copy all of the extended attributes of the source file along with the file data to the destination file. Extended attributes are new in the Solaris 9 release.

-f

If a file descriptor for a destination file cannot be obtained, try to unlink the destination file and proceed.

-i

Interactively prompt for confirmation whenever the copy would overwrite an existing target. A y answer means proceed with the copy. Any other answer prevents cp from overwriting target.

-p

/usr/bin/cp only. Duplicate not only the contents of source-file, but also preserve ID, permission modes, modification and access time, and ACLs if applicable. Note that the command may fail if you copy ACLs to a file system that does not support them. The command does not fail if unable to preserve modification and access time or permission modes. If unable to preserve owner and group ID, cp does not fail and it clears S_ISUID and S_ISGID bits in the target. Print a diagnostic message to standard error and return a non-zero exit status if unable to clear these bits.

To preserve the owner and group ID, permission modes, and modification and access times, users must have the appropriate file access permissions; this includes being superuser or the same owner ID as the destination file.

-p

/usr/xpg4/bin/cp only. Same as -p, above, except the command fails if unable to duplicate the modification and access time or the permission modes. Print a diagnostic message to standard error and return a non-zero exit status.

-r

Recursively copy the directory and all its files, including any subdirectories and their files, to target.

-R

Same as -r, except replicate pipes.

graphics/new.gif

To copy an existing file, type cp old-filename new-filename and press Return. You have made a copy of the file, retaining the original one. Suppose you need to modify the /etc/group file and you want to keep the original copy around in case you make a mistake. Just copy /etc/group to /etc/group.orig, as shown in the following example.


# cp /etc/group /etc/group.orig
#

graphics/new.gif

To move (and rename) an existing file, type mv old-filename new-filename and press Return. You have changed the name of the file. Suppose you've downloaded the myapp.tar.gz file and think that it may be corrupt, although you're not completely certain. So, you move it out of the way so that you can download a fresh copy and still retain the original copy with a descriptive name, just in case.


oak% mv myapp.tar.gz myapp.tar.gz-possiblycorrupt
oak%



Using Text Editor

You can use the CDE Text Editor to create and edit files. To start Text Editor from the CDE front panel, click on the Applications menu and click on Text Editor. To start the CDE Text Editor from the command line, type /usr/dt/bin/dtpad& and press Return. A Text Editor window is displayed. Use the commands from the Edit menu or the Cut, Copy, Paste, and Undo keys from the keyboard to make editing changes.

Using vi

The visual editor, vi, is commonly used by system administrators to edit text files. Whole books have been written about using vi. This section provides only a quick-reference table with some of the most commonly used editing commands.

To start vi, type vi filename and press Return. If the file does not exist, a new file is opened. The new file is created when you save changes made to it. If the file exists, the beginning of the file is displayed.

graphics/new.gif

The three vi modes are described in Table 23.

Table 23. vi Modes

Mode

Description

Command

Normal and initial mode. Other modes return to command mode on completion. Use the ESC key to cancel a partial command.

Input

Enter input mode by setting any of the following options: a A i I o O c C s S R. You can then type arbitrary text. You usually exit input mode with an ESC character or, abnormally, with an interrupt.

Last line

Read input for : / ? or !. Terminate by pressing Return. An interrupt cancels termination.

Table 24 shows a few of the many vi editing commands.

Table 24. Some Basic vi Commands

Task

Command

How to save/quit a file.

 

Quit without saving changes.

:q!

Write changes.

:w

Write changes and quit.

:wq

Write changes and quit.

ZZ

How to move around in a file.

 

Move cursor one character left.

h

Move cursor one character right.

l

Move cursor up one line.

k

Move cursor down one line.

j

Go to end of the file.

G

How to add text.

 

Insert text (insert mode).

i text Esc

Append text at cursor location.

a text Esc

Append text at end of the line.

A text Esc

How to exit to command mode.

Esc

How to make changes to a file.

 

Delete line.

dd

Delete character.

x

Delete word.

dw

Open new line above.

O text Esc

Open new line below.

o text Esc

Yank/copy line.

Y

Put before.

P

Put after.

p

    [ Team LiB ] Previous Section Next Section