The UNIX file system is where Solaris stores, organizes, and manages information. This hierarchical file system enables the user to organize files into directories. More in-depth information on Solaris file systems can be found in Chapter 14, “Managing File Systems.” One thing to keep in mind about UNIX is that it views directories, regular files, and devices (for example, printers, disks, tape drives, and terminal screens) all in the same way. There are many file types available in Solaris, including the following:
Regular or ordinary files These files hold data. These include ASCII text files, binary data, image data, databases, application-related data, and computer source code.
Directories Contrary to what it might seem, a directory does not contain other files. A directory is simply a file that associates filenames with I-node numbers (see Chapter 14 for more information on I-nodes). A list of files with their associated I-node numbers is the only type of data a directory can hold.
Symbolic links A symbolic link is a file that points to another file. A symbolic link contains the pathname of the file to which it points. Symbolic links can point to regular files, directories, or other symbolic links using absolute or relative pathnames.
Commands Most commands are simply executable files. That is, they are files you can execute to run a particular program. You’ll see that many of the commands I describe throughout this book are simply files located in the /usr/bin directory.
Devices Your terminal, printer, and disk drive(s) are devices that have special files associated with them. Device files differ from ordinary files in that they do not store data (they do not use data blocks); instead, they provide access to devices connected to the system. Device files are described in detail in Chapter 11.
Solaris provides several commands with which to list files on your system. You can list all of the files in your current directory or selectively list files based on their creation dates, owner, and just about any other criteria. For now, the simple way to list files is using the ls command. I’ll describe more advanced methods in later chapters.
To list the files in your current directory, you’ll use the ls command, which has the following syntax:
ls [options] <argument>
The options are many, but Table 1.2 lists a few you’ll use regularly.
The <argument> can be either a filename or a directory. Here’s an example of how to list all of the files in the current directory:
ls <cr>
The system responds with the following:
TT_DB dev home mnt proc var bin devices kernel net sbin vol cdrom etc lib opt tmp xfn data export lost+found platform usr
By not specifying an argument, ls listed files in the current directory.
Most times, we like to have more information about the files in a particular directory, so we add the –l option to produce a long listing, as follows:
ls –l / <cr>
The system responds with the following:
total 155 drwx------ 2 root other 512 Feb 27 10:39 Mail drwxr-xr-x 2 root root 512 Feb 27 10:08 TT_DB lrwxrwxrwx 1 root root 9 Feb 26 15:34 bin -> ./usr/bin drwxr-xr-x 2 root nobody 512 Mar 19 15:55 cdrom drwxr-xr-x 4 root root 512 Mar 27 17:12 data drwxr-xr-x 2 root other 512 Apr 18 13:45 data2 ……….. output has been truncated
You’ll notice that I specified a directory for the <argument> so that I would get a listing of everything in the root (/) directory, regardless of which directory I’m currently working in.
The information displayed in the long listing is in the form of columns and is as follows (reading from left to right):
The next nine characters in the first column describe the file’s permission mode, which is described in detail in Chapter 16.
Column 2 | Displays the number of links to the file. |
Column 3 | Displays the file’s owner. |
Column 4 | Displays the file’s group. |
Column 5 | Displays the file size in bytes. |
Column 6 | Date/time of the file’s last modification. |
Column 7 | Filename. |
Note: If the file is a symbolic link, the filename is followed by -> and the pathname of the referenced file.
Organizing files is an important task for the system administrator. The Solaris directory system is designed to help you organize files into folders called directories. A directory can contain information about files as well as other directories. The main-level directory is the parent directory, and the directories within that are called subdirectories. There is no limit to the depth of subdirectories, and we refer to this directory structure as a directory tree. I’ll get back to the topic of managing directories later in this section.
Before we create a file, I’ll outline a few of the file-naming rules you need to follow:
A filename can be 1 to 255 characters in length.
Any combination of letters and numbers can be used (that is, A–Z, a–z, 0–9).
Do not use the following characters in a filename: / “ ‘ * ; - ? [ ] @ ^ # & ! $ { } < > | + ~ % `
Do not use names that contain spaces.
Note
Although the shell will allow spaces, asterisks (*), ampersands (&), pipes (|), quotes (“ “), and dollar signs ($) to be used in a filename, this is not recommended because these characters have special meaning to the shell.
Be careful when using a plus sign (+) or a period (.) as the first character of a filename. They are allowed; just understand what they do before you use them.
Upper- and lowercase characters are treated differently in filenames.
No / (slashes) in the filename.
Extensions and prefixes are not significant in a UNIX filename.
In addition, Solaris uses a few filename extensions described in Table 1.3 of which you should take note.
Extension | Description |
---|---|
.c | A C programming language source file |
.o | An extension used to denote a file as containing object code |
.ps | A postscript file |
.Z | A file that has been compressed using the compress command (see Chapter 20, “Backup and Recovery”) |
.gz | A file that has been compressed using the gzip command (again, see Chapter 20) |
.html | A file with HTML code to be viewed using a browser such as Netscape |
.jpg, .gif, .tif | Graphics files such as pictures |
The easiest way to create a file is using the touch command. Using the following syntax:
touch <filename>
If a file by this name does not already exist, the system creates an empty file with the name specified. If the filename already exists, touch updates the access time of the file.
Example:
Use the following to create a file named file1:
touch file1 <cr>
Solaris has many commands that can be used to examine and view the contents of a file. I’ll outline some of the commands you’ll use to examine files in the next few sections. In addition, I’ll describe other commands that can be used to manage and modify files.
The file utility prints the file type. It performs a series of tests on each file in an attempt to classify it. If the file is not a regular file, its file type is identified. The command will identify the file type as a directory, FIFO, block special, and character special file. If the file is a regular file and the file is zero-length, it is identified as an empty file.
If the file appears to be a text file, file examines the first 512 bytes and tries to determine its programming language. If the file is a symbolic link, by default the link is followed, and file tests the file to which the symbolic link refers.
If a file does not exist, cannot be read, or its file status could not be determined, the output will indicate that the file was processed but that its type could not be determined.
The syntax is as follows:
file pathname
Example:
I can use the following to display information about a file named .profile in my home directory:
file ~/.profile <cr>
The following information is displayed:
/export/home/bill/.profile: ascii text
The cat command is used to display the contents of an ASCII text file. The syntax is as follows:
cat filename
filename can be a single file or a list of filenames separated by spaces.
Example:
Use the following to specify a list of filenames to be displayed:
cat file file2 file3
The system displays the contents of all three files. The information scrolls by on the screen and does not pause at each page.
contents of file1 contents of file2 contents of file3
Use the more utility to browse through a text file, one page or one line at a time. The more utility will pause after each page and display the percentage of characters displayed so far at the bottom of the page.
You also can display a help screen while running more. While still in the more utility, if you type h, a help screen will appear with many more commands that you can use to browse through the file. Some of these commands are described in Table 1.4.
The head command prints out the top few lines of a file. The default is to print out the top 10 lines. The syntax used to print out the first 10 lines of a file is as follows:
head filename
For example, I’ll print out the first 10 lines of the /etc/default/login file:
head /etc/default/login
The system responds with this:
#ident "@(#)login.dfl 1.11 00/10/19 SMI" # # Copyright (c) 1989-2000 by Sun Microsystems, Inc. # All rights reserved. # Set the TZ environment variable of the shell. # #TIMEZONE=EST5EDT # ULIMIT sets the file size limit for the login. Units are disk blocks.
You can also specify how many lines to view as follows:
head –5 /etc/default/login
The system responds by displaying the first five lines of the file, as follows:
#ident "@(#)login.dfl 1.11 00/10/19 SMI" # # Copyright (c) 1989-2000 by Sun Microsystems, Inc. # All rights reserved. # Set the TZ environment variable of the shell.
The tail command prints out the last few lines of a file. The default is to print out the last 10 lines. The syntax used to print out the last 10 lines of a file is as follows:
tail filename
For example, I’ll print out the last 10 lines of the /etc/default/login file:
tail /etc/default/login
The system responds with this:
# allowed before login exits. # #RETRIES=5 # # The SYSLOG_FAILED_LOGINS variable is used to determine how many failed # login attempts will be allowed by the system before a failed login # message is logged, using the syslog(3) LOG_NOTICE facility. For example, # if the variable is set to 0, login will log -all- failed login attempts. # #SYSLOG_FAILED_LOGINS=5
As with the head command, you can also specify how many lines to display.
The wc command reads a file and displays the number of newline characters, words, and bytes contained in the file. The wc command also writes a total count for all files, if more than one filename is specified.
The syntax for the wc command is as follows:
wc [-options] filename
The following are options to the wc command:
Option | Description |
---|---|
c | Counts bytes |
M | Counts characters |
l | Counts lines |
w | Counts words delimited by whitespace characters or newline characters |
If no option is specified, the default is -lwc (count lines, words, and bytes.)
Example 1:
Here’s wc with no options:
wc /etc/default/login
The system displays the following:
74 336 2014 /etc/default/login
The file contains 74 lines, 336 words, and 2,014 bytes.
Example 2:
Use the following to only display the number of lines in a file:
wc –l /etc/default/login
The system displays the following:
74 /etc/default/login
The cp command enables you to produce an exact copy of a file in your working directory or in another location on the system. The original file remains untouched.
Whenever you modify a file, it’s always a good practice to create a backup copy before modifying the original file. If you have problems editing or manage to corrupt the contents of the file, you have the duplicate copy to fall back on. The syntax for the cp command is as follows:
cp [–option] source destination
Where:
source is the name of the original file or directory that you want to copy.
destination is the name of the new file or directory that you want to create.
Caution
Be careful with the destination that you choose. If the destination of your file is the same name as an existing file, you will overwrite the existing file without any notification unless you use the –i option with the cp command.
The following are some commonly used options to the cp command:
Note
ACLs are an advanced level of permission that can be assigned to a file or directory. See Chapter 16 for a complete discussion on ACLs.
Example 1:
Use the following to copy a file:
cp file1 file1.org
The system makes a copy of file1 and calls it file1.org. When you list the contents of the working directory, the system displays the two files:
ls <cr> file1 file1.org
In this example, if file1.org already existed, the existing file would get overwritten when I issued the cp file1 file1.org command. By using the –i option, if the file named file1.org already exists, the system would display the following message:
cp: overwrite file1.org (yes/no)?
Type y to overwrite the original. If you type n, the command will be aborted.
Example 2:
Use the following to copy a list of files to a destination directory:
cp ~/* /tmp
This command copies all files in my home directory to the /tmp directory. The * (asterisk) is a shell metacharacter. We haven’t discussed shell metacharacters yet; they are described in the next section.
Example 3:
Here’s how to copy an entire directory (with subdirectories and files) to a new location:
cp -r ~/src ~/bkup
This example copies all files located in the src subdirectory, which is located in my home directory, to a directory named bkup, also located in my home directory. When copying directories, you must use the –r option.
The mv command enables you to move a file or directory to a new location. In Solaris, we use the mv command to rename a file. You might want to move individual files around as you strive to keep directory contents organized.
The syntax for the mv command is as follows:
mv [-i] source destination
Where:
source can be a file, multiple files, or a directory. There is no recursive option with the mv command because mv by itself is recursive.
destination can be your current directory or can be a path to another directory. The mv command will not move a file or directory onto itself. If the source and destination are within the same directory, you must use a new name. If the source and destination are in different directories, you can use the same name.
As with the cp command, use the –i option to prompt for confirmation whenever the move would overwrite an existing file.
Example 1:
Here’s now to move a file from the current directory to another directory:
mv –i file1 /export/home/user1/file1
file1 is moved into the directory named /export/home/user1/file1. To move a directory, see the section “mv: Move a Directory”later in this chapter.
Use the rm command to permanently remove a file or directory. After a file has been removed, it cannot be retrieved unless it has been backed up to another location. The syntax for the rm command is as follows:
rm [-option] filename
The following are options that can be used with the rm command:
f |
When the user
has write privileges to a directory, that user can remove all files in that directory, even if the files themselves are write-protected. Without the f option, the user will get the following message, asking if it is okay to remove the file:
rm: file1: override protection 644 (yes/no)? The f option removes all files, including write–protected files, in a directory without prompting the user. |
i | Interactive. You’ll be prompted for confirmation before the file is removed. The default is no confirmation. |
r | Recursively removes directories and subdirectories that are specified. The directory will be emptied of files and removed. If the file is a symbolic link, the link will be removed but not the original file. It’s a good practice to use the -i option with this command. For more information on links, see Chapter 4, “Advanced File Management.” |
Example 1:
Use the following to remove a file named test in the current working directory:
rm test <cr>
No message or output is displayed.
Example 2:
Here’s now to remove all files and directories from the current working directory. Ask for confirmation for each file before removing it, however.
rm –ri * <cr>
The system responds as follows:
rm: remove file1 (yes/no)?
Reply with a y or n for each file and directory.
You can only remove files that you either own or have the level of permission that allows you to remove the file. You cannot remove write-protected files. See Chapter 16 for more information on permission modes.
For information on removing directories, see the section titled “rmdir: Removing Directories” later in this chapter.
There are a number of characters that take on a special meaning in the Solaris shells. These special characters are called metacharacters (or sometimes they’re called wildcards). They are used with commands that perform file operations, such as cat, ls, cp, mv, and other file commands.
When used with a command, the metacharacters take on a different meaning than their obvious appearance. For example, a ? in Solaris is a special character that means literally “match any single filename character.” When one of these special characters appears in an argument on the command line, the shell expands that argument into a list of filenames and passes the list on to the program that the command line calls. The process that the shell performs on these filenames is called pathname expansion or globbing.
Metacharacters act as abbreviations in commands and enable you to refer to ranges of characters in a shorthand manner, saving you the effort of typing the names individually. This section describes the four shell metacharacters that relate to the file commands I’ve described. I’ll describe additional metacharacters throughout this book.
The * (asterisk) will match any number of characters (or no characters at all) in a filename. The * is not limited to a single character. It’s easier to illustrate this metacharacter with a few examples.
Example 1:
Use the following to list any file in your current working directory:
ls * <cr>
The system responds with a list of all files in the current working directory:
100FILE 1FILE FILE10 file1 file100 10FILE FILE1 FILE100 file10 FILE40
Example 2:
Use the following to list any filename in your current working directory that begins with any number of characters followed by fi (lowercase), contains additional characters, and ends with 1:
ls *fi*1 <cr>
The system responds with the following:
file1
Example 3:
Use the following to list any file that begins with FILE (uppercase) and ends with any number of characters:
ls FILE* <cr>
The system responds with the following:
FILE1 FILE10 FILE100
The ? (question mark) will match on any single character in a filename. The position of the question mark in the description is significant.
Example 1:
Use the following to list all files that begin with FILE (uppercase) and end with any two characters:
ls FILE?? <cr>
The system responds with the following:
FILE10
Example 2:
Use the following to show the contents of all files with names that are five characters long:
ls ????? <cr>
The system responds with the following:
1FILE FILE1 file1
The set [ ] means to match any single character that is found within the brackets. The position of the set in the description is significant.
Example 1:
Use the following to list files that begin with FILE (uppercase) and end with a 1 or a 0:
ls FILE[10] <cr>
The system responds with the following:
FILE1
Example 2:
Use the following to list all six character filenames that end with 10, 20, 30, or 40:
ls ????[1-4]0 <cr>
The system responds with the following:
FILE10 FILE40 file10
The range will match any single character that is within the range specified in the brackets. The range can consist of letters or numbers. The position of the range in the filename is significant.
Suppose we have a directory with the following files:
$ ls FILE1 FILE100 aFILE1 dFILE3 file10 FILE10 FILE40 bFILE2 file1 file100
Example 1:
Use the following to list any file that begins with the letters a, b, or c (lowercase) and ends with a 1, 2, or 3:
ls [a-c]*[1-3] <cr>
The system responds with the following:
aFILE1 bFILE2
Example 2:
Use the following to list all filenames that begin with A through Z or a through z (upper- or lowercase) and end with any number of characters:
ls [A-z]*
The system responds with the following:
FILE1 FILE100 aFILE1 dFILE3 file10 FILE10 FILE40 bFILE2 file1 file100
Before you can start creating files, you must be in a directory that has permissions that allow you to create files. Chapter 16 describes directory permissions in detail. For now, your home directory is the best place to start.
The home directory is set up for you to store your personal files and gives you authority to create and remove files. You’ll notice that the system administrator has put a few files in your home directory already; many of these are startup files, which are described in Chapter 13, “Setting Up User Accounts.” These startup files provide shell-specific information about you, the user. They also contain shell variables, which can be set to control your environment. For more information on these variables, see Chapter 3.
You’ll use your home directory to store personal files, and you have complete control over this directory. Until you become more experienced with the Solaris operating environment, I recommend that you use this directory to experiment with the commands and concepts I’ll be describing in this section. As long as you work within your home directory, you will be less likely to inflict damage to the Solaris operating system while you’re learning.
See the cd command described in the next section for information on how to navigate a directory tree and how to find your home directory.
Various methods are used to get to your home directory. When you initially log in, you will automatically be put into your home directory. Usually, your home directory is set up as a subdirectory in /export/home. My home directory, for example, is /export/home/wcalkins. Use the cd command to change directories. The syntax for the cd command is as follows:
cd <pathname> <cr>
Use the cd command to traverse the directory tree. There are several ways to specify the pathname to which you want to change. For example, to get to my home directory, I could use the cd command as follows:
cd /export/home/wcalkins <cr>
You can use this method to move to other directories also. Notice that I specified the full pathname to the directory. The full pathname begins with a leading slash, which signifies the root directory. The root directory is the top-level directory, as shown in Figure 1.5. This figure outlines part of the default directory structure within the Solaris environment.
The full pathname of a directory or a file begins with a slash (/) and describes the entire directory structure between that file (or directory) and the root directory. However, you can often use a shorter name that defines the file or directory relative to the current working directory.
When you are in the parent directory—in this case, /export/home—you can move to a subdirectory by using only the directory name and not the full pathname, as follows:
cd wcalkins/dir1 <cr>
In this example, the cd command uses the relative pathname of the directory wcalkins/dir1. In other words, from my current working directory (/export/ home), I will traverse down into a subdirectory called wcalkins/dir1. The pathname I specified does not begin with a leading slash, so it will look for the specified path “relative” to my current working directory. To specify the full pathname of this directory, use the pathname /export/home/wcalkins/dir1.
When a <pathname> is not specified, the default is to use the current user’s home directory as the pathname. Therefore, I also could type this command to change to my home directory:
cd <cr>
In addition, the ~ (tilde) or the shell variable $HOME can be used as a shortcut to specify your home directory, as follows:
cd ~ <cr> or cd $HOME <cr>
Both commands will take me to my home directory, regardless of which directory I am currently in. You can also use this shortcut to specify another user’s home directory. For example:
cd ~username <cr>
username is another user’s login name. This example would change your working directory to that user’s home directory.
As I stated earlier, the directory immediately “above” a subdirectory is called the parent directory. In the next example, /export/home is the parent directory of /export/home/wcalkins. The symbol .. (dot-dot) represents the parent directory. The command cd .. changes the working directory to the parent directory, as in this example:
$ pwd /export/home/wcalkins $ cd .. $ pwd /export/home
If your current working directory is /export/home/wcalkins and you want to work with some files in /export/home/sburge, type the following command:
$ pwd /export/home/wcalkins $ cd ../sburge $ pwd /export/home/sburge
../sburge tells the system to look in the parent directory for sburge. As you can see, this command is much easier than typing the entire pathname /export/home/sburge.
The pwd command (print working directory) displays where you are in the file system hierarchy. In other words, it displays your working directory. For example:
pwd
The system responds with the following:
/export/home/wcalkins
Your working directory is the directory in which you are currently working.
As I stated earlier, you’ll use directories like folders to organize and store your files. When creating directories, use names that are indicative and describe the contents.
A directory name can be any legal UNIX filename. (See the next section for a list of UNIX file-naming rules.) Use the mkdir command to create a directory. The syntax is as follows:
mkdir <option> <directoryname>
Options to the mkdir command are as follows:
m <mode> | This option allows users to specify the permission mode to be used for the new directory. Permission modes are described in detail in Chapter 16. |
p | This enables you to create a directory and additional subdirectories in the single command. |
Example 1:
Here’s how to create a directory named dir1 with a permission mode of 774:
mkdir –m 774 dir1
Example 2:
To create a directory named dir2 with a subdirectory named subdir2, I would issue the following command:
mkdir –p dir2/subdir2
Directories can be renamed, moved, and deleted. To perform these tasks, you first need to make sure you are the owner. Chapter 16 describes file and directory ownership in detail.
Just as I described using mv to move files, you can also move a directory using the mv command. The syntax and command options are the same as described for the mv command earlier in this chapter.
If you are moving a directory and the new directory does not exist, it will be created. If an existing directory is the destination, the source will be placed into the existing directory as a subdirectory with its original name.
In the following example, I have a single directory named user1 (/export/home is the parent directory):
pwd <cr>
The system responds with the following:
/export/home
I’ll list the contents of the /export/home directory using the ls command, as follows:
ls <cr>
The system responds with the following:
user1
I’ll use the mv command to rename the directory from user1 to user2:
mv user1 user2 <cr> ls <cr>
The system responds with the following:
user2
You can also use the mv command to move a directory to a location within another directory. Starting in the /export/home/user2 directory, I see the subdirectory named dir1. I’ll first verify that I’m in the correct directory:
pwd <cr>
The system responds with the following:
/export/home/user2
I list the contents of the directory:
ls <cr>
The system responds as follows:
dir1
Now I’ll use the mv command to move the dir1 directory to /export/home/ user3/dir1 as follows:
mv dir1 ../user3 <cr>
Listing out the contents of /export/home/user3 shows that the directory has been moved:
ls ../user3
The system responds with the following:
dir1
To remove an empty directory, use the rmdir command as follows: rmdir directoryname
Example 1:
Use the following to remove a directory named user1:
rmdir user1 <cr>
If the directory is not empty, the directory will not be removed, and you’ll receive the following message:
rmdir: directory "user1": Directory not empty
Example 2:
Use the following to remove a directory and all its contents, including any existing subdirectories and files:
rm -r user1
The rm command and its options were covered earlier in this chapter. Refer to the section “rm: Remove a File.”
Caution
Directories and files that are removed with the rmdir or the rm -r command cannot be recovered.