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.
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.
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 /export/home/username
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
$ 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)
$ ls tempfile 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)
$ 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.
Moving and Renaming Files (mv)
$ 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)
$ rm copyfile $ ls *file emptyfile
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 bigfile file1 file2 file3 $
For further information on the more(1) or cat(1) commands, refer to the man Pages(1): User Commands.