dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH...

20
The first thing to know about the Terminal is how to launch it, which you do by going to \Applications\Utilities\Terminal. Also, you’re no longer in graphical user-interface land: Most of what you’ll do in the Terminal is enter specific text strings, then press Return to execute them. When you see generic references like name-of-file or path-to-file throughout this article, replace that text with your file name or path. 1. Handy Terminal Pointers > Only text commands are accepted--you won’t use your mouse much within the terminal, with a few exceptions. > Execute commands by pressing Return. > Every keystroke matters, including spaces and special symbols. > Interrupt any running command by pressing Control-Z. > Recall previously entered commands without retyping them by pressing the up arrow. > Curious about a new command? Type man name-of-the-command, then press Return to read its instruction manual. For instance, man SSH will produce the manual for SSH. > For a list of all commands available in the Terminal, hold down Escape, then press Y when prompted. > Page down through multipage results by pressing the space bar. > For commands that require the path to a file or folder, save yourself some typing by dragging and dropping the file or folder at the end of the command. The Terminal will automatically copy the dropped item’s path and name. 2. Know Your Way Around By default, the Terminal targets your Home directory (folders are called directories in Terminal-speak). You can move to different directories by executing the command cd path-to-directory. If you get lost, type cd ~ to return to your Home directory. View a list of all files in the Terminal’s current directory by executing ls. 3. Anatomy Of A Terminal Command

Transcript of dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH...

Page 1: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

The first thing to know about the Terminal is how to launch it, which you do by going to \Applications\Utilities\Terminal. Also, you’re no longer in graphical user-interface land: Most of what you’ll do in the Terminal is enter specific text strings, then press Return to execute them. When you see generic references like name-of-file or path-to-file throughout this article, replace that text with your file name or path. 1.  Handy Terminal Pointers>    Only text commands are accepted--you won’t use your mouse much within the terminal, with a few exceptions.>    Execute commands by pressing Return.>    Every keystroke matters, including spaces and special symbols.>    Interrupt any running command by pressing Control-Z.>    Recall previously entered commands without retyping them by pressing the up arrow.>    Curious about a new command? Type man name-of-the-command, then press Return to read its instruction manual. For instance, man SSH will produce the manual for SSH.>    For a list of all commands available in the Terminal, hold down Escape, then press Y when prompted.>    Page down through multipage results by pressing the space bar.>    For commands that require the path to a file or folder, save yourself some typing by dragging and dropping the file or folder at the end of the command. The Terminal will automatically copy the dropped item’s path and name. 2.  Know Your Way AroundBy default, the Terminal targets your Home directory (folders are called directories in Terminal-speak). You can move to different directories by executing the command cd path-to-directory. If you get lost, type cd ~ to return to your Home directory. View a list of all files in the Terminal’s current directory by executing ls.  3. Anatomy Of A Terminal Command

Page 2: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Unlike the sentences you were forced to diagram in sophomore English, Terminal commands only have three parts: the command, which calls a specific command-line utility; the options, which modify the command’s output; and the argument, which is typically the website, file, or other resource that the command will be operating on. 4. Get Comfortable in Your ShellThe Terminal app is Apple’s implementation of a traditional Unix command-line environment, also called a shell. Keep in mind, though, that Unix shells come in many different flavors. OS 10.5 (Leopard), for instance, uses the Bash shell by default. If this article inspires you to Google even more command-line tricks, remember that some command-line utilities are shell-specific, meaning that what works in Tcsh on that helpful blogger’s Linux box might not work in Bash on your Mac. 

When it comes to customizing the look and feel of your Mac, the options listed in System Preferences only scratch the surface. Hiding throughout the OS are dozens of hidden preferences that can only be changed through the defaults command. Too many exist to list them all here, so we’ve compiled our five favorites. Always remember to quit an application before modifying its defaults. You can reset any of these commands by replacing TRUE with FALSE and executing it again. 5. Make All Links In Safari Open As New Tabs

Page 3: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

New windows, baaad. New tabs, gooood.In general, Safari’s tab controls are wonderful, but one failing drives us crazy: Certain links are allowed to override your preference for opening new webpages in tabs, essentially forcing the application to open a new window. To prevent this in the future, execute this command: defaults write com.apple.Safari TargetedClicksCreateTabs -bool TRUE.

6. Force Mail To Display All Messages As Plain Text

Page 4: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Even those annoying bank statements appear in plain text, thanks to the magic of defaults.Few things are more irritating than that office mate who just discovered how to make colored fonts and add funny graphics in his email program. With the exception of Mail’s attractive New Message templates, most heavily formatted messages are spam anyway, so make things easier on your eyes by forcing all messages to display as plain text by executing this command: defaults write com.apple.mail PreferPlainText -bool TRUE. 7. Show Hidden Files in The Finder

