Mac OS X Command Line

Source: Internet
Author: User
Tags parent directory

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 line man command-name . For example, if you want to know ls how to use this command, you can enter it man ls into 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? Input man -k and 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.


    • 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 input itunes is invalid and must be entered .
    • 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 written Public/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 enter cd .. 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 , ls and cd .

    1. pwdThe meaning is "Print working directory", which displays the absolute path of the current directory.
    2. lsThe meaning is "list directory Contents", which lists the contents of the current directory. This command also has additional parameters to choose from.
    3. 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 the cd command to enter your home folder, cd P and 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 fill Public/ . 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, the ls command hides . the files that begin with the file name, but these files can be displayed by using the -a options. For example:

michelle$ ls -la

We have also added an -l option 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 -a change 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,, cat less which file as well find . Don't forget, you can man use commands to check the instructions for each command.


catis "concatenate" meaning, will read the file sequentially and output to the Terminal window, the syntax is followed by the cat path 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 the cat ../textOne.txt >> textTwo.txt textOne.txt to the end of the textTwo.txt. This >> belongs to the "Extras" mentioned in the previous article.


This command is more suitable for viewing long text files because it allows you to find text. The syntax is followed by the less file path, and the cat same. 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 the vi text editor.


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 is which followed by a command.


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 the file file path. , this example is a PNG file and gives information about its size, number of colors, and so on.


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 the find search (enclosed in quotation marks). For example:


    1. If you want to search the root directory, perhaps you want to use the -x option to avoid searching the/volumes folder.
    2. If you want to use the Soptlight Search service, use the mdfind command 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?ok match book but does not match Brook.
    • square brackets ([],square brackets)-defines a range of characters, such as [Dd]ocument matching 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 -r or -R the option to set the command that you want to execute recursively. For example, the following example shows -R how the ls command is executed after it is added:

Edit files and folders

There are many basic commands for editing files and folders, including,,,, mkdir cp mv rm rmdir as well vi . Let's take a brief look at these commands.


The "Make diretory" abbreviation, used to create the folder, is syntactically mkdir followed by the directory of the new folder. You can use the -p options to create a folder that doesn't exist in the path together (so you don't have to create a layer).


The abbreviation "copy", used to copy files from one place to another. The syntax is followed cp by 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 -R option. 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.


The "move" abbreviation, used to move the file. The syntax is mv followed 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).


The "remove" abbreviation will permanently delete the file. Note that there is no trash in the command-line. The syntax is followed by the rm file 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 the srm command.

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 is rmdir followed by the path where you want to delete the directory. However, the rmdir command cannot delete a folder that contains any other files, so the rmdir command is not applicable in most cases. However, you can use rm -R the Add option to delete the folder and all the files it contains.


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 the vi file path. Mac OS X also provides a nano more 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, the vi command 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 is su followed 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 use who -m the command to verify the identity of the current login. After switching, you will always remain the user until you exit Terminal or enter a exit command.

About sudo using sudo overview

The more powerful command is sudo to 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 used sudo to gain root privileges, even if the root account is disabled on the graphical interface sudo . 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 adjust sudo the 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 it sudo , just enter sudo !! it sudo to execute the previous instruction.

Remember, the greater the power, the greater the responsibility. Improper use sudo can 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 mistakes sudo , 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 way sudo -s to do this is to enter it first and then type your password.

Tips for other command-line tips
    • Enter open . 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 the history commands to view the history.
    • You can use the combo key control + L to clear the screen.

Mac OS X Command Line

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.