Archive for the 'Mac' Category

Pipes

If you're new here, you may want to subscribe to my RSS feed.

I mentioned previously that Unix uses lots of text files, which are easy to create and manipulate. Unix has a variety of commands to manipulate text files. The way to move text between these commands is with pipes. The pipe symbol is the vertical bar (|) above your Enter key.

For example, suppose you have a text file full of email addresses, with one email address per line. Suppose it’s named “emails.txt” and it’s on your Desktop.

To view the emails, type the following command in the Terminal:
cat ~/Desktop/emails.txt

To view the emails sorted alphabetically, type the following:
cat ~/Desktop/emails.txt | sort

The pipe causes the output of the cat command to be fed into the sort command, then outputted to the screen. You can tack on as many pipe commands as you need.

To view the emails sorted alphabetically and with a prompt between each page, type:
cat ~/Desktop/emails.txt | sort | more

The list of emails “flows” from the cat command to the sort command to the more command, as if going through water pipes.

Viewing text files: cat, head, tail, more, less

Many Unix files are text files. This may seem simplistic, but it’s actually very powerful. Text files are easy to create, read, and manipulate. Many Unix commands are for working with text files.

The first category of Unix commands for working with text files might be called “text viewers”. These includes commands such as cat, head, tail, more, and less. All of these commands are used by typing the command plus the name of a file.

cat — Short for “concatenate”, this command is used to temporarily combine files. But if you use it on only one file, it just displays the file to the screen.

head — This command shows the first 10 lines of a file. (You can adjust the number of lines using the flag -n.)

tail — This command shows the last 10 lines of a file. (You can also use -n here to show more lines.)

more — Like cat, this shows the entire contents of a file, but prompts you for “more?” between each page. Hit the space bar to go to the next page, or use the up and down arrows. Hit “q” to quit.

less — This is another “pager” like more, i.e. it shows one page at a time. It was meant to be a successor or alternative to more and was hence given the opposite name less. It had more features than more (sounds like an Abbott and Costello routine) when it first came out, but now the two commands are about the same.

Here are some examples:

How to view your hosts file, where you can map host names to IP addresses:
cat /etc/hosts

If you have Personal Web Sharing turned on, how to view the last 10 lines of the Apache log file:
tail /var/log/httpd/access_log

How to read the Apache configuration file, one page at a time:
more /etc/httpd/httpd.conf

Tab completion, part 2

In the last post we discussed how tab completion makes typing Unix commands much easier. The downside, as Janssen pointed out, is that tab completion is case-sensitive, so typing cd doc and hitting tab won’t complete cd Documents/. But there’s a way to change this.

In your home folder you’ll find a file called .profile that contains settings for working in the bash shell (which is what you do every time you use Terminal.) It’s sort of the “preferences” file for the command line. The .profile file can do a lot of things, one of which is making tab completion case-insensitive. (We’ll cover more uses of .profile later.)

Because .profile begins with a dot, it’s invisible to most Mac OS X applications. A notable exception is Smultron, a free text editor that has an “Open Hidden” option. You can either use Smultron to open .profile in your home folder, or open Terminal and type open .profile, which should open the file in Apple’s TextEdit. If you don’t have this file, you’ll just create a new empty file by the same name and save it.

The line you’ll add to .profile is the following:

bind "set completion-ignore-case on"

If your .profile contains other text, simply put this at the end on its own line. If you’re creating .profile from scratch, this is the only line you’ll need. Save and close .profile.

After closing and reopening Terminal, your tab completion should now be case-insensitive. If you type cd doc and hit tab, it should fill in cd Documents/.

Tab completion

Tab sodaAs Nathan Sweeney mentioned in the comments of the last post, there is actually a faster way of “typing” Unix commands: the tab key. Hitting the tab key causes Unix to fill in as much of whatever you’re typing as possible. For example, if you open Terminal (you’ll begin in your Home folder) and want to change to your Documents folder, you’d normally type cd Documents. But it’s much easier with tab completion: type cd Doc and hit tab. You’ll notice that Unix fills in the rest of the word “Documents”.

You’ll also notice that tab completion only works when whatever you’re typing is unique. (Or rather, it only works to the degree that it is unique.) For example, if you type cd D from your home directory and hit tab, Unix won’t fill in anything because it doesn’t know whether you want cd Desktop or cd Documents. But if you hit tab a 2nd time, it will display Desktop and Documents to show you your options. Hitting tab once always causes Unix to fill in as much as it can. Hitting tab the 2nd time always shows you your options, if any.

You can even use tab completion to select a command. At the beginning of a command prompt, hitting tab twice will cause Unix to display ALL the commands that are available to you. There will probably be so many that Unix will ask you if you really want to see them all. Hit “y” to confirm, and then hit “q” (quit) when you’re done.

Tab completion makes it much easier to work with Unix.

Navigating files and folders

Each time you open Terminal, you begin in your Home folder. To confirm this, type pwd and press Enter. The command pwd tells you where you are (i.e. your “Present Working Directory”). Any commands you type there will by default occur in this folder (unless you refer to some other directory explicitly.)

After opening Terminal and typing pwd on my computer, it returned /Users/richard.

To navigate to a new folder, use the cd (”change directory”) command. For example, to switch to the Applications folder, type cd /Applications. You can then type ls to list all of your Applications.

To move deeper into the hierarchy, use cd and the name of the folder. For example, if I’m in /Users/richard, I can type cd Movies to move into the Movies folder. I’ll then be in /Users/richard/Movies.

To move back a folder, use two dots (..). For example, if I’m in /Users/richard and type cd .. I will then be in /Users.

Directories, Files, and Path Names

In Unix, folders are called directories. Files are still files. The location of a file is called its path. The very top folder (or bottom folder depending on how you think about it) is called the root directory. This root directory is the one you see when you double click your hard drive. It contains the Applications, Library, System, and Users folders.

Here is how you refer to various parts of the file system in Unix:

  • The forward slash (/) refers to the root folder.
  • A path name beginning with a slash (/) is an absolute path. For example, the path /Applications refers to the Applications folder, no matter where you currently are. To see a list of Applications, type ls /Applications. It doesn’t matter where you currently are because it’s an absolute path.
  • A path name beginning without a slash is a relative path. For example, if you are currently in your Home folder, the path Documents refers to the Documents folder under your Home folder. While in your Home folder, type ls Documents to see a list of your documents. If you were to type ls Applications (without the slash), you’d likely see an error since you (probably) don’t have an Application folder inside your Home folder, and the path is relative to where you currently are.
  • The tilde (~) refers to your Home folder. (It’s also an absolute reference, despite not having a preceding slash.) To see what’s in your Home folder, type ls ~. To see your own Documents type ls ~/Documents. It’s an absolute reference that works from anywhere. For me, the path /Users/richard/Documents is the same as ~/Documents.
  • You can use the tilde with someone else’s username to refer to their Home folder. For example, to see what’s in Mary’s Home folder, type ls ~mary or to see what’s in John’s Documents folder, type ls ~john/Documents.

You should now be able to refer to any path on your hard drive. Did I leave anything out?

How to get help with Unix commands: the “man” command

When discussing what a Unix command is, I mentioned that you can alter the function of a Unix command by using flags or arguments. For example, to list all the files in your current folder, you use the ls command. To reverse its order you add the “r” argument: ls -r. But how do you know which arguments to use?

Unix has a built-in manual called the man system. Each Unix command comes with a manual page which you can access by typing man and then the name of the command. For example, to get help with the ls command, open the Terminal and type man ls.

Each manual page shows the name, synopsis, description, examples, and more for that command. You can use the up and down arrows to scroll through the entry. To skip Forward a page hit “f”; to skip Back a page hit “b”. When you’re done reading the manual page, his “q” to quit.

The man command is one of the most important commands for learning and using Unix. I use it daily.

On a side note, these manual pages are also available on the Internet, including at Apple.com: Mac OS X Man Pages

Back to business

My two weeks in Brazil were awesome but I am also glad to be back. If you’re joining us for the first time, this site is all about using Unix (Terminal) on your Mac. If you need some background, start here:

Two week hiatus

I’m very excited for FreeMacUnix.com. I’m looking forward to having an outlet for discussing how Unix can make every Mac user more productive. I sincerely enjoy this.

Unfortunately for my baby, I’m taking a two week trip to Brazil. I’ll have only occasional access to email (I’m not taking my Powerbook) so I won’t be able to post on FMU until I return.

The first posts I’ve written have given some necessary background. Please join me at the beginning of October for more details on how Unix can make you a better Mac user. See you then.

What is a Unix command?

A Unix command is like an application, but it usually performs a much simpler task. There are Unix commands to show the contents of a file, to sort, to filter, and even to browse the Internet. Every Apple computer comes with hundreds of Unix commands that each do something unique. Contrast that with the dozens of applications that come with every Mac, some of which perform very complicated workflows.

Mac applications have Preferences for selecting how you’d like them to run. Unix commands, on the other hand, don’t (usually*) have any preferences or persistent settings. To specify how a Unix command runs, you use what are called flags or arguments. These flags or arguments are specified each time you run the command.

Arguments are specified with one dash and a letter (e.g. -a -b -c), or two dashes and a word (--help.) Sometimes certain settings or parameters can follow an argument (-u "stevejobs" or --user "stevejobs".) Whenever you use multiple single-letter parameters, you can combine them (-abc is the same as -a -b -c.)

Here’s an example. The ls command lists the files in a folder. Open Terminal, type ls, and hit Enter. You’ll see a list of the files and folders in your Home folder. Each name is listed one after another.

If, instead, you’d like to see all the files listed in columns, type ls -l and his Enter. You’ll see all the files listed in one column. It will also show the dates the files were created, and other information. This is the “long” format, as specified by the l flag.

You’ll notice that the list of files is in alphabetical order. If you want to reverse it, you can use the r flag. Type ls -l -r. The files will be listed in reverse alphabetical order. Since you can combine single letter flags, ls -lr will produce the same result. Most of the time the order of the flags doesn’t matter either, so ls -rl also produces the same result.

the ls command