Almost everything we've learned so far has been portable, meaning that it will work in any version of Unix, not just in Mac OS X. Of course, we did discuss a few Mac idiosyncrasies along the way, but the commands and techniques are fundamental Unix. In this chapter, we're going to look at some techniques that will work only on Mac OS X. We'll begin by looking at how we can integrate our work in the Unix environment with our work in the Finder. The simplest of these is how we can take a Finder path and be able to use it in Unix. So let's say we're inside the Finder, we're working, we've got windows open, and we suddenly want to take this folder here and reference it inside Unix for some reason.
Well, all we have to do is grab it, drag it in, and let go. In Mac OS X, we'll then paste the path that Unix needs to reference that same thing. Now I switch to Unix. I'll hit Ctrl+A to go to the front of the line. cd space. Now I've moved into that Sites directory. You can use this for any command that takes a path, whether it's copying or moving or making symbolic links. For anything that uses a path you can simply drag the object from the Finder into the Terminal window and then Mac will give you the path. This is really useful if you need a path to a deeply nested directory.
Instead of typing the seven or eight parts of the path to get there, if it's already open in your Finder window, well then just drag it in there. Save yourself the typing. Now you can also do the reverse, that is, take a Unix path, and use it in the Finder. Mac OS X has a Unix command called open that will open things in the Finder. folders, files, applications, even URLs. So let's change to our Home directory, and from here if we just say open unix_files, then it opens that folder. If we want to open a specific file, we say open unix_files/lorem_ipsum.txt and now it opens up that text file in whatever our default text editor is.
For me, that's TextMate. It may be something else for you. Essentially, it's just like if we double-click on that file or that folder. One technique that comes in really handy is that if you're moving around inside Unix and you get to a certain directory and you really wish you had that directory open in the Finder, well open period. That references the current directory so it pops it up in the Finder. Then anything that you're more comfortable doing inside the Finder, maybe renaming files, throwing things in the trash, all those things can be done in the Finder. And anything you're more comfortable doing in Unix, you can then do in Unix.
You can do the same thing with the parent directory with dot dot. Open doesn't open files and folders. It'll also open the applications. So for example, open -a calculator will open up the calculator. The -a option basically says go to the Applications folder and find a Mac application there with the name that matches. If you want to open a specific file in a specific application, well, then you just say open -a TextEdit and then let's use our lorem_ipsum.txt. Now it opens up inside TextEdit.
One time-saving tip is that you can create aliases for some of these that you use often, so like alias firefox= 'open -a firefox'. Now whenever you type firefox, it opens up Firefox for you. Remember if you want to use these aliases, you'll need to put them in your bashrc file. open will also accept input from standard n if we use the f option. So for example, ls -la can be piped into open with the f option, and then it will open up the contents. Instead of outputting it to the screen here in our Terminal, it will open it up in TextEdit in this case.
This is nice because it allows us to use a lot of the power of Unix to construct exactly what we want and then we can send it to a regular Mac application. So for example, I can find the fields in us_presidents.tsv that I want and I can send them to Numbers or Excel or another program like that. And it will open it up, and there it is, and you can see that it's got them in nice columns for me. Another nice trick is that we can look at the man pages in Unix in preview. So if we type man -t and then whatever we want to search for. The t is just going to say format this as PostScript and then it will pass that off to fa Preview, so that takes a second while it composes it.
And the last thing that open will do that's really useful is open will open URLs for you. Basically, what it is doing is that it says "All right, if what you're sending me is http:// well, then I know it's a URL and I know I need to open it up with whatever your default browser is." So for example, if we want to open lynda.com then it will open up my default browser which is Firefox and open up the lynda.com web site. Again, this is a place where you can create aliases for things that you use often. So if there are web sites that you frequent often, you can make aliases for them or, you could keep a file that has a list of many URLs and you could use xargs to open them all at once for you, or you could grep that file for a subset of URLs and then use xargs on it.
I think you can see how we've learned to combine some of the Unix techniques that we've learned to speed up our workflow in the Finder.
- Moving around the file system
- Creating and reading files
- Copying, moving, renaming, and deleting files and directories
- Creating hard links and symbolic links
- Understanding user identity, file ownership, and sudo
- Setting file permissions with alpha and octal notation
- Changing the PATH variable
- Using the command history
- Directing input and output
- Configuring the Unix working environment
- Searching and replacing using grep and regular expressions
- Manipulating text with tr, sed, and cut
- Integrating with the Finder, Spotlight, and AppleScript
Skill Level Beginner
Q: The exercise files for the following movies appear to be broken:
Is there something wrong with them?
These exercises include one or more "dot files", whose file names start with a period. These files are normally hidden from view by the Finder. So that they would show up in the Finder, the period has been removed from the file names. Additionally, "_example" has been added at the end of the file name to make it clear that the file will not work as-is.
To make the dot files usable, either:
1) Open the file in a text editor to view its contents. Note that it may not be possible to double-click the file to open it because there is no file extension (such as .txt).
2) Resave the file under a new name (usually by choosing File > Save As), adding a "." to the beginning of the file name and removing "_example" from the end.
1) Copy and rename the file from the Unix command line using the techniques discussed in this course. Rename the file by adding a "." to the start and removing "_example" from the end. Include the "-i" option to prevent overwriting an existing file unexpectedly.
Example: cp -i ~/Desktop/Exercise\ Files/Chapter_07/07_02_files/bashrc_example ~/.bashrc
The instructor uses the UNIX program 'units' to convert 72° Fahrenheit to degrees Celsius. The returned value of 40 is incorrect. The correct result should be 22°C. What's the reason for this discrepancy?
The problem is that units does the 5/9 calculation but does not have the ability to subtract 32. So you'll need to subtract (or add) the 32 degrees yourself.