Page 5: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

The names of hidden files always begin with a period--keep that in mind before you delete or edit a file that doesn’t look familiar.Believe it or not, the files you see listed on your Desktop in the Finder do not represent all of the files contained in your Desktop folder. In almost every folder, the OS hides system files that Apple considers too important for the likes of us to mess with (or too mundane for us to be bothered with). Now and again, though, it’s useful to view these files. To see the full contents of all folders in the Finder, execute  defaults write com.apple.finder AppleShowAllFiles TRUE. 8. Change The File Format For Screenshots

Page 6: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

PNG, or portable network graphics, is a file format that offers high image quality and small file sizes, but may not be opened by all image-editing software.OS 10.5 saves screen captures as PNG files by default, but it can save them in a variety of formats, including PDF, JPG, and GIF, to name a few. Switch to your preferred format by executingdefaults write com.apple.screencapture type file-extension, replacing file-extension with the three-letter abbreviation for your chosen format. For instance, to change the default format to PDF, you’d execute defaults write com.apple.screencapture type PDF. 9. Disable the Dashboard

Page 7: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

When the Dashboard appears on our Desktop, it’s usually because we missed the delete key and hit F12 instead.We’ve always liked the Dashboard in theory--on occasion, we’ve even downloaded widgets for it. Unfortunately, we never get around to using them, and our aging Mac laptop could use the extra RAM to run real apps. If you’re in the same boat, free up some system memory by terminating the Dashboard with two quick Terminal commands. First, set its default to Off by executing defaults write com.apple.dashboard mcx-disabled -boolean YES. Second, kill and restart the Dashboard and Dock with this command: killall Dock.

The Unix command line is where many (if not most) of OS X’s security practices were born--and it’s still the place to go to lock down files, resolve system conflicts, and erase your tracks. 10. Compress And Password-protect A File or Folder

Password-protected archives can’t be expanded by the Finder, a limitation that adds another layer of hassle, er, security.The Finder can make zip archives from files and folders in one click from the File menu or the contextual menu summoned with a Control-click. When security is an issue, the Terminal’s zipcommand supersedes that with its ability to encrypt archived files and folders. The encryption standard is relatively weak by military or industrial standards, but it should be more than enough to defeat a nosy boss or family member. To create an encrypted archive from a folder in the current directory, execute zip – re name-of-archive name-of-folder, replacing name-

Page 8: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

of-folderwith the name of the folder you’d like to compress and name-of-archive with the name you’ve chosen for the zipped file. To retrieve the contents of an encrypted archive, execute unzip archive-file-name.

11. Fix File Permissions

When permission errors prevent you from accessing files or folders, chmod and chown can sort them out.In a multiuser system such as OS X, file permissions ensure privacy and security. Now and again, though, they can cause problems, especially when different users are swapping things through a shared folder on the same machine. If the permissions controls in the File Info dialog fail to resolve a problem, chmod and chown are certain to.Generally, chmod is used to assign role-based permissions for users other than the owner. To ensure that anyone can open and modify a file, execute sudo chmod 777 path-to-file, wherepath-to-file is replaced with the actual file path, which you can generate automatically by dragging a file from the Finder window into the Terminal window.This assigns full permissions to anyone who uses that machine, which is bad for security but good for convenience. For more limited permissions, execute sudo chmod 644 path-to-file.This allows anyone to access and open the file, but only the file’s owner can modify it. To modify permissions on a folder and all of its contents, add the -R option after the command name.Chown, on the other hand, assigns ownership of a file to particular user. If a file you’ve copied from someone else’s account refuses you access, execute sudo chown your-short-user-name path-to-file. 12. Securely Erase Free Space On Your Dard Drive

Page 9: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Disk Utility’s 35-pass standard provides a high degree of security, but can take days to complete on larger drives.It may sound strange, but deleted files aren’t actually deleted. Emptying the trash merely marks hard drive space as available, without actually removing the data that occupies it. When new data needs the space, it writes over the old data, but until then, anyone with the right software can still recover the original data--and sometimes even after.Disk Utility offers a one-line Terminal command to scrub free space of existing data. Executediskutil secureErase freespace 3 /Volumes/name-of-drive. The utility will write to each drive sector 35 times, using a special algorithm. Thirty-five passes is well above the U.S. Department of Defense’s own standard for erasing data, which requires only seven passes. Be Careful With SudoNote that chmod, chown, and several other commands in this tutorial are running under sudo, which requires an administrator’s password before executing the proceeding command, because it executes commands as the administrator, overriding the system’s usual warnings and precautions. Be extremely cautious with this—if sudo tells the system to erase the entire hard drive, for example, the system will do it, no questions asked.

