A Crash Course in Using the Mac Terminal

by Milind Alvares

A Crash Course in Using the Mac Terminal

by Milind Alvares on November 9, 2010

Post image for A Crash Course in Using the Mac Terminal

Almost everything can be done with the Terminal. At least that’s what the experts tell you. We find glorious “Terminal Hacks” which unlock some special features of the Mac. While for most part it’s fine not knowing, it wouldn’t hurt to know what exactly is going on behind those lines. Here’s a short crash course in using the Terminal, from a total newbie at that. Though I did do my homework, my less than exceptional knowledge of the Terminal might produce some errors in this guide. I have tried all the commands myself, so I don’t suppose anything should happen to your system, but do always backup before messing around with your system. Always.

Where’s the Terminal?

Use Spotlight to launch it, or you’ll find it in the Applications/Utilities folder.

Navigating the Terminal

The Terminal will generally launch with your home directory selected. To navigate down a folder, enter “cd foldername”. To navigate up to the containing folder, enter “cd ..”.

You can even directly navigate to a specific folder. Enter something like “cd users/myhomefolder/documents”. “cd ~” takes you back to the Home folder. The tilde key (~) stands for home folder. You can enter “cd ~/music” to go into the music folder within your home folder. If you just want to get the job done, Cmd+C any folder in Finder, and Terminal will paste in its path.

To know that you’re in the correct directory, hit “ls” to bring up a list of file names. I’m guessing ls stands for list. While you’re writing out commands, you might want to navigate that line itself. ^a to get to the start of the line, ^e to the end. Option+Click on any part of the line to move your cursor there. ^w (Ctrl+W) to backspace on any word, while ^u will clear out the entire line prior to the cursor. ^k is the inverse of ^u.

One important command: ^r will allow you to cycle through your history of commands. So if you can’t remember which command it was that enabled some setting (and you want to disable it now), this is a good way to go back in time. You can also type in history to get a list of your past commands.

Sudo

Very often one comes across this command ‘sudo’, which is generally appended at the start of a string. ‘sudo’ stands for ‘super user do’. The sudo command provides logging for individual commands run and some cases it has completely supplanted the root logon for administrative tasks. That is, it runs the command as root user, which is useful if the system isn’t allowing you to perform some tasks such as deleting, etc. To get into root mode, type in “sudo -s”. To test whether you’re in root mode, type in whoami (follow by a return, of course). To get out of root mode, type in exit.

Creating Symbolic links

Symbolic links allow you to create aliases that behave like normal files and folders. Which means you can have a symbolic link to a folder in your projects folder, that’s also present in your dropbox folder. Dropbox will think that symbolic link is a real folder, and sync the data up into its cloud, not realising that the files are in a totally different location. In this example, we’re going to create a symbolic link to Things’ database:

ln -s ~/Li- brary/Application\ Support/Cul- tured\ Code ~/Dropbox/Library/ Cultured\ Code

ln stands for link name, and -s stands for creating a symbolic link. The reason for those reverse slashes, is because there’s a space in the names of those folders. There are so many use cases for symbolic links, so be creative.

Nano: The Terminal text editor

If you’re feeling extra geeky, you can bring out the Terminal text editor to create new text documents or edit existing plaintext files — all in monospaced font and UI straight out of the 80s. Simply type:

nano “my filename.txt”

If it finds a matching filename in the current directory, it will open it. If not, it’ll create it for you. Navigating inside of the text editor is solely via the keyboard, where the bottom row shows you the available commands. It’s fun.

Splitting large files using the Terminal

It’s always bad timing, where you have to copy that large file over, but your flash drive is still in FAT32 format, or your email service doesn’t allow large files, or .

After navigating to the folder your file resides in, enter “split -b 10m filename.dmg part_”. Here ‘split’ is self explanatory; tells the computer to split the file. “-b 9m” stands for the file size. In place of 9 you enter whatever size in MB you want the file to split into. Replace m with k for kilobytes. Then comes the filename itself, and lastly “part_” is an appendage to the split files.

