Viewing and Editing Files


Unix’s origin as a command-line environment brings with it a number of tools for dealing with files. You have tools to display file contents and many kinds of editors, from ones that execute commands a line at a time, through screen editors, and on to stream editors that process commands against entire files. To use these editors, you type (or store) your commands rather than use a mouse and menus; all editors covered here are keyboard driven.

Standard input, standard output, and pipes

To the shell (and to Unix in general), any place it obtains or places data is a file. The keyboard data stream is a file, the window in which output is displayed is a file, and files on your disk are files. The input stream from your keyboard is the default for a special file called standard input (stdin). The default file for output, your Terminal window, is called standard output. You can redirect standard input to come from some other file by preceding the file name with a less than sign (<). Similarly, you can redirect standard output by using a greater than sign (>). If, for example, you wanted to create a file containing a directory listing of your Documents folder, you could type ls Documents >docdir.txt, as shown in Figure 24-7. A number of commands display the contents of files. Among these commands are cat, more, pr, head, and tail. The meaning for each command is provided in Table 24-2.

click to expand
Figure 24-7: Output from the list directory (ls) command is redirected into a text file docdir.txt. The concatenate (cat) command is used to view the file.

Table 24-2: Commands that Display Contents of Files

Command

Meaning

Cat

Concatenate the files given as arguments and display on standard output.

More

Display the arguments on standard output a page (window full) at a time. You type a space to get to the next screen; type b to go back a screen. Typing a return advances one line, and typing q terminates the command.

Pr

Similar to more, but it includes page headers and footers at would-be page breaks if the output were directed to a printer.

Head n

Display the first n (10 if n is omitted) lines of a file on standard output.

Tail n

Display the last n (10 if n is omitted) lines of a file on standard output.

Tip

You can append output to an existing file by using two successive greater than signs. For example, ls Movies >>docdir.txt would append the directory listing for your Movies folder to the file docdir.txt.

You can chain commands together with the pipe symbol, a vertical bar (|), so that the output from the first command is the input to the next. For example, ls –R|more would display a recursive directory listing one screen at a time.

Most commands default to taking their input from standard input. For example, if you omit the file argument from the cat command, cat patiently waits for you to enter it from the keyboard. When you enter data from the keyboard, you need to indicate that you’re finished by entering the Unix end-of-input character (Control-D).

start sidebar
Paths and Variables

With few exceptions, such as cd, almost every command you enter at the Unix prompt is a program stored on your disk. The shell is a programming environment, complete with loops, conditionals, and variables. Shell programs are usually called scripts. Shell programs are interpreted rather than compiled. Therefore, when a shell script or command executes, the shell parses the command, evaluates the variables, and then executes rather than having the instructions reduced to binary machine instructions.

You create shell variables by using the set command. For example, set myPath = "~/MyApps" would create a variable named myPath that had the value /Users/craigz/MyApps (assuming that your username is craigz). You can determine the current value of any shell variable by issuing the echo command, with the variable name, preceded by a dollar sign ($), as an argument. One very important shell variable is PATH. PATH is the variable the shell evaluates to determine where it should look for commands you issue. The following figure shows the initial value of PATH for the user craigz.

click to expand

As you can determine, PATH is a series of directory specifications that are separated by colons. Every time you issue a command, such as ls or cp, the shell starts checking each directory in PATH until it finds the command and executes it. If the command cannot be found in any directory in PATH, you receive an error message to that effect. If the program is not in one of the PATH directories, you need to specify a directory path to it. To execute a command in your current directory, you precede it with ./. If you are curious as to which directory holds the command you want to execute, you can use the which command (a built-in shell command). The which command can come in handy if, for example, you are attempting to execute a newly installed command, and the wrong command executes because a command with the same name was located in an earlier PATH directory.

end sidebar

About permissions

In addition to the read and write permissions, you find an execute permission at the file level. Because Unix commands aren’t applications with an APPL file type (or a bundle with an .app extension), the shell needs some way to indicate that a particular file is executable, and Unix has used this method of file-based permissions for over three decades now.

Permissions on directories have a slightly different but analogous meaning compared to files as shown in Table 24-3.

Table 24-3: Unix Permissions and Meanings

Permission

Meaning for a file

Meaning for a directory

R read

read a file

list files in ...

w write

write a file

create file in ...

rename file in ...

delete file ...

x execute

execute a shell script,

read a file in ...

command, or program

write to a file in ...

execute a file in ...

execute a shell script in ...

Commands, programs, and shell scripts are examples of files for which execute permission should be enabled. As with any other file, this file has three levels of permission: owner, group, and everybody.

Note

Unix users typically refer to permissions as three (octal) digit numbers. Read permission is worth four points, write permission is worth two, and execute permission is worth one. Therefore, when you hear that a file has 740 permission, the first digit being 7 means that the owner can read, write, and execute the file (4+2+1=7); the second digit being 4 means members of the group can read the file (4+0+0=4); and the third digit being 0 means everyone else has no permissions with respect to the file (0-0+0=0).

Changing permissions

The chmod command enables the owner or root to change the permissions on a file. The simplest form of this command is to follow chmod with the new permissions and then the file or list of files to receive those permissions. For example, chmod 777 myscript.sh would give read, write, and execute permission to everyone for the file myscript.sh.

If you don’t want to do the math, you only need to remember six letters — u for user, g for group, a for all, r for read, w for write, and x for execute. You also need to remember three symbols as well: + to add a permission, - to subtract a permission, and = to set permissions. For example, chmod g+w dirdoc.txt would add write permission for members of the group to the file dirdoc.txt without affecting any other permissions.

Changing owner and groups

Only the root user (system administrator) has the authority to change a file’s owner. Assuming that you have logged in as root or are running from an administrator account and using the sudo command, the syntax to change a file’s owner is chown newownerid filelist. The newownerid is either the new owner’s login name or her numeric ID, as displayed in NetInfo Manager, and filelist is the file or files whose ownership is to be changed.

If you are a member of the group to which you want to change group ownership and you have write and execute permissions to the files and directories in question, you can change the group ownership of a file with the chgrp command, whose form is the same as chown command except that you use a group name or number rather than a user name or number.

Tip

To determine to which groups you belong, you can enter the groups command. You can also determine to which groups another user belongs by typing groups with their username as an argument.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net