background

Introduction to the Mac OS X Command Line

Introduction to the Mac OS X Command Line. How to use Command line in MacOS, including how to open the command line, Anatomy of the Console, Running a Command, and so on.

{"first":"Lightman","last":"Wang"}

Lightman

CEO

Introduction to the Mac OS X Command Line

This is a command line tutorial primarily conducted in in the OS X command line. Because of OSX’s unix heritage, much of the info here is also useful in other unix inspired systems, like the Linux command line.

The command line can be a scary place when you first encounter it. When you read some instructions that tell you to open up a terminal window and type some cryptic words and phrases, it can seem like you’ve been sucked into the matrix, expected to decrypt an endless stream of indecipherable characters.

Fear not, it’s really not that difficult to use. In fact, when you see an experienced user looking at a terminal that is scrolling line after line of text faster than you can even read it, they aren’t really reading it either. For the most part they may be scanning for some key words, but mostly they are just waiting for it to stop.

 

How to open the command line.

Before you can use it, you need to be able to find it.

So what we need to do is open the terminal. On OS X, open your Applications folder, then open the Utilities folder. Open the Terminal application. You may want to add this to your dock. I like to launch terminal by using Spotlight search in OS X, searching for “terminal”

Introduction to the Mac OS X Command Line

Anatomy of the Console

First let’s clarify a few terms.

Console: This is the system as a whole. This is both the command line as well as the output from previous commands.

Command Line: This is the actual line in a console where you type your command.

Prompt: This is the beginning of the command line. It usually provides some contextual information like who you are, where you are and other useful info. It typically ends in a $ . After the prompt is where you will be typing commands.

Terminal: This is the actual interface to the console. The program we use to interact with the console is actually a “terminal emulator”, providing us the experience of typing into an old school terminal from the convenience of our modern graphical operating system.

Running a Command.

Nearly all commands follow a common pattern with 3 main parts. The program, the options, and the arguments. Let’s see an example.

$ ls -l ~

Introduction to the Mac OS X Command Line

Type the code above. Do not type the leading $. This is a common convention used is denote what follows is a command to be run. Once you have typed it out, hit enter to run it, and see what happens.

The program is the verb. It describes what you want to do. In our example ls is the program.  ls is short for list, meaning, I want to see a list of files somewhere on my computer.

Options are like the adverb. They usually modify the way the program will run. In our example -l is an option. It’s short for “long”. Without this option, the list will be simply the filenames. When we modify the command with -l, it will display to us the files along with more detailed information. Options are just that: Optional. Any command should have some default behavior when called without options. Each command has it’s own options. Most often the order of the options do not matter, but occasionally they may.

The arguments are what’s left. In our case the ~ .These are the objects of our sentence. They describe what we want our command to act on. In our example the ~ is a shorthand name for a special folder on your computer: your home folder. So we are saying we want to list all of our files in our home folder. Some programs may not need arguments. For instance, without arguments, ls will list the files in the directory you are currently in. More on that later. Again, each program has different arguments, and the order of the arguments typically matter.

Where Are You?

In the console, you are always working in a directory, or folder, on your computer. We call this your working directory. You can see where you are using pwd(short for print working directory)

$ pwd

Introduction to the Mac OS X Command Line

This command will print out your current location. You can change your directory with cd (short for change directory). If you pass it an argument, it will change your to that location, if it exists. Without an argument, it will take you to your home directory (~).

$ cd Documents

You’ll notice that I just passed it a directory named Documents, because I was in my home directory, that contains a directory called Documents. This is relative path, because I specified my destination relative to my current directory. I can provide an absolute path by providing the full path beginning with the /, or starting with my home directory (~) such as:

$ cd /Users/jim/Documents

or

$ cd ~/Documents

If you want to navigate “up”, that is to the directory that contains your current directory, you can use the special name ..which you can even use separated by slashes to navigate several levels up.

From my Documents directory, this command will take me up to my home directory.

$ cd ..

Get Some Help, man!

Even when you learn what commands you can use, there is still a lot of power in each command or program. There are often dozens of available options, and depending on your arguments, your command could behave in several different ways.

Fortunately, most commands have a manual. To read, use the man command. Pass the name of the command you want to learn about as it’s only argument. For instance to learn more about ls, run

$ man ls

Introduction to the Mac OS X Command Line

The manual can be scrolled with the arrow keys or space bar. Pressing q will quit.

Want to know more about man? Run man man

Some more commands.

There are a ton of different commands you can use, but only a couple dozen will get you pretty effective in the command line.

We learned about lspwdcd, and man.

Try using the man command to learn about these commands

  • mkdir Make a new directory
  • touch Make a new empty file
  • cp Copy a file
  • mv Move a file
  • rm Remove a file or directory (learn about the -r option)
  • less Show the contents of a file in a scrolling buffer

 

 

 

即将开的培训课程

墨尔本Java就业集训班第2期

城市: 墨尔本
课程安排:一周2次课,每次3小时
开课时间:May. 25
早鸟截止日期:Mar. 19
早鸟价:3200
原价:3800
课程更新
顶级导师教学
针对就业
紧贴就业市场需求

悉尼web全栈班第7期

城市: 悉尼
课程安排:一周2课,每次3小时,外加2小时Tutorial
开课时间:Jun. 16
早鸟截止日期:May. 01
早鸟价:4200
原价:5000
课程更新
Nodejs后端
新增4项目
豪华团队
项目5选一

悉尼web全栈班第7期(包含一个月项目实习)

城市: 悉尼
课程安排:一周2课,每次3小时,外加2小时Tutorial
开课时间:Jun. 16
早鸟截止日期:May. 01
早鸟价:5000
原价:5700
课程更新
新增Nodejs
新增4项目
项目5选1
新增Redux

悉尼商业数据分析班第1期

城市: 悉尼
课程安排:一周2次,每次3小时
开课时间:Jun. 23
早鸟截止日期:Jun. 01
早鸟价:2970
原价:3630
课程更新
Power BI
SQL
Excel
VBA
Python

悉尼数据全栈(数据分析+数据工程+数据科学)项目班 第2期

城市: 悉尼
课程安排:每周两次,每次三小时,不定时tutorial
开课时间:Jul. 07
早鸟截止日期:Jun. 15
早鸟价:4600
原价:$5200
课程更新
对接就业
数据分析
数据科学
数据工程
实习机会
工作内推

布里斯班Web全栈班第7期

城市: 布里斯班
课程安排:每周6小时课程,分为3小时一天,两天完成,由学员们开课前统一确定商讨每周两天上课时间。
开课时间:Jul. 08
早鸟截止日期:May. 08
早鸟价:5200
原价:5700
课程更新
就业率高达86%!澳洲华人IT最高品质课程!
新增Nodejs
新增4项目

布里斯班Web入门班第4期

城市: 布里斯班
课程安排:每周6小时课程,3小时一节课分两天进行,会和所有报名的学员在开课之前一起商讨确认。
开课时间:Jul. 08
早鸟截止日期:May. 28
早鸟价:3000
原价:3400
课程更新
一个月实战本地商业项目实习!
入门即可独立制作网页接个人项目!