To join the files, “cat part_a[a-g]>filename.dmg”. I assume ‘cat’ stands for the house cat, which then specifies which files you need to rejoin. “[a-g]” is the last letter of the files that the previous step spits out.

Useful Terminal Commands

Every software release from Apple brings out some new terminal commands. I don’t know how they find these commands, but they always make their way into the Apple community. They’re usually just boolean commands, meaning you have a string of text with a YES or NO at the end. Either way enables or disables the feature.

Enable path bar.

If you want your Finder window titlebar to always show the directory structure, you can enable it using a single command:

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES

QuickTime X

With QuickTime X we lost all power over its preferences. Thankfully there are still some settings in the player; they’re just not accessible via a GUI.

Always hide titlebar:

defaults write com.apple.QuickTimePlayerX MGCinematicWindowDebugForceNoTitlebar 1

Unfortunately, this means you can’t close the movie window or minimise it.

Keep fullscreen while App Switching:
If you Cmd+Tab or somehow interact with another app while QuickTime X is playing something, it’ll automatically exit fullscreen mode. This command ensures your fullscreen stays intact.

defaults write com.apple.QuickTimePlayerX MGFullScreenExitOnAppSwitch 0

Autoplay movies:
If you’re tired of hitting the play button every time you launch a movie, this command will do the obvious.

defaults write com.apple.QuickTimePlayerX MGPlayMovieOnOpen 1

There are more commands for QuickTime X, if you feel the need.

List view stacks

If you’re not already using this feature, you’re missing out. Instead of the showy stacks or the extra large grid, you can have the best of both words, in a slick list-stack. Once you’ve enabled it, hit the + or – buttons on your keyboard to increase or decrease the icon sizes.

defaults write com.apple.dock use-new-list-stack -bool YES

Global text substitutions

If you want to enable all the text substitution features for every app that supports it, just blindly copy paste it in. On the other hand, if you want just one of the features enabled, read through the command and remove all that you don’t need. You don’t need this command really, since you can very well manually enable text substitution on a per app basis.

defaults write -g WebAutomaticTextReplacementEnabled -bool truedefaults write -g WebAutomaticDashSubstitutionEnabled -bool truedefaults write -g WebContinuousSpellCheckingEnabled -bool true

Dock tricks

The Dock responds well to terminal commands. You can move your Dock to the left or right by entering:

defaults write com.apple.dock pinning -string start

Replace start with end if you want it on the right. Replace with ” ” to restore position. You need to run a killall Dock for any of the settings to take action.

There’s also a command to make hidden application icons show translucent:

defaults write com.apple.Dock showhidden -bool YES

You can also add a free moving spacer to your dock icons by entering the command from this txt file.

And finally, to get the 2D dock:

defaults write com.apple.dock no-glass -boolean YES

Hide desktop icons

This one I’ve found really useful. With just one command all my desktop icons are always hidden from view.

defaults write com.apple.finder CreateDesktop -bool false

That’s it for now

We’ll continue posting cool terminal commands as and when they surface, but till then this list should do. There are a lot more commands, but most of them are for nerds (more so than you and me), or things that make your Mac look ugly. If you have any killer commands, shoot them into the comments and we could even add them into the article.

{ 16 comments… read them below or add one }

Nicholas Smith

How long til a command line text editor holy war starts? Place bets now!

   

MonsterG

Even though I didn’t have very many desktop icons, the hide desktop icons command is AWESOME!

   

blah

Yeah, love the hide desktop trick. I had done this with “Secrets”, but never knew the actual command. I just hate looking at icons, but now my desktop has gotten really junky and I don’t have an incentive to clean it now that I don’t see the icons. ;)

   

trucklover

