OSX Leopard: Terminal

Discussion in 'macOS' started by WhySoSerious, May 15, 2008.

  1. WhySoSerious macrumors 65816


    Jun 30, 2007
    Dallas, TX
    I'm making an attmept at becoming more tech savy with my new OSX, Leopard. I see, from a lot of posts on these boards, that people use "Terminal" to do some great things or as an alternate work-around on OSX problems.

    1. What exactly is Terminal? Is this the equivalent of opening DOS in windows and running executables?

    2. Is there some sort of tutorial or "dummies 101" to instruct me on the basics of using Terminal?

    3. What are some common/generic scenarios in which I would be able to apply the use of Terminal (example, I saw a post earlier about typing a command to erase something from the trash bin that wouldn't go away normally)?

    4. Are there any other tips/advice you can offer me on using Terminal? Such as..."never use it for this" or "it's a dangerous app to use if you type the wrong thing in it", etc.

    To all who contribute to this thread, Thanks!
  2. MacsRgr8 macrumors 604


    Sep 8, 2002
    The Netherlands
    1. Well, yeah a bit. I usually describe the Terminal as a "window to Darwin", i.e. an app that will take you to the CLI based OS of Mac OS X (=Darwin, quite similar to the Linux CLI), while running Aqua.

    2. Maybe this is a start.

    3. Sometimes the Finder or some other GUI command is either not available, or prevents you of doing something "to protect you". The example of really erasing a file, is a good one. If you delete something using the Terminal (CLI), it immediately is removed, and not sent to Trash first, where the user can recover...

    4. Get used to it first. Learn to navigate, then learn a few simple commands, then try stuff out. It is very dangerous if you're not familiar with it. But, if you have learnt your way around, you'll also find out that it is very powerful!
  3. WhySoSerious thread starter macrumors 65816


    Jun 30, 2007
    Dallas, TX
  4. wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    One extremely powerful (and dangerous, if you don't know what you're doing) feature of the Terminal is the ability to run so-called "shell scripts". These are executable text files, using a specific format and header, that can be used for, among other things, automating tedious tasks that can't easily be automated with Automator or AppleScript.

    Here's a very simple example:
    # findband.command - written by wrldwzrd89
    # change to iTunes directory
    cd ~/Music/iTunes/
    # find the word "band" in the iTunes XML file, save all occurrences to bands.txt
    cat iTunes\ Library.xml | grep band > ~/Desktop/bands.txt
    What this little script does is this: It searches your iTunes Library XML file for the word "band", and outputs all lines containing this word to a file on your desktop called bands.txt.

    The lines starting with # are comments, and the very first line tells the Terminal which shell to use to execute this little script.
  5. killerwhack macrumors regular


    Aug 5, 2004
    Los Angeles, California
    Terminal - choice of shell

    When you run terminal, the default shell is specified in your user profile. The Mac OS seems to favor the bourne again shell (bash). The bourne shell was the first shell program (named after the programmer who created it) and the c-shell is a refinement with many enhancements. There is also the korn shell.

    Personally, since I cut my unix teeth on suns back in the late eighties, I am a c-shell enthusiast.

    You need to google these names to study the differences and decide which one you want to use.

    In shell scripting, the sharp symbol (#) is a comment indicator.

    The first line of the script tells the OS which shell to use for that script.
  6. wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    The default shell in Mac OS X was tcsh until Tiger (or was it Panther, I'm not sure), when the default changed to bash.
  7. crackpip macrumors regular

    Jul 23, 2002
    Terminal (or any other terminal program such as iTerm) exposes the Unix underpinnings of Mac OS X. It enables you to do a number of things that are not accessible via the GUI. For example, it seems the Disk Utility program is actually a GUI wrapping the 'hdiutil' command. There are a great deal of things that you can do with hdiutil in the terminal that are not available in Disk Utility.

    It also enables access to many of the projects developed for BSD and especially Linux. The MacPorts project and Fink maintain a repository of osx compatible ports of many of these software packages, and make it easy to install and update these packages.

    I would recommend using the default shell, bash. When starting out, the different shells don't really matter much, but I think there are a lot of good features when you become more advanced. There are a number of tutorials on the web for using bash.

    Once you've gone through the tutorials a bit, I recommend two types of books-- a book on bash such as the O'Reilley book which provides details on more advanced features, and a book on shell scripting (I used the SAMS book "Teach Yourself Shell Scripting in 24 Hours"). It's good to have a couple books for reference, especially when you want to accomplish something that requires features you haven't used before.

    There are a few things, like getting rid of files that are stuck in the trash, where it can be useful. Also anything you learn how to do, can also be done on a remote machine without having to setup something like VNC. This can come in handy when a process freezes that impacts the interface. You can log in remotely from a different computer and kill the problem process. The most significant benefits, however, come from the vast array of utilities and programs now available and the ability to create scripts.

    When I was writing my dissertation, I had a script that would execute twice a day. It would check to see if any of the files had changed in my dissertation, and if so, would create a compressed archive and upload it to an offsite server for backup.

    Install the developer tools and X11, if you haven't already. They are necessary when using MacPorts or fink.

    After going through a basic tutorial, think of something that you want to be able to do, even if it can be accomplished with the GUI, like my backup example. It is nice to have a clear task when you are learning. Once you accomplish it, pick another.

    Eventually, you will want to take advantage of the shortcut features (e.g. aliases and functions). Don't alias destructive commands to themselves. Here's an example: The `rm` command deletes files. If you type `rm -i <someFile>` it gives you asks you for confirmation before deleting a file. A bad habit is to make an alias so that when you type `rm` it actually executes `rm -i`. The reason is that you will get so used to it, that when you are in a different setting, like a different account or a different machine, you will forget that it isn't setup that way, and accidentally delete things you don't intend. A better way would be to alias `rm -i` to `rmi`.

    Another tip involves the use of 'globbing' with a destructive command. It's a great thing, but you have to be careful. Let's say I have five files, (file1, file2, ...). I can use a command like `rm file*` to delete all of those files. However, * can represent anything, so if I also have files, fileBlue,fileRed..., it will delete them too. If I'm working with important files or with complicated globs, I'll often test a glob using a command like `du` which displays the size of a file. If a file shows up that I don't want to delete, I'll refine the glob.

    Good luck,

Share This Page