Solaris Advanced User's Guide

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 – 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.

Displaying File Type (file)

Some files, such as binary files and executable files, are not printable and cannot be displayed on the screen. Use the file command to show the file type.

$ file *
myscript:      executable shell script      PostScript document
save.txt:      ascii text