Page 10: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Terminal commands aren’t limited to the computer in front of you. One of their most practical applications is controlling other Macs through remote shell connections. 13. Establish A Secure Connection

SSH connections require less bandwidth and system resources than screen sharing.SSH, or secure shell, opens a Terminal session on a remote machine, so all commands are then executed in the remote Terminal rather than the local one. The connection is encrypted, meaning none of the traffic passing between the two machines can be read if intercepted.To get started, first make sure that SSH log-ins are enabled on the remote Mac by opening System Preferences and selecting the Sharing pane. Check the box next to Remote Login, and theSSH service will activate.

Page 11: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

To connect to a remote computer via SSH, open the Terminal on the local machine and execute this command: ssh -l username remote-address.Replace username with your username on the remote Mac and remote-address with the remote Mac’s IP address. (The remote IP address is listed in the remote machine’s Sharing pane when you click on Remote Login.) Press Return, and enter the remote username’s password at the prompt.SSH will connect you to the remote machine. (If you’re asked to add the remote machine to a list of known hosts, press Y for yes.)Once you’re connected, you can execute any Terminal command remotely. 14. Freak Out Your Roommates

Your Mac’s reading voice won’t win an Oscar, but it’s plenty good for gags.There are hundreds of useful remote commands, but let’s get you started with a good gag. If you know that someone else is using the remote computer, SSH into the machine, and execute say “Ouch. Don’t press the keys so hard.”The remote machine’s default voice will read the statement over the speakers. This is even funnier if can do it from a laptop in the next room. (Please use this power responsibly. Impersonating deities or the Department of Homeland Security is bad form.)Pranks aside, say can quickly create a machine-read audio book from a text file. Execute say -o savedaudio.aiff -f file-to-read.txt. Say will convert the file and save it as savedaudio.aiff in the Terminal’s current directory. 15. Troubleshoot A Remote Server’s Network Connection

Page 12: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Ping is a little like the Energizer Bunny: It keeps going and going. Press Command-Z to interrupt it.Remote servers are great--until they stop responding. Then one of the first questions to answer is whether the entire machine has gone offline or whether a particular service (such as remote log-in) has stopped responding. To find out, execute ping remote-address. (For instance, toping our home file server from work, we execute ping 72.204.20.186.) The utility will send a generic knock-knock to the server. If the server is still operating and attached to the network, ping will display each response, along with the time it took to receive it.

16. Copy Files Across A Secure Network connection

Page 13: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Be careful with syntax and argument order when using SCP. The longer the command, the easier it is to make a typo.The next time you leave your jump drive at home, grab the files you need over the network with secure copy, or SCP, which piggybacks on SSH, so you’ll need Remote Login enabled on the remote machine. The syntax for this command can get a bit long--in addition to the remote machine’s address and log-in info, you’ll need the names and locations of the files you want to copy--but it’s well worth the effort if it saves your bacon for that big client presentation. LikeSSH, SCP transfers data over an encrypted connection.To copy a file from a remote machine, execute scp username@remote-address:path-to-file target-file.For instance, to copy a file called rental_agreement.doc from our remote machine to a new file called agreement.doc on our local machine, we’d execute scp [email protected]:/Users/tandorra/Desktop/rental_agreement.doc agreement.doc.Obviously, the burden in this situation is knowing the exact location and name of the file before transferring it. Copying a file from your local machine to a remote one is a little easier. In that situation, we’d execute scp file-to-copy user@remote-address:path-to-new-saved-file.In our example, we could also replace our originally copied file--agreement.doc--to the remote machine with this command: scp agreement.doc [email protected]:/Users/tandorra/Desktop/agreement.doc. 17. Turn Off A Mac Remotely

Page 14: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Executing shutdown while someone else is in the middle of something important on the remote computer is mean, so use it with caution, OK?If you need to turn off a Mac after leaving the house, SSH into the machine, and execute sudo -h shutdown now.

If you like Spotlight, you’ll love grep, an old-school pattern-matching utility. Like Spotlight, grepsearches the full contents of files. Unlike Spotlight, however, grep specializes in locating patterns, which makes it ideal for analyzing text documents. Meanwhile, diff, fmt, and textutil offer other ways to quickly compare, format, and manage text files. 18. Find Patterns In Text DocsOne of our favorite uses of grep is checking finished documents for words and phrases we use too frequently. For instance, we tend to overuse compound sentences joined by “but.” The following command tells us how many lines in article.txt contain the offending word: grep -ic but article.txt.

Page 15: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

n this example, the i option tells grep to ignore case (counting both uppercase and lowercase instances), and the c option tells it to return only the number of matches, not all of the lines in which the search term appears. For details, execute man grep.

19.  Compare The Differences Between Two Text Files

