A Guide to the Terminal, bash, and the UNIX side of Mac OS X

Discussion in 'macOS' started by wrldwzrd89, Nov 25, 2009.

  1. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #1
    The Terminal represents your gateway to the UNIX back-end of Mac OS X. It is normally located in the Utilities folder, inside the Applications folder. WARNING: chmod, mv, and rm can do bad things to your system if misused, including making Mac OS X no longer boot. Make sure you know what you're doing BEFORE you do it, since there isn't an undo.

    First, a guide to the most frequently needed commands:
    cd: change directory
    This command takes one argument, the directory to change to. You can also drag the folder name from the Finder to the Terminal window, which makes changing the working directory a lot simpler.
    pwd: print working directory
    This command takes no arguments. It simply displays the current working directory. Useful for finding out exactly where you are.
    mv: move
    This command takes 2 arguments: The file to be moved, and the new name for this file. Note that this command is also used to rename files.
    man: manual
    This command takes one argument: The name of the command to get manual information about. Note that the manual page viewer is operated with the keyboard, and not the mouse. Use the spacebar to advance one page of text at a time.
    rm: remove
    This command takes a variable number of arguments: A list of files/directories to remove. Note that there is no Undo if you delete something this way.
    chmod: change mode
    This command is a bit more complex than the others, but it's real handy for changing permissions. I highly recommend reading its manual page to learn how it works.
    ls: list directory contents
    This command can either be used with no arguments, to list the contents of the current directory, or with one argument, to list the contents of a directory other than the current one. It supports some options, such as -a (show hidden files) and -l (long list format). These two options can be combined, as in: ls -al

    Navigating the UNIX folder structure

    Mac OS X uses a UNIX-like directory scheme for most of its inner workings. If you've used Linux before, you'll learn the Mac way quickly. The bin folder is where most normal commands go. The sbin folder contains commands that generally require elevated permissions to use. There are also other folders, like etc (for systemwide settings), var (for various things), and tmp (where temporary files live). If you use the GUI, you never need to worry about these things - they work behind the scenes. All these folders live inside /, the root of the file system (usually corresponding to Macintosh HD in the GUI).

    Other useful commands

    zip: compress files
    This one's real handy. Ever wanted to make an archive of a folder, but didn't want those ugly __MACOSX folders showing up in your archives? This tool automatically strips those. Here's a simple guide to its use:
    zip -r filename.zip * - compress everything in the current working folder, including subfolders
    zip -d filename.zip *.DS_Store - remove those unwanted .DS_Store files from your archives
     
  2. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #2
    Anyone who doesn't know how to use Terminal shouldn't mess around with mv, chmod, and especially rm. Thanks for the random info though... maybe someone will find it useful.
     
  3. wrldwzrd89 thread starter macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #3
    While I completely agree with you, someone asked me to create this guide. I plan on adding more to it, so it won't be entirely useless.
     
  4. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
  5. dbwie macrumors 6502

    Joined:
    Jun 11, 2007
    Location:
    Albuquerque, NM, USA
    #5
    There are some good books out there on the UNIX side of Mac OS X.
     
  6. ViViDboarder macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #7
    Haha, yea... ls is pretty important. I recently was doing some DOS batch programming and I kept trying to use ls... dir is too many characters! :D
     
  7. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #8
    Why not just link whoever wants to know this information to a helpful page like this?
     
  8. wrldwzrd89 thread starter macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #9
    That's a really good idea, actually. I didn't think of adding links to other UNIX references. However, there is some stuff that UNIX references don't cover:

    open: open applications
    This command is most typically used to start GUI apps, and needs the -a switch to really be useful for this. For instance:
    Code:
    open -a TextEdit
    ...launches TextEdit.

    say: speak text using Mac OS X's Text to Speech engine
    This command takes the text to say as its arguments. For example, if the default voice is set to Alex:
    Code:
    say Hello, world!
    ... will speak that text using the Alex voice.

    osascript: run Open Scripting Architecture-compliant scripts
    This command is most frequently used to execute AppleScript scripts from the command line.
     
  9. HLdan macrumors 603

    HLdan

    Joined:
    Aug 22, 2007
    #10
    While I do commend the OP for offering Terminal tips, it might be better to have a sticky up top for "Terminal tips". Also it's not a good idea to post terminal hacks that might mess up things on the computer. Yes, I see the warning but what's the point of posting dangerous terminal hacks? Not sure who would actually use the Terminal and type in a command to launch GUI apps when a simple launch of the app from the Dock will this or from Spotlight.
     
  10. wrldwzrd89 thread starter macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #11
    You make a valid point, which I agree with. The use of these commands is primarily not for typing directly at the prompt, but to be used as part of a larger shell script. Shell scripts are where the true power of UNIX lies, at least in my opinion... I've written several shell scripts to perform monotonous tasks, such as fixing up the permissions on my user files, ad blocking via the HOSTS file, and even mass-downloading bits of web sites.
     
  11. ViViDboarder macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #12
    What here is a dangerous terminal hack?

    It's all stuff you can do with the mouse. No more dangerous.

    Also, with a mouse you can't easily automate all this stuff (Automator can... to a point). With the terminal you can put everything in a .sh script and it'll open any of your apps, move/rename/delete/copy any files, connect to any servers, mount any drive, upload any data, run remote commands on other computers, etc...

    Lots of reasons to use terminal.
     
  12. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #13
    The only people that would use it are the people that don't need this guide :p
     
  13. maflynn Moderator

    maflynn

    Staff Member

    Joined:
    May 3, 2009
    Location:
    Boston
    #14
    Personally those folks who want to know how to use the terminal will be buying books, having a guide especially with rm, mv, chown included increases the risk that a user screwing up his system.

    There's no way you can have a guide on a forum that will even scratch the surface of the capabilities.

    Take, grep, tail, cat, tar, rsync, cp, top, sudo and su

    All of which are very powerful and useful commands/utilities. I didn't even see the cp command in the list and that's probably one of the most basic commands that people will be using.

    Providing an incomplete list of some commands can be more dangerous and risky then providing everything and providing in detail what each does.

    There's plenty of books that go into such detail and for those people who want to know about this stuff, they will (or already have) purchase those books.
     
  14. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #15
    Good caveats. Another really handy command is cd .. that moves you up a directory level.

    alias name of alias='command' can be handy, too. Those should be placed in .bash_profile so they are always available. I like to alias ll to mean ls -lt, because that's the generally the most useful of the ls commands for me, as well as la to mean ls -la.
     
  15. wrldwzrd89 thread starter macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #16
    Thank you, maflynn, for your feedback! This guide was never meant to be exhaustive, nor was it meant to cover things in great detail. The reason I created it was to give those who want a primer on what can be done at the command line a small taste of its power. I totally understand that some of the commands I included are dangerous... but that's because I use those all the time, and know how to use them correctly.
     
  16. HLdan macrumors 603

    HLdan

    Joined:
    Aug 22, 2007
    #17
    Okay, I did you use the wrong terminology. Not "dangerous", but the OP posted a warning about performing these tasks as you can mess things up, especially if you don't know what you are doing. If I had a nickel for every time someone took a risk with their Mac on here, not checking out the consequences first and then come here and create a panic thread that they screwed up their system, I would be about as rich as Steve Jobs. :D. People just ignore warnings and take the chances.

    Okay, I now understand about the repetitive tasks and the OP mentioned, that's actually a good idea. I don't know a lot about the Terminal, I use it for very basic tasks.
     
  17. Vikz macrumors regular

    Vikz

    Joined:
    Nov 13, 2007
    Location:
    San Juan, Puerto Rico
    #18
    Also you may want to try the command top instead of activity monitor when trying to isolate OS usage problems :apple:
     
  18. sidewinder macrumors 68020

    sidewinder

    Joined:
    Dec 10, 2008
    Location:
    Northern California
    #19
    Nobody uses zip any more. Replace this with gzip if you want to be "modern".

    S-
     
  19. sidewinder macrumors 68020

    sidewinder

    Joined:
    Dec 10, 2008
    Location:
    Northern California
  20. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #21
    The guides I've read here at MR have started a lot like this. Someone takes a stab, others chime in with suggestions. Calling it incomplete 59 minutes after the first post might not mean you deserve a timeout, but it should get you a stinkeye.

    Another thread delved into bash login files, which is about where my Terminal knowledge ends. I had suggested MR have a guide on the Terminal since MR is where a lot of people turn for information like that. My thanks to wrldwzrd89 for taking the first step.

    I would also suggest that maflynn provide us an entry, possibly on grep, tail, cat, tar, rsync, cp, top, sudo or su. I'm sure lots of folks here would benefit from something on grep. Man pages are far too terse to offer any sort of comprehension.

    mt
     

Share This Page