How to use Terminal on a Mac

版权声明

该文章为转载,作者是Kenny Hemphill
只为方便学习而保存
如果有侵犯相关权利,请联系删除

Paste_Image.png

Terminal provides a command line interface to control the UNIX based operating system that lurks below OS X. Here's everything you need to know about termial, and what it can do for you and your Mac.
Of all the tools tucked away in OS X's Utilities folder, few are as mis-understood as Terminal. At first glance, it's the very antithesis of what the Mac is all about. Isn't the whole point of a graphical user interface that we don't need to concern ourselves with learning arcane instructions and typing them on a command line? Well, yes it is, but learning those commands and using them allows you to customise elements of your Mac, perform tasks that would otherwise be cumbersome or require additional software, and troubleshoot quickly when things go awry.

We also have a Mac Terminal Tutorial where we show you how to do 10 fun things on the Mac using Terminal here
What is Mac Terminal?
The first thing to understand about Terminal is that it's just an application and it lives in the Utilities folder in Applications. You launch it like any other and when you do, you'll see Apple's implementation of a Unix command-line environment, known as a shell. There are various types of shell; Apple uses one called Bash.
The title bar of a Terminal window displays the name of the current user, the type of shell, and the size of the window in pixels. If you look at the command-line inside the window, you'll see that each line starts with the name of the Mac and is followed by the name of the current user. The 'cursor' is indicated by a shaded box.
You can run other shells with Terminal, but you'll have to install those yourself. Commands in Unix are shell-specific, so it's important, say when you're following tips written for a different flavour of Unix, that you use the right shell for the commands, or vice versa.
How to use Terminal on a Mac
We're getting ahead of ourselves, however. Using Terminal is straightforward, you type a command on the command-line and press Return to execute it. A command has three elements to it; the command itself, which calls a specific tool, an option which modifies the command's output, and an argument, which calls the resource on which the command will operate.
Often, the argument takes the form of a specific file, in which case you need to type the file path at the end of the command. There is a shortcut, however. If you locate the file in the Finder, you can drag and drop it onto the Terminal window and Terminal will extract its path and slot it into the command for you.
There are a few rules that you need to bear in mind when using any command-line interface. One of these is that every character, including spaces, matters. So if you copy a command from a website, magazine, or book, you need to make sure you type it exactly as it's shown.
You can re-run previous commands without re-typing them by using the up arrow on the keyboard to navigate to the command and then pressing Return. And you can interrupt a command by pressing Control-C. To see a list of available commands, hold down the Escape key and then press 'y' when asked if you want to display a specific number of possibilities. You'll see a list of commands, with their meanings next to them. If you press spacebar more commands will load. Press 'q' to exit and return to a command prompt.
Unix has its own built in manual and you can call it in Terminal to find out more information about a command. To use it, type man command, where 'command' is the name of the command on which you want more information.
When you type a command in Terminal, it's executed in your current location, unless you specify otherwise. When you launch a new Terminal window, that location is at the top level of your Home directory, so every command is relative to that location. To change location, use cd followed by the path of the location you want to move to. To return to the default, type cd ~/ and your location will change from wherever you are to your Home directory. You can view a list of the files and folders in your current location by typing ls to list them in Terminal.

Paste_Image.png

The LS command displays the contents of a directory
How does the command-line work
To get an idea of how the command-line works, let's start with some basic commands. Create a file in Text Edit called TestFile and save it to a folder called Test inside Documents in your Home Directory. In Terminal type cd Documents/Test to navigate to the Test directory. Now type ls to display the contents of the directory. You should see the result 'TestFile.rtf.'

Paste_Image.png

The cp command is used to copy a file.
You can make a copy of the file using the cp command. Type cp TestFile.rtf TestFile-copy.rtf and check the result in the Finder. We're now going to use Terminal to move the copy to another directory, which we'll also use Terminal to create.

Paste_Image.png

Moving a file using Terminal
Type cd then drag your Documents folder onto the Terminal window and press Return. That will place you in the Documents folder in your Home Directory. Now type mkdir Test2 to 'make' a new directory called Test2. To move the file TestFile-copy.rtf to the Test2 directory, type mv ~/Documents/Test/TestFile-copy.rtf ~/Documents/Test2/TestFile-copy.rtf The '~' is shorthand for your Home directory, so '~/Documents' is the Documents folder in your Home folder. You can also use the mv command to rename files. So, in our example above, instead of TestFile-copy.rtf, you'd give the moved file a different name. To rename files without moving them, just remove the second directory from the command.
To delete the test files, use the rm command. So, to remove our original test file, we'd type rm ~/Documents/Test/TestFile.rtf That will delete the file without asking for confirmation. If you're deleting files, it's good practice to double-check, so to add a confirmation step put -i immediately before the file name.
Those are just a few of the most commonly used Terminal commands. But the should give you a flavour of how to use this most under-appreciated of OS X utilities. It should also show you that there's nothing to fear in using Terminal, so long as you take the time to learn the basics and understand what your doing.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,686评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,668评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,160评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,736评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,847评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,043评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,129评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,872评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,318评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,645评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,777评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,861评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,589评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,687评论 2 351

推荐阅读更多精彩内容