Sun Microsystems, Inc.
spacer |
black dot
  Previous   Contents   Next 
Chapter 3

Working With Files and Directories

You can use the SunOS command line to work with, organize, and manage files and directories. You type the file and directory names with SunOS commands to complete specific operations. The command line operates differently than a desktop File Manager. In a File Manager, you can display files as icons that you can click on and move, and you can select commands from menus.

This chapter describes the following topics.

File Concepts

A file is the basic unit in the SunOS operating system. Almost everything is treated as a file, including the following items.

  • Documents - These items include text files, such as letters or reports, computer source code, or any other document that you write and want to save.

  • Commands - Most commands are executable file. That is, they are files you can execute to run a particular program. For example, the date command in the previous chapter, which executes a program that provides the current date, is an executable file.

  • Devices - Your terminal, printer, and disk drive(s) are all treated as files.

  • Directories - A directory is simply a file that contains other files.

The following section explains the commands for creating, listing, copying, moving, and deleting files. This section also includes information on how to list the contents of a file and how to determine the nature of a file.

Using File Commands

Each of the command descriptions in this section includes an example of how to use the command. Try the examples as you read the text.

Before You Begin

Before you experiment with files, make sure that you are in your home directory. Your system administrator established this directory for you at your account creation. To avoid changes to parts of your system that other users expect to remain unchanged, perform the following tasks in your home directory.

To make certain that you are in your home directory, type the cd (change directory) command. This command moves you to your home (default) directory. Then type the pwd (print working directory) command to display your current location within the file system. The directory displayed is your home directory:

$ cd
$ pwd

In this example, the user's home directory is /export/home/username, where username is the name of the user who owns the home directory.

Creating a Test File

Use the touch command to create an empty file.

$ touch tempfile

If a file by the name you specify does not exist, the touch command creates an empty file.

Note - If the file already exists, touch updates the last file access time.

Listing Files (ls)

Now list the file with the ls command to verify that you've created it:

$ ls tempfile

When you type the ls command by itself, it lists all the files in your current location. If you type the ls command with a specific file name, it lists only that file, if the file exists.

For more information on listing files, see the man page ls(1).

Copying Files (cp)

Use the cp command to copy tempfile to a file called copyfile:

$ cp tempfile copyfile

Now list both files. Notice that both names end with the characters "file." You can use the wildcard character, asterisk (*), to match any character or sequence of characters. The command ls *file lists both tempfile and copyfile, and any other file in this directory with a name that ends with file.

$ ls *file
copyfile    tempfile

Notice that copyfile is listed first. Files are listed in alphabetical order. Capital letters and numbers precede lowercase letters.

For detailed information on copying files, see the man page cp(1).

Moving and Renaming Files (mv)

You can move and rename files by using the same command, mv (move). In this example, use the mv command to rename tempfile to emptyfile:

$ mv tempfile emptyfile

Then list both files again to verify the change.

$ ls *file
copyfile    emptyfile

tempfile is replaced by emptyfile.

For more information on moving and renaming files, see the man page mv(1).

Deleting Files (rm)

Use the rm (remove) command to delete copyfile, and verify the result with the ls command:

$ rm copyfile
$ ls *file

Caution - Be careful when you use the rm command, and be particularly careful when you use rm with the wildcard character (*). You cannot recover files that you have removed with rm.

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

Displaying File Contents (more, cat)

Use the more command to display the contents of a file. Type more and follow it with the name of the file to be displayed. The contents of the file scroll down the screen. If the file is longer than one screen, this message appears:


where nn is the percentage of the file already displayed.

You can also use the cat command to display the contents of a file, but it displays the file contents rapidly without pausing. The cat (concatenate) command is more often used to join two or more files into one large file, as in this example:

$ cat file1 file2 file3 > bigfile
$ ls *file

For further information on the more(1) or cat(1) commands, refer to the man Pages(1): User Commands.

  Previous   Contents   Next