Management--there’s another word we use way too often.Here’s one for writers and office workers who deal with a lot of document revisions. The next time you need to quickly compare the differences between two text files, execute diff -y name-of-first-file name-of-second-file.The -y option tells diff to split the output into two columns, one for each file, so the differences can be seen more easily. 20. Combine And Normalize Text Files

Page 16: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

We could combine and convert all these documents by hand, or we could execute a single command in the Terminal.If you’ve ever been involved in a large research project, you know how cutting and pasting from lots of different sources can produce a Frankenstein-like collection of documents, with different line lengths and inconsistent spacing. Next time, combine and clean those docs with this command: fmt -sp filneme1 filename2 filename 3 > name-of-new-file.txt.The command will force line lengths of 65 characters, and normalize tabs and spacing. 21. Combine And Convert Documents of All TypeNot everyone wants or needs a copy of Microsoft Word. Unfortunately, we all sometimes need to work with Word documents. Textutil can convert between Word, rich-text, and plain-text formats--and it can combine multiple documents, change fonts, and adjust font size while doing it. To convert and combine all Word documents in the current directory to a single rich-text document called combined.rtf, execute textutil -cat rtf  -output combined.rtf *.doc.Sometimes, of course, you only need to convert a single file. Use this command to do so while changing the font to 12pt Helvetica  textutil -cat rtf -font Helvetica -fontsize 10 -output converted-file.rtf file-to-convert.doc.

Page 17: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

One of the reasons we use Macs is how self-sufficient OS X tends to be. Most of the time, system maintenance occurs in the background, and mundane tasks that do require user participation (backups, for instance) are made as painless as possible (thank you, Time Machine.) For those times when we want or need more system-level control, these are the Terminal commands to rely on. 22. Backup In A Snap

Rsync has far too many options to mention. Execute man rsync for the full list.In any article about the Terminal, federal law requires us to mention rsync, the only backup utility you really need. Rsync can create full bootable backups of your entire hard drive or any combination of folders. On subsequent backups, it’s smart enough to copy only those files that have changed, saving a lot of time. Indeed, rsync is so powerful and flexible that many graphical backup utilities are little more than shiny wrappers that call rsync in the background.To back up your entire Home folder, execute rsync -auE --progress ~ /Volumes/name-of-drive/name-of-backup-folder.

Page 18: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Replace the placeholders after the last two slashes with the name of your drive and backup folder, respectively. To back up your entire startup drive, run sudo rsync -auE --progress / /Volumes/name-of-drive/name-of-backup-folder. 23. Identify And Eliminate A Memory Hog

Take out your aggression on misbehaving processes with the kill command.When sluggish performance slows your desktop to a crawl, a runaway application might be the culprit. Check CPU and memory usage in all open applications by executing top. The command returns a list of every running process our your Mac, along with its allocated resources. The CPU field is easy enough to interpret--any process using more than half is sure to slow others down. The memory fields are a bit more complicated but, in general, the bigger the numbers, the greater the drain on your system.If the problem app is visible on the Desktop, quit it as you normally would, using Command-Q or, if that fails to respond, using Force Quit (Command-Option-Escape).

Page 19: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

But if the culprit is a lower-lever utility that runs behind the scenes, you’ll need more Terminal magic to end its misadventures. Make note of the problem process’s number in the PID column, and execute sudo kill PID-number, replacing PID-number with the application’s actual number. The targeted process will terminate, freeing up its system resources.Be extremely careful when killing background processes. Some are necessary for OS X to function. Ideally, you would only use kill if you were absolutely certain that a third-party background utility was slowing you down.  24. Find Free Space On All Your Hard Drives

You might find you have a lot more—or a lot less—free space on your drives than you thought.At last count, our primary Mac was spread between three different hard drives--one internal for the OS and essential documents, and two externals for our photos, movies, and music. In this kind of setup, it can be difficult to know at a glance just how much free space you have left--and where, exactly, it is.To see your free space, execute df -hl. 25.  Snap superior screenshots

Page 20: dbechtold.weebly.comdbechtold.weebly.com/uploads/2/3/7/5/23753579/code.d…  · Web viewman SSH will produce the manual for ... sentences joined by “but.” The following command

Forget OS X’s built-in Grab utility. Snap a picture of whatever’s displaying onscreen and select the image file’s format and name with one Terminal command.If OS X’s default screenshot settings don’t meet your needs, you can assign image formats and file names for screenshots on the fly with screencapture, which lets you take shots of DVD Player (usually forbidden by the Finder at the insistence of the movie industry’s copyright lawyers). To create an instant, noiseless capture, execute screencapture -x -t jpg capture.jpg.Replace jpg with the 3-digit abbreviation for your chosen file format (PDF, TIF, GIF, and PNG are also available) and capture.jpg is the name of the saved file.