Just a correction to the article. Everything is case sensitive in Unix operating systems which includes OS X, Linux, FreeBSD, etc. The sentence that states “You can even directly navigate to a specific folder. Enter something like “cd users/myhomefolder/documents” will not work. You would need to type in this: /Users/shortusernamehere/Documents in OS X where the user accounts are stored in /Users by default. In Linux operating systems it would be /home/shortusernamehere/Documents where the user accounts are stored in /home. Nano is a great text editor at the command line. I find it far easier to use than vi (pronounced v i). Learning the command line is extremely valuable. Learning how to navigate the system, ssh, and forwarding X over ssh are great things that any budding Unix user should learn how to use.

   

Richard Bronosky

The default Mac OS install creates a non-Case-sensitive format. I hate that they made this choice. It leads to sloppy code development. For example Adobe developed their code on a case-insensitive FS and that allowed sloppy code to pass QA. Now their products will only run an a non-Case-sensitive formatted drive. http://www.google.com/search?q=adobe+CS+case-sensitive

You check a disk from the command line with diskutil. You can see the difference in this example:

$ diskutil info /
Device Identifier: disk0s3
Device Node: /dev/disk0s3

File System: Journaled HFS+
Type: hfs
Name: Mac OS Extended (Journaled)
$ diskutil info /Volumes/C\ Development
Device Identifier: disk2s2
Device Node: /dev/disk2s2

File System: Case-sensitive Journaled HFS+
Type: hfs
Name: Mac OS Extended (Case-sensitive, Journaled)

   

Donald Lyles

The article is informative. However, one thing that should have been considered is to have also provided the appropriate command to “undo” the tweak that was offered. For instance it would have been complete to have shown the way to bring back desktop icons -without having to guess what that command would be.

   

Milind Alvares

The goal behind the article is to give you a general sense of how to navigate the terminal, and not just dish out a bunch of codes. If you can’t figure out the logic behind -0/-1 or true/false switches, you probably shouldn’t be messing with the Terminal.

   

Bart

After the “Hide desktop icons” you’ll also need to run killall Finder to make it work without restarting the system.

   

Eddie

That’s easy!
Just change the command you used:
defaults write com.apple.finder CreateDesktop -bool false
To:
defaults write com.apple.finder CreateDesktop -bool true

After both of these commands use the command killall Finder to see its effect.

   

Aaron

Ya sorry i didn’t read before i posted that, I figured it out but thanks anyway. Also, I was wondering when is it necessary to use sudo mode? I don’t really feel comfortable using it yet since it is possible to mess with your system. What kind of consequences are there?

   

Karti

Is it possible to change non-case-sensitive terminal to case-sensitive one?

   

Jane

Hi,

Enjoyed that a lot BUT I was just wondering why you would want to hide your desktop icons. Why would you have so many that they are distracting? If you’ve got a lot of shortcuts you can just get rid of them or move them. I just don’t understand this. Sorry if I’m being a bit thick here but I’ve been using Macs for 25 years and I’ve never had huge amounts of icons on my desktop, so I’m intrigued. Thanks.

   

Milind Alvares

Since you’re intrigued:

I use the desktop as my dump for all temporary files. If I’m saving something in PS that I need to upload to the web? It goes on the desktop. Why? Because of the shortcut Cmd+D in the Open/Save menu. It’s so easily accessible that it’s tuned into my muscle memory. The only other shortcut that works there is for the home folder, but I don’t want to crowd that. That’s why.

   

Jane

Thanks for that. Sorry to be an absolute pain but you mention to Cmd+D in the Open/Save menu but I’m not sure what you mean. How does it work since it might be useful to me. I use Default Folder X to keep my desktop uncluttered.

   

Steve

Jane,

He means the Save As dialog.

Command + Shift+S then Command D then the return key saves your file to the desktop.

   

Jane

Oh right, thanks for that Steve. As I said, I use ‘Default Folder X’ which means I can designate commands to save to whatever folder I like using Alt + a letter that represents the folder. So saving to my desktop is Alt D first and then I do the Command Shift S stuff. I’ve been using it so long I’ve forgotten how to do things the original way but it’s always nice to know :-)

   

Leave a Comment

We'd love it if you would add your opinion to the article or discussion, whether positive or negative. We reserve the right to moderate comments at our discretion.

Previous post:

Next post: