Blog Post

Unix Tip: Remember The Tab

terminal In what I’d like to make a weekly series, I’m going to begin sharing Unix (stuff you do in the Terminal, for the uninitiated) tips for those wishing to learn more about it in the New Year. Or just learn more about it because you want to. Either way, welcome!

I’ve been thinking about doing this for a week or so, and it seems like just the right time too, as there’s a good thread that’s started in the TAB Community on just this subject. But since not everyone will make it there, I’ll be sure to capture some tip here from week to week. You know, something for the casual readers. Anyway, let’s get to it.

Since these tips are focused on helping the folks who want to get their feet wet, or are newer to OS X and its Unix underpinnings, I wanted to start with a tip that will significantly enhance your ability to figure things out and dig around in the Command Line Interface (CLI), which is accessed through the Terminal. In order for us to get started, you’ll need to open the Terminal which can be found in your /Applications/Utilities folder.

Obviously with nothing more than a cursor blinking at you, you’re going to have to know commands in order to get around the system in this manner. Using the Tab key will significantly assist you in remembering and correctly typing these commands. You see, as you start to type a command and you aren’t sure of the spelling or syntax, you hit the Tab key and it will help you out.

An example: If you type ‘his’ and hit Tab, your ‘his’ will be completed to read ‘history’ and now you can hit Return and execute the command.
Or say you want to do a delete command and remember that it starts with ‘rm’ (think, remove) but can’t recall if there’s more to the command. Hit Tab, and it will give you a listing of all the commands that start with ‘rm’.
You can also use it when you’re navigating through file structures and don’t feel like typing full names of directories. The command, ‘cd ~/Doc’ and a Tab will turn into ‘cd ~/Documents/’

In the last example there are a couple other little bits that I’ll explain while we’re here. ‘cd’ stands for Change Directory, and the tilde (~) represents your home directory. So the command ‘cd ~/’ signifies that you want to change to a path that originates in your Home directory.

So there you have it. I don’t plan on any particular order of posting these tips, so if I make mention of something trivial that hasn’t been discussed before, I’ll do my best to explain it as well, to avoid creating confusion rather than helping combat it.

As you play in the Terminal, the Tab key will be a good friend, as it will help you discover and remember commands that you’re not yet used to. Before long, it’ll either become committed to muscle-memory, or you’ll pick things up without and and find that you don’t need to lean on it any longer.

14 Responses to “Unix Tip: Remember The Tab”

  1. If your tab is not working, chances are you are using a different shell.

    You are probably using the Bourne Shell – its the default. /bin/sh

    Try using bash, type “bash” and then it should work.
    Bash is just an advanced version of sh.

  2. @Mark 2000

    DOS is nothing but a very stripped down unix.

    “It doesn’t tell you anything about the disk or the number of files your looking at. It makes no distinction between directories and files.”

    Of course it doesn’t tell you about the disk because ‘ls’ for listing the files in a directory. if you wish to know more about the disk itself, you need to run the command to do so. du and df will do that for you. UNIX has a more powerful set of commands which are more specialized than DOS. To get ls to give you more information, you have to tell it to give you more information.

    Want to know the difference between a file and a directory…

    ls -l
    drwx—— 2 user group 68 Jan 6 00:38 dir
    -rw——- 1 user group 0 Jan 6 00:38 file

    First character tells you. d = directory.
    – = regular file

    There are other types of special files, but I won’t go into those.

    You could also do a ls -lF

    drwx—— 2 user group 68 Jan 6 00:38 dir/
    -rw——- 1 user group 0 Jan 6 00:38 file

    You’ll notice the / after the directory name. Executable files not only will they show up with the x’s in the permissions, but they’ll have a * after the file name.

    Why doesn’t ls do this by default? Fewer options asked for, less information. Seems to make sense to me. I sure wouldn’t want to ls -kjhyiuhsdftijh to have it only list the file names like you have to give the dir command in DOS.

    You can also refer to the man page for a larger set of options.

    dir in DOS has similar flags for sorting, for showing/hiding other information. dir in dos won’t tell you permissions, You have to use attrib, etc.

    The best part about this is you don’t have to like or use UNIX. It’s all a matter of preference. Personally, its command set make a lot more sense than DOS ever did.

  3. Gustaf – excellent, thanks for adding that.

    I actually debated whether to make mention of the shell differences, but decided not to in hopes of keeping it a little less daunting. But also figured that those newly on OS X, are most likely running 10.4.* which has been on bash all along. I know it was a different shell before 10.4 but can’t recall which at the moment.

    either way, thanks for making that note in case people do get confused.

  4. First off, this tab auto complete functionality is only for the bash shell. If someone happens to use a different unix shell, the Korn shell for example, tab won’t work the way you expect…

    Nick mentions that tab will list all the commands that start with rm if you type ‘rm’ and hit tab. Similarly, bash also has a “tab tab” functionality for directories and files. Here’s what i mean:
    say i have a folder called “foo”. In this foo folder, I have two other folders called “tests” and “testing”.

    [email protected]~/foo$ ls -l
    drwxr-xr-x 2 gus gus 68 Jan 5 10:08 testing
    drwxr-xr-x 2 gus gus 68 Jan 5 10:08 tests

    At the command prompt, you type “cd test” and hit tab, bash will issue a beep at you because it doesn’t know which folder to use. but if you type “cd test” and hit tab twice, bash will list the possible choices:

    [email protected]~/foo$ cd test #hit tab twice, results in the following line:
    testing tests
    [email protected]~/foo$ cd test

    I hope this makes sense and helps those new to UNIX…

  5. Mark – maybe these tips will help change your mind. I think there’s a lot you’re not acknowledging (or maybe just aren’t aware of) in the unix arsenal. Hopefully we can share a few things that lighten your opinion of it.

  6. I have to say as an old MSDOS man, I can’t stand UNIX. I understand UNIX is the better operating system under the hood, but as far as usability DOS has it beat hands down. UNIX command are far more archaic and far less functional. The “ls” command, for instance. It doesn’t tell you anything about the disk or the number of files your looking at. It makes no distinction between directories and files. And the coup de gras, if you use it in a directory with nothing in it it gives you no feed back, just another prompt as if you did nothing but hit enter. One wonders why, in the 21st century, and more friendly command line hasn’t been created for UNIX, especially since UNIX is so deeply still in use.

  7. Grant –
    Hopefully we’ll have some useful things to show you here. It’s been a couple years since I was knee-deep in Unix on a daily basis. I’m hoping it’ll jog my own memory while helping others get into it, at the same time.

  8. Exceptionally helpful bit of information. Tabbing about will be of great help in getting myself around in terminal. I’m the one who started that thread to get my feet wet and while saving typing by not having to put in full directory names will be of help, using it to complete commands that I am regularly botching will be of great use. Thanks.