Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

How to use Terminal on a Mac: Introduction to OS X Terminal, Mac terminal tricks and shortcuts

Kenny Hemphill | May 5, 2015
Get more from your Mac with OS X Terminal. Here are the most commonly used Terminal commands.

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.

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.'

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.

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.

 

Previous Page  1  2  3  Next Page 

Sign up for CIO Asia eNewsletters.