1. Trang chủ
  2. » Công Nghệ Thông Tin

Unix for mac your visual blueprint to maximizing the foundation of mac osx phần 2 potx

36 307 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 36
Dung lượng 2,76 MB

Nội dung

DELETE A FILE ⁄ Type rm and a space. ¤ Type the name of the file and press Return. ■ The shell deletes the file. DELETE A FILE SAFELY ⁄ Type rm -i and a space. ¤ Type the name of the file and press Return. ■ The shell asks you if you want to delete the file. ‹ Type your response and press Return. ■ The rm command either deletes the file or abandons the operation, depending on your response. 23 Y ou can use the rm command to delete files you no longer want to keep. When you delete a file using the Mac OS X Finder in Aqua, the Finder does not immediately delete the file, but stores it in the Trash. However, when you delete a file using the rm command, the file completely and instantly disappears. This means that if you mistakenly delete a file from the shell, you cannot recover it by simply dragging it back out of the Trash. As with the cp and mv commands, deleting files can be dangerous because people can make mistakes. You can use the -i option to tell the rm command to confirm that you want to delete a file; if you do, you can type y for yes, and press Return. If you do not, you can press any other key or simply the Return key. By default, the rm command only removes ordinary files, although you can remove an empty directory with the rmdir command; for a directory containing files, you can use the rm –r command. You cannot remove directories unless they belong to you or you have write permission to the files. However, there is no special access permission required to delete a file. It is possible to delete a file whose name begins with a dash, such as -foo or -bar. These are extremely hard to remove, because the rm command tries to interpret the filenames as an option, because options begin with dashes. In this situation, you can use the option (a double dash). This means that the command does not check the rest of the line for more options, making this your last option flag. DELETE A FILE WORK WITH FILES 2 DELETE A FILE 02 53730X Ch02.qxd 3/25/03 8:56 AM Page 23 REMOVE READ ACCESS ⁄ Type chmod and a space. ¤ Type og-r and a space. ‹ Type the name of a file and press Return. ■ The shell indicates that only you can read the file. ADD WRITE ACCESS ⁄ Type chmod g+w and a space. ¤ Type the name of a file and press Return. ■ The shell indicates that anyone in the file group can change this file. Y ou can change the permissions on files you own, allowing you to grant or withhold permission to others to read, write, or execute your files. There are two parameters for the chmod command, the first being the new mode, and the second being the file you want to change. You specify the mode change by indicating who the change affects, whether the change should be to add, remove, or set permissions, and what kind of permissions you want to set. Unix uses letters to designate the users that your mode change affects: u, for the owner of the file, or user, g,for users in the file group, o, for others — users not in the group — or a, for all users. You can designate more than one set of users by combining these designators together, such as go, to indicate users in the file group and users not in the file group, while omitting the file owner. You can mark permission changes with a plus symbol (+) to add permissions, a minus symbol (-) to remove them, or an equal symbol (=) to set them. The possible permissions are the same as those shown by the ls -l command: r for read access, w for write access, and x for execute access. You can make more than one change at a time to a given file by listing them with commas as separators. For example, to set the file example to be readable, writeable, and executable by you, readable and executable by those in the file group, and readable by those outside of the group, you can type chmod u=rwx,g=rx,o=r example. CHANGE FILE PERMISSIONS UNIX FOR MAC 24 CHANGE FILE PERMISSIONS 02 53730X Ch02.qxd 3/25/03 8:56 AM Page 24 SET EXECUTION PERMISSIONS ⁄ Type chmod a-x,u+x and a space. ¤ Type the name of a file and press Return. ■ The shell indicates that only you can execute this file. SET CUSTOM PERMISSIONS ⁄ Type chmod a=r and a space. ¤ Type the name of a file and press Return. ■ The shell indicates the new permissions: the file is now read-only by everyone. Note: You cannot alter this file without changing the permissions back. WORK WITH FILES 2 You can change the permissions for an entire directory and its contents, including your Home directory, by using the -R option with the chmod command. The -R option dictates that modes should be changed recursively, which means that the changes apply to all files in that directory or in its subdirectories. For example, if you want to make sure your Home directory is readable only by you and not by other users of your computer, you use the chmod –R command. 25 TYPE THIS: [ferro:~] user% chmod -R go-rx ~ [ferro:~] user% ls -l RESULTS: drwx 8 user staff 272 Nov 13 13:57 Desktop drwx 12 user staff 408 Nov 13 02:00 Documents drwx 22 user staff 748 Nov 13 03:40 Library drwx 3 user staff 102 Nov 4 13:49 Movies drwx 3 user staff 102 Nov 4 13:49 Music drwx 3 user staff 102 Nov 4 13:49 Pictures drwx 4 user staff 136 Nov 4 13:49 Public drwx 5 user staff 170 Nov 4 13:49 Sites 02 53730X Ch02.qxd 3/25/03 8:56 AM Page 25 MATCH A GLOB PATTERN ⁄ Type a command that takes a filename as an argument. ¤ Type a wildcard pattern that matches the files you want to select. ■ The shell executes your command on the files specified by the wildcard pattern. USE CARETS TO SELECT UNMATCHED FILES ⁄ Type a command that takes a filename as an argument. ¤ Type ^ followed by the wildcard pattern matching the files you do not want to select. ■ The shell executes your command on the files in the current directory, except for those specified by the wildcard pattern. 26 SELECT FILES USING WILDCARDS Y ou can use wildcard characters to avoid having to type out the full name of each file you want to work with in Unix. This not only saves typing but also lets you refer to multiple files with similar names in a single command. Unix shell wildcards are also called glob-patterns, and the use of these wildcards is known as globbing in Unix jargon. You can use these wildcards whenever you give a filename argument to a shell command. The asterisk wildcard (*) matches any sequence of zero or more characters in a filename. The question mark (?) matches any single character. A range of characters in square brackets, such as [a-m], matches any single character within that range. Keep in mind that letters in filenames are case-sensitive, so while the letter d is a match for [a-m], the letter D is not, because it does not come between lowercase a and m. A caret (^) at the start of a pattern or a range of characters indicates that the pattern should select those characters that do not match the given pattern. For example, if you want to use the ls -l command on all directories and files that begin with the letter P, you can type ls -l P*.To remove all files beginning with A, followed by three letters, and ending in .doc, from your Documents directory, you can type rm -i Documents/A???.doc. You should always use the -i option with the rm command if you give it wildcard arguments, to avoid deleting the wrong files. A careless rm * command in the wrong directory can remove the contents of the directory. Hidden files are resistant to being identified through wildcards. The command ls *, for example, does not list them while ls .* lists only hidden files. SELECT FILES USING WILDCARDS UNIX FOR MAC 02 53730X Ch02.qxd 3/25/03 8:56 AM Page 26 ⁄ Type set autolist. ¤ Type a shell command that takes a filename as an argument. ‹ Type the first few letters of the filename and press the Tab key. ■ A list of possible matches appears. › Type enough letters to make an unambiguous match. ˇ Press the Tab key again. ■ The shell completes the filename. Á Press Return. ■ The shell executes the command. 27 Y ou can easily specify filename arguments by using command completion, which is a special function built into the shell. To use filename completion, you must type out the first part of the filename and then press the Tab key. If there is a file that matches the incomplete filename, the shell fills in the full filename for you. If no file matches the filename, your shell program emits a beep, indicating that there is not a match. If more than one file matches, the shell program fills in as much as it can and then emits a beep, while waiting for you to enter more text to differentiate between the files that match the pattern up to that point and then differ. At this point, you can either type out the rest of the command line and press Return, or type enough of the command line to uniquely identify the file you want to access, and then press Tab again. To see the results of an ambiguous match, you can press Control + D after the beep. Alternatively, you can type set autolist, which instructs your shell to list the remaining choice whenever file completion fails. This is a good command to save in your .tcshrc file, which ensures that this feature is set every time you start a Terminal window. Similarly, you can turn off autolist with unset autolist. You can combine filename completion with full or relative pathnames as well; if you type a partial pathname and press Tab, the shell lists the options within that path. Using filename completion can save you a lot of typing as well as making it less likely that you mistype any characters. SELECT FILES USING COMPLETION WORK WITH FILES 2 SELECT FILES USING COMPLETION 02 53730X Ch02.qxd 3/25/03 8:56 AM Page 27 LOCATE FILES BY NAME ⁄ Type find and a space. ¤ Type the pathname of a directory and a space. ‹ Type -name and a space. › Type a filename. ˇ Type -print and press Return. ■ The shell executes the find command and prints the file location. Y ou can locate files by their names or other criteria by using the find command. This command tends to be faster than the Mac OS X Sherlock application for locating files, especially those that are used by the system or otherwise hidden from the Finder. The find command is a complex and very powerful utility that lets you run many different types of searches and perform a variety of actions on the files that it finds. However, the most common use for the find command is simply to locate files with a certain name and print out their locations. The basic syntax for the find command is: find pathname -name filename -print The first parameter, pathname, is the directory where you want to search. The filename is the name of the file for which you are looking. The argument -print tells the find command to display the location of the file. You can also give a wildcard pattern instead of a filename, and the find command lists all files that match that pattern. When you use wildcard patterns in this way you need to enclose them in quotation marks. For example, if you save a Word document but you do not keep track of what folder you save it in, you can use the find command to locate all the .doc files in your Documents directory: find ~/Documents -name "*.doc" -print This displays a list of all file locations matching the pattern. If you want to see a full file listing instead of the relative pathname for the files found, you can replace the print command with the ls command. FIND FILES BY NAME UNIX FOR MAC 28 FIND FILES BY NAME 02 53730X Ch02.qxd 3/25/03 8:56 AM Page 28 FIND FILES BY PATTERN ⁄ Type find and a space. ¤ Type the pathname of a directory and a space. ‹ Type -name and a space. › Type a wildcard pattern in quotes. ˇ Type -print and press Return. ■ The shell executes the find command and prints the file location for each file matching the wildcard pattern. WORK WITH FILES 2 In addition to searching by name or wildcard pattern, you can also search by other qualities of the file, such as the last time the file was changed, the owner of the file, or the permissions of the file. You control the search with the arguments you include after the pathname of the directory. 29 ARGUMENT SEARCHES FOR -group groupname Files belonging to a certain group. -iname filename Like -name, but matches regardless of case. -mmin minutes Files modified within a certain number of minutes. -mtime days Files modified within a certain number of days. -perm +filemode Files that have specific permissions set. -size charsc Files that are an exact size in characters. 02 53730X Ch02.qxd 3/25/03 8:56 AM Page 29 DETERMINE YOUR CURRENT WORKING DIRECTORY ⁄ Type pwd and press Return. ■ The shell shows your current location. CHANGE TO A DIFFERENT DIRECTORY ¤ Type cd and a space. ‹ Type a new directory name and press Return. ■ Your current directory changes to the new location. Y ou can change your current directory by using the cd command. Each shell window operates in a specific location within the file structure, and you can view this location by typing the pwd command. The pwd command is Unix shorthand for print (display) working directory. The current working directory is the directory from which relative pathnames are computed and the default directory in which files are found or saved. If you type cd alone, it returns you to your Home directory. If you provide the cd command with a pathname argument, either relative or full, you change to that directory. You can change your current directory to any directory that grants you execution access permissions. This allows you to explore nearly all of the directories on your hard drive, with the exception of a few system directories that are protected. When your current directory changes, your prompt changes as well, to reflect the new location. Your prompt displays the names of up to three directories above your current location, or fewer if you are close to the root directory. For example, if you are in the directory /usr/share/ tcsh/examples, on a computer named ferro, your prompt looks something like this: [ferro:share/tcsh/examples] user% You can use wildcards and filename completion with the cd command; it accepts special directory names such as cd , to go up one directory level, and cd ~user, to change to the Home directory of the user user. You can also use the cd - command to return to the last directory in which you were working. CHANGE THE CURRENT DIRECTORY UNIX FOR MAC 30 CHANGE THE CURRENT DIRECTORY 03 53730X Ch03.qxd 3/25/03 8:56 AM Page 30 RETURN TO THE PREVIOUS DIRECTORY › Repeat steps 2 and 3 to move to a new directory. ˇ Type cd - and press Return. ■ Your current directory changes back to the previous directory. RETURN TO YOUR HOME DIRECTORY Á Type cd and press Return. ■ Your current directory changes to your Home directory. WORK WITH DIRECTORIES 3 You can move through the file system by using the directory stack. The stack is a list of directories of which the shell keeps track. You can list the stack by typing dirs. To change to a new directory and add it to the directory stack, use the command pushd pathname. This command adds a new pathname to the stack, and prints the stack. You can use the directory stack by typing popd to remove the top directory from the stack and change to the next one down, or by typing pushd — with no arguments — to swap the top two stack entries and change to the new top directory. The directory stack is very useful if you are going to be switching between several directories in the same shell window. Example: [ferro:~/Documents] user% pushd /usr/lib /usr/lib ~/Documents [ferro:/usr/lib] kynn% pushd /etc /etc /usr/lib ~/Documents [ferro:/etc] kynn% popd /usr/lib ~/Documents [ferro:/usr/lib] kynn% 31 03 53730X Ch03.qxd 3/25/03 8:56 AM Page 31 CREATE A DIRECTORY ⁄ Type mkdir and a space. ¤ Type the name of a new directory and press Return. ■ The shell creates the new directory. CREATE SEVERAL DIRECTORIES AT ONCE ⁄ Type mkdir and a space. ¤ Type the names of several directories you want to create and press Return. ■ The shell creates the new directories. Y ou can use the mkdir command to create new directories. Directories are useful for organizing your files into a logical structure. When you create a directory in the Unix shell, you can access it through the Mac OS X Finder as a folder; conversely, when you create a folder in the Mac OS X Finder, you can access it as a directory in the Unix shell. The arguments that you give to the mkdir command affect how the command creates new directories. When you create a directory with a space in the name, you must enclose it in quotation marks, unless you want to create several directories at once. To create a directory, you need to have write permissions for the current directory or the directory in which you are creating the new directory. For example, if you want to create the directory /usr/local/resp, you must have write access to the /usr/local directory. You can either change your working directory to the /usr/local directory using the cd command and type mkdir resp, or you can type mkdir /usr/local/resp from any current directory. When you give a pathname, whether relative or full, the intervening directories must exist. If they do not, you receive an error message, and the directory is not created. You can tell the mkdir command to create any necessary directories by using the -p option. For example, to create the directory ~/Documents/by-date/2003/03, you can type mkdir -p ~/Documents/by-date/2003/03. This command creates the by-date and 2003 directories as well as the 03 directory if they do not already exist — presumably, the Documents directory already exists in your Home directory. CREATE A DIRECTORY UNIX FOR MAC 32 CREATE A DIRECTORY 03 53730X Ch03.qxd 3/25/03 8:56 AM Page 32 [...]... option When you use the ls -l command, you see a value for the size of the directory just as you do for other files However, this value does not represent the size of the contents of the directory; it represents the size of the directory entry itself, a list of the files stored in the directory To obtain the size of the contents of the directory, including subdirectories, you can use the du command If... Documents is a directory (not copied) To copy a directory, you must use the -R option, which tells the cp command to copy the directory and its contents to the new location For example, to create a copy of the Documents directory, you can type cp -R Documents "Backup of Docs" This will copy the entire directory at once, creating the new directory and duplicating all of the files The names of the original... files will be the same, although the directory names will be different COPY A DIRECTORY ⁄ Type cp -R and a space ¤ Type the name of the original directory and a space ‹ Type the name of the new › Type ls –F and a space copy of the directory and press Return followed by the name of the original directory, and press Return ■ The new directory is a duplicate of the original directory ˇ Repeat the ls –F command... step 4 to show the contents of the new directory 37 03 53730X Ch03.qxd 3 /25 /03 8:56 AM Page 38 UNIX FOR MAC DETERMINE DIRECTORY SIZE Y ou can use the du command to display the size and contents of a directory The name of the du command stands for disk usage, and it tells you how much space each file or directory uses on your hard drive This information is also available from the Finder using the Get... directory, and press Return ˇ Type cd to move to the parent directory ¤ Type ls -A and press Return the name of the directory you want to delete ‹ Type rm, a space, and the name of a hidden file Á Type rmdir, a space, and ■ The shell deletes the directory and its contents 35 03 53730X Ch03.qxd 3 /25 /03 8:56 AM Page 36 UNIX FOR MAC MOVE FILES INTO A DIRECTORY Y ou can move files into or out of a directory... ¤ Type the names of the files separated by spaces ‹ Type the name of the destination directory and press Return › Type ls –F and a space followed by the name of the directory, and press Return ■ The shell shows the files in the new directory 03 53730X Ch03.qxd 3 /25 /03 8:56 AM Page 37 WORK WITH DIRECTORIES 3 COPY A DIRECTORY Y ou can use the cp command to make a copy of a directory by using the -R... Type the name of a file and press Return 42 ■ The less command displays the first page of the content of the file ‹ Press the Spacebar ■ The shell displays the next page of text 04 53730X Ch04.qxd 3 /25 /03 8:56 AM Page 43 WORK WITH TEXT 4 In addition to pressing the Spacebar to page through a file using the less command, or the man command, you can also use other keystrokes to move through the file For. .. cd, a space, and the name of the non-empty directory › Type y and press Return for each item that you want to delete from the directory ¤ Press Return ‹ Type ls –F and press DELETE A NON-EMPTY DIRECTORY ˇ Type ls –F and press Return to confirm that the directory is empty Note: You may want to use the ls command to view the contents of the directory before you delete it 34 ■ The directory is empty 03... command by piping the output to the column command For example, if you want to make columns from the output of the grep command, you can type the following: grep 'pattern' filename | column The maximum width of the lines in the input file determines the width of each column If, for example, your Terminal window is 80 characters across and the maximum width of all lines within the file is 21 , then you can... non-directory where you cannot even use the ls command to list the directory contents You can use the cd command, with no arguments, to return to your Home directory In such a case, you can go into the directory and remove any existing files using the rm command, or any DELETE A DIRECTORY DELETE A DIRECTORY ¤ Type the name of an ⁄ Type rmdir and a space empty directory and press Return Return to see the . contents of the directory; it represents the size of the directory entry itself, a list of the files stored in the directory. To obtain the size of the contents of the directory, including subdirectories,. popd to remove the top directory from the stack and change to the next one down, or by typing pushd — with no arguments — to swap the top two stack entries and change to the new top directory. The. INTO A DIRECTORY UNIX FOR MAC 03 53730X Ch03.qxd 3 /25 /03 8:56 AM Page 36 ⁄ Type cp -R and a space. ¤ Type the name of the original directory and a space. ‹ Type the name of the new copy of the

Ngày đăng: 09/08/2014, 16:20

w