OpenWindows Advanced User's Guide

3.2 Using File Commands

Each of the commands presented in this section includes an example of how the command is used. Try the examples as you read the text. This practice will make the commands and their respective concepts easier to understand and remember.

3.2.1 Before You Begin

Before you start experimenting with files, make sure that you are in your home directory. This is a directory established for you by your system administrator when your account was created. If you perform the tasks shown in the following examples from your home directory, you'll be less likely to create, copy, move, or (worst of all) delete files within portions of the system that other users expect to remain unchanged.

To make certain that you are indeed in your home directory, type the cd (change directory) command by itself. This moves you to your home (default) directory. Then type the pwd (print working directory) command to display your current location within the filesystem. 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 owning the home directory.

3.2.2 Creating a Test File

Use the touch command to create an empty file. If a file by the name you specify doesn't already exist, the touch command creates an empty file (if the file already exists, touch updates the last file access time).

$ touch tempfile

3.2.3 Listing Files (ls)

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

$ ls tempfile

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

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

3.2.4 Copying Files (cp)


$ cp tempfile copyfile

Now try listing both files. Notice that both names end with the characters "file." You can use the wildcard character, asterisk (*), to stand for any character or sequence of characters. Therefore, the command ls *file should list 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 the cp(1) command, refer to the man Pages(1): User Commands.

3.2.5 Moving and Renaming Files (mv)

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

$ mv tempfile emptyfile

Now list both files again to verify the change:

$ ls *file
copyfile    emptyfile

As you can see, tempfile is replaced by emptyfile.

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

3.2.6 Deleting Files (rm)

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

$ rm copyfile
$ ls *file

Caution - Caution -

Once you delete a file, it is gone for good. Unless there is a backup copy, the file cannot be restored. Be careful when using the rm command, and be particularly careful when using rm with the wildcard character (*). Files removed with rm cannot be recovered.

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

3.2.7 Displaying File Contents (more, cat)

Use the more command to display the contents of a file. Type more followed by the name of the file to be displayed. The contents of the file scrolls down the screen. If the file is longer than one screen, this message appears:

--More--(nn%)  [Press space to continue, `q' to

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 flashes through the entire file 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.

3.2.8 Displaying File Type (file)

Some files, such as binary files and executable files, are not printable and cannot be displayed on the screen. The file command can be handy if you're not sure of the file type.

Use the file command to show the file type:

$ file copyfile
copyfile:    ascii text