Recently learning Apple's certified MAC OS X support Essentials tutorial, see Command Line section has a lot of practical knowledge, below Select a part of translation + notes, organized into this article.
You can drive cars all day without knowing how to fix them, but if you want to be a maintainer, you need to know how things work. The same thing happens on Mac OS X; You can always use your Mac without knowing how to fix it, but if you want to do some maintenance or solve some problems with the system, you need to know how to use command-line.
Why use the command line/How do I open the command line?
- Many features are not available in the graphical interface and are implemented only through the command line.
- The Finder hides many files that you don't really need, but command line will allow you to access all the files.
- You can access your MAC remotely via command line (using SSH).
- Administrators users cansudoget root user rights through commands.
- By command-line script, you can make your work more efficient.
- The Terminal (terminal) program can be found in the "utilities".
- If you open the user name login mode manually, enter the user name at login to enter the>consolecommand line interface directly. Then you still need to log in to an account.
First command line
About the man command
Although there are thousands of commands, each command has many optional parameters and specific usage, but you do not need to remember these commands. You just have to remember one:man
Most commands will contain a usage guide that will tell you any details you need to know about the command, which you can enter on the command lineman command-name. For example, if you want to knowlshow to use this command, you can enter itman lsinto the Usage Guide page.
The usage guide is often very long, so you can use the ▲ (up arrow) or (down arrow) to move up and down, use to page pages, input/and keywords to follow the keyword search, press Q to exit the Usage Guide page.
So--what if you don't even know the name of the command? Inputman -kand keywords to search the entire usage guide database.
command line, file and path
If you know how to use commands as the first step in mastering command line, the second step is to learn how to use the file path in command line. If you master the file path, you will find that it is quicker than using the Finder.
Attention
- The command line tool is case-sensitive and must include an extension for the file name. For example, if you want to find itunes for this program, the inputitunesis invalid and must be enterediTunes.app.
- Mac OS traditionally prefers to use the name "folder" (folders), but in command line, the word "directory" is used primarily. This is consistent with UNIX.
Two paths: absolute path and relative path
- Absolute path: A full description of the location of a file, always beginning with a slash (/) (forward slash). For example/Users/michelle/Public/Drop Box.
- Relative path: Describes only a subset of location information, which is related to your current directory in command line. When you open the new Terminal program, the command line Session directory should be your home folder. The relative path of the above example folder is then writtenPublic/Drop Box. Obviously it starts with the current directory. Like HTML, you can also use two dots ("..") to represent the parent directory, so you can represent the ancestor or sibling directory with a relative path. For example you can entercd ..evencd ../..
Switch to a different path and directory
If you want to switch your current command line session to a different directory, you need to use three commands:pwd,lsandcd.
- pwdThe meaning is "Print working directory", which displays the absolute path of the current directory.
- lsThe meaning is "list directory Contents", which lists the contents of the current directory. This command also has additional parameters to choose from.
- cdThe meaning is "change directory", which changes the current directory to the directory you specify. If you do not specify it, you will be returned to your home folder.
Handling special Characters
If the directory has special characters (spaces, parentheses, quotes,,,,,,,,,,,,,,,[]!$&*;|\), then entering spaces directly can cause system recognition difficulties, and special syntax must be used to represent these characters. For example, in the example above, add a backslash "\" (back slash) before the space:cd Punlic/Drop\ Box/. In addition to backslashes, you can also use the quotation marks method:cd "Public/Drop Box"。
-If you don't want to enter it manually, you can also drag the file from the Finder to the Terminal window to create an absolute path, which is easier because all of the special characters mentioned above will automatically become a system-aware representation when dragged. In fact, a more efficient solution is to use the Tab complete feature.
Tab Complete is one of the most time-saving features in command line, and it also prevents you from entering errors by using its AutoComplete file and directory name feature. Use thecdcommand to enter your home folder,cd Pand then press the TAB key. You may hear an error tone because you have multiple folders in your home folder that start with P. Press once again tab,terminal will list two folders for you starting with P: public and Pictures. Press U, and then press tab,terminal to automatically complete the fillPublic/. Tab Complete also handles those special characters. Note that this retains the symbol at the end,/most of the time it's fine, but if something goes wrong, remove the extra try/.
In addition, the~tilde can represent the current user's home folder in command line. For example,~/Public/Drop\ Box/it is legal.
View hidden files
To simplify your work, command line and the Finder will hide many files and folders, which are usually required by the system. It is difficult to make the Finder display hidden files without using third-party tools, but it is very simple in command line. First, many hidden files are hidden by hidden properties in the Finder, and command line ignores them, so the files are displayed in command line. In addition, thelscommand hides.the files that begin with the file name, but these files can be displayed by using the-aoptions. For example:
michelle$ ls -la
We have also added an-loption to control the output format. If you pay attention to the output, you will find also include.and..two items, which represent the current folder and the parent folder (). If you don't want to show these two items, just-achange them-A.
Go to another volume
In command line, the system volume (also known as root volume) is represented by a forward slash at the beginning. It may sound incredible, however, that other volumes in command line appear to be in a folder called Volumes in the file system. The following command clearly shows this logical relationship: I started off my home folder and eventually went to a volume called Time Machine, which was external to the MAC.
bogon:~ renfei$ pwd/Users/renfeibogon:~ renfei$ cd /Volumes/bogon:Volumes renfei$ pwd/Volumesbogon:Volumes renfei$ lsMacintosh SSD Time Machinebogon:Volumes renfei$ cd Time\ Machine/bogon:Time Machine renfei$ pwd/Volumes/Time Machine
Viewing files with command-line management files
There are many basic commands used to locate, view files and folders, including,,catlesswhichfileas wellfind. Don't forget, you canmanuse commands to check the instructions for each command.
Cat
catis "concatenate" meaning, will read the file sequentially and output to the Terminal window, the syntax is followed by thecatpath you need to view the file.catcommands can also be used to add content to a>>text file, such as a command that adds the contents of thecat ../textOne.txt >> textTwo.txttextOne.txt to the end of the textTwo.txt. This>>belongs to the "Extras" mentioned in the previous article.
Less
This command is more suitable for viewing long text files because it allows you to find text. The syntax is followed by thelessfile path, and thecatsame.lessthe file opened with the command is actually a viewer when you view the command, so the operation is the same, you can use the ▲ (up arrow) or (down arrow) to move the text up and down, use to page, enter/and keywords to follow the keyword search, press Q to exit the Usage Guide page. In addition, press the v key to use thevitext editor.
which
This command locates the file path of a command. In other words, it tells you which file to use when you execute a specific command. The syntax iswhichfollowed by a command.
File
This command attempts to output the file type based on the contents of the file. This command can be useful if a file is missing an extension. The syntax is followed by thefilefile path. , this example is a PNG file and gives information about its size, number of colors, and so on.
Find
This command is used to locate file paths based on search keywords.findthe command does not use the Spotlight search service, but it allows you to set very specific search conditions, as well as wildcard characters (described later). The syntax is the starting path to the next search, followed by the option to define the search, followed by thefindsearch (enclosed in quotation marks). For example:
Attention
- If you want to search the root directory, perhaps you want to use the-xoption to avoid searching the/volumes folder.
- If you want to use the Soptlight Search service, use themdfindcommand followed by the search keyword.
Using wildcard characters (Wildcard characters)
The following are common wildcard characters:
- asterisk (*,asterisk)-represents any character of any length. For example*.tiff, all files formatted as TIFF are represented.
- The question mark (?, Question Mark)--represents any single character. For example,b?okmatch book but does not match Brook.
- square brackets ([],square brackets)-defines a range of characters, such as[Dd]ocumentmatching document and document,doc[1-9]matching Doc1, doc2, ..., doc9.
The use of the above three wildcard characters can greatly improve the efficiency.
Using recursive commands
Simply put, a recursive command can allow a command to not execute on a specific file, but rather all files under the specified path. Most commands contain one-ror-Rthe option to set the command that you want to execute recursively. For example, the following example shows-Rhow thelscommand is executed after it is added:
Edit files and folders
There are many basic commands for editing files and folders, including,,,,mkdircpmvrmrmdiras wellvi. Let's take a brief look at these commands.
Mkdir
The "Make diretory" abbreviation, used to create the folder, is syntacticallymkdirfollowed by the directory of the new folder. You can use the-poptions to create a folder that doesn't exist in the path together (so you don't have to create a layer).
Cp
The abbreviation "copy", used to copy files from one place to another. The syntax is followedcpby the original path, followed by the destination path. If you want to copy the entire folder and all the content, you need to add an-Roption. If the specified destination path does not contain a file name, the CP command is copied as the original. If the specified destination path includes a file name, the file name that you specified is copied. If you specify only the new file name, a copy of the file is created with a new name. Note that files with the same name are automatically replaced without prompting.
Mv
The "move" abbreviation, used to move the file. The syntax ismvfollowed by the original path, followed by the new path. The specified path rule for MV is the same as the CP (yes, if you specify only the new file name, it becomes the Rename command).
Rm
The "remove" abbreviation will permanently delete the file. Note that there is no trash in the command-line. The syntax is followed by thermfile path. However, files that are deleted by using the RM command may be recovered through the data recovery tool. If you want to safely delete a file, you can use thesrmcommand.
RmDir and Rm-r
RmDir is the "remove directory" abbreviation, which permanently deletes the folder. Again, the wood in the CLI has a waste paper basket. The syntax isrmdirfollowed by the path where you want to delete the directory. However, the rmdir command cannot delete a folder that contains any other files, so thermdircommand is not applicable in most cases. However, you can userm-Rthe Add option to delete the folder and all the files it contains.
Vi
Represents "Visual" (visual), but the name is quite ironic: VI may be the worst-performing text editor. However, VI is the most common text editor in command line. To open a text file with VI, you just need to enter thevifile path. Mac OS X also provides ananomore modern text editor. It is also more convenient, for example at the bottom contains a cheat strip (=_=), which has a list of commonly used shortcuts (you don't have to memorize them). However, vi is sometimes the default text editor, so mastering VI is useful.
lesslike commands, thevicommand takes up the entire Terminal space to display the contents of the file. After opening, in "Command mode", VI will wait for you to enter some pre-defined characters to tell VI what you want to do. You can also use the arrow keys on your keyboard to simply navigate through the files. When you want to edit, press A to start (will enter edit mode). The text is inserted at the cursor. If you want to save, you need to exit edit mode to enter command mode first. method is to press the ESC key. When you return to command mode, hold down shift and press two times Z to save and exit. If you do not want to save, enter in command mode:quit!and press ENTER
Return to exit directly.
Using SU to switch users with the command-line management system
suThe command represents "substitute user identity", allowing you to easily switch to another user account on the command line. The syntax issufollowed by the short name of the user. You will then be asked to enter your password (but it will not be displayed when you enter it). Once executed, the prefix of the command changes to indicate that you have rights to other users. You can usewho -mthe command to verify the identity of the current login. After switching, you will always remain the user until you exit Terminal or enter aexitcommand.
About sudo using sudo overview
The more powerful command issudoto represent "substitute user do", or, more appropriately, "Super User do."sudousing the root account permission is used to execute a command. Of course, prior to using the Administrator account (Administrator account) authorization (such as entering a password).
By default, any administrator account can be usedsudoto gain root privileges, even if the root account is disabled on the graphical interfacesudo. This command is the reason why we have to use Terminal in many cases--and it is also a danger to the identity of each user administrator. However, you can adjustsudothe configuration file to limit its use.
bogon:~ renfei$ cat secret.txtcat: secret.txt: Permission deniedbogon:~ renfei$ sudo cat secret.txtPassword:This is the contents of the secret.txt text file that the user account renfei does not normally have access permissions to read. However, because he is an administrative user, she can use the sudo command to envoke root user access and read the contents of this file.
Tip : If the command line returns an error because you forgot to use itsudo, just entersudo !!itsudoto execute the previous instruction.
Remember, the greater the power, the greater the responsibility. Improper usesudocan easily damage your system settings. The command line will only prompt you before the first serious disruptive behavior, and then it will assume you know what you're doing. If you only have three guidelines for using the command line, it will be: always double-check your commands, always use tab completion to help you avoid spelling mistakessudo, and always check your commands carefully before using them.
Using sudo to switch the Shell
If you are an admin user, you need to perform many commands that require root privileges, and you can temporarily switch the entire command-line shell to get root-level access. The waysudo -sto do this is to enter it first and then type your password.
Tips for other command-line tips
- Enteropen .the command to open the current location with the Finder.
- In Terminal's preferences you can set its appearance and style.
- To abort a wrong or crazy command, you can use the combo key control + C.
- You can edit the command before executing, just use the arrows and other letters on the keyboard.
- If you do not enter any commands, you can use the ▲ and to navigate through the history commands. can also be edited and executed again.
- You can also use thehistorycommands to view the history.
- You can use the combo key control + L to clear the screen.
"Go" Mac OS X Terminal 101: Terminal use beginner Tutorial