8 Replies Latest reply: Dec 14, 2010 10:14 AM by rccharles
J-e-L-L-o Level 1 (25 points)
I have a question. I am learning c++ programming and I want to use terminal to start off with. I was saving my source code on the desktop. I got them up and running thru terminal by cd desktop to choose it.

but now to make it simpler and cleaner i want to put these files in a seperate folder. If my folder is called C++ source code that is on my desktop... what do I type in terminal to use that for compiling my code.


13in 2.4Ghz macbook pro, Mac OS X (10.6.5), 4GB
  • Matt Clifton Level 7 (29,850 points)
    If the folder has spaces, such as "C++ source code", you can either surround the folder with quotes as I just did, eg

    cd "C++ source code"

    or you can escape the spaces, eg

    cd C++\ source\ folder

    Don't forget, too, that you can usually hit the tab key to autocomplete a path in Terminal.

    PS - I would generally advise against using punctuation in folder names, such as "C++", just to avoid incompatibilities and code confusion.

  • J-e-L-L-o Level 1 (25 points)
    this didnt work. here is a code snippet from terminal:

    drwxr-xr-x 5 amwalsh07 staff 170 Dec 12 15:10 Code

    this is the folder name "Code". I tried cd Code- cd "Code"- and cd \Code

    this didnt work. any suggestions?
  • MrHoffman Level 6 (14,757 points)
    The backslash is an escape character for the shell; it allows you to get spaces into the path, for instance. (Windows uses  as a directory separator. Mac OS X and other Unix and Linux systems uses the / for that purpose. There are other differences in file specification syntax between Windows and other platforms, as well.)

    If you have a subdirectory of your login directory named Code, then the path in Terminal is

    cd ~/Code

    That tilde is shorthand for your login directory, and the longer version of that path is usually something like this:

    cd /Users/your-short-name-here/Code

    Here's a longer command sequence, moving first to your login directory, then a relative path into the Code subdirectory:

    cd ~
    cd Code

    Had you had a space in that directory with, say, My Code, then you'd have one of the following:

    cd ~/My Code
    cd /Users/your-short-name-here/My Code

    Since you're planning to do some programming, here is a [file system overview|http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptua l/BPFileSystem/Articles/Domains.html] from the Apple developer documentation. This document describes various concepts and constructs, as well as the directories that should be used for various purposes.
  • J-e-L-L-o Level 1 (25 points)
    well well well...guess what i found out.

    seems as if the name "code" and "source code" won't work in terminal for folder names. cd code didn't work and neither did cd source code. I seen another folder on my desktop so i tried that to see what would happen. and it worked.

    i renamed my folder and it works now. weird.
  • Camelot Level 8 (46,650 points)
    seems as if the name "code" and "source code" won't work in terminal for folder names

    Then you're doing something wrong. There's nothing special about the name 'code'.

    cd code didn't work

    'cd code' would change to the 'code' directory relative to your current location. In other words, if you open Terminal and start in your home directory (e.g /Users/yourname/) then this will change to the 'code' directory in your home directory (e.g. /Users/yourname/code/)

    From your earlier posts it seems that your code directory is on your Desktop, not in your home directory, and therefore cd code would not work (unless you were already in your Desktop.

    So your options are:

    1: cd to your Desktop, then cd to code:

    $ cd Desktop
    $ cd code

    2: include the Desktop path element in your cd statement:

    $ cd Desktop/code

    3: specify a full pathname (with a leading /) to the directory you want to change to:

    $ cd /Users/yourname/Desktop/code

    or other variations on the theme. Understanding the working directory and relative paths is going to be a critical step in your learning process.
  • rccharles Level 5 (7,574 points)
    To see what directory you are in, use the pwd command.

    Here is an overview of the terminal commands. Lets assume that your account has a short user name of mac.
    Macintosh-HD -> Applications -> Utilities -> Terminal
    #What is my short user name? Type the whoami command.
    mac $ whoami
    mac $ 

    #How to list all of your disks.
    # The ls command is for list
    mac $ ls /Volumes/
    Audio CD       Macintosh-HD   Spotless       Tiger-ext
    mac $ 

    # Let's say your flash drive is named Spotless

    # cd is change directory
    mac $ cd /Volumes/Spotless
    # pwd is print working directory
    mac $ pwd
    mac $ 

    # The ls command is for list
    # l is long
    # F is type of file where / is directory
    mac $ ls -lF
    total 134704
    -rw-r--r--     1 mac  staff     64560 Mar  3  2009 A-picture-of-Youpi-key.png
    drwxr-xr-x    83 mac  staff      2822 Nov  7 14:52 Applescript files/
    drwxrwxrwx    12 mac  staff       408 Dec 13  2008 Christmas Cards/
    drwxr-xr-x     9 mac  staff       306 Dec 21 17:39 Christmas Cards 2009/
    ... trimmed ...

    What does all this mean?
    d = directory
    r = read
    w = write
    x = executeable program
     |  |  |
     |  |   all other users not in first two types
     |  |  
     |  group

    # l is long
    # a is all to show hidden files & folders
    mac $ ls -lFa
    total 134736
    drwxr-xr-x    41 mac   staff      1496 Dec 22 17:11 .
    drwxrwxrwt     8 root  admin       272 Dec 24 13:55 ..
    -rwxrwxrwx     1 mac   staff     15364 Dec 23 12:52 .DS_Store*
    drwx------     4 mac   staff       136 Jan 22  2009 .Spotlight-V100
    drwxrwxrwt     5 mac   staff       170 Sep 14 16:36 .TemporaryItems
    d-wx-wx-wx     4 mac   staff       136 Dec 31  1969 .Trashes
    -rw-r--r--     1 mac  staff     64560 Mar  3  2009 A-picture-of-Youpi-key.png
    drwxr-xr-x    83 mac   staff      2822 Nov  7 14:52 Applescript files
    drwxrwxrwx    12 mac   staff       408 Dec 13  2008 Christmas Cards
    drwxr-xr-x     9 mac   staff       306 Dec 21 17:39 Christmas Cards 2009

    ... trimmed ...

    # mv is move or rename
    mv -i the-name the-new-name

    # You can just rename the file back to what it was with mv command.
    mv -i old-name new-name

    Here is what these commands mean:
    cd is change directory
    pwd is a print working directory
    ls is list
    sudo is Super user do
    mv is move or rename

    For cryptic comments, you can always uses the manual command which is man. For example:
    man mv

    # Type the letter q to quit.

    In case you have spaces in your filenames or directories, you need to escape them. See examples:

    mac $ ls -l ~/"see it"
    -rw-r--r-- 1 mac staff 3171 Oct 26 23:38 /Users/mac/see it
    mac $
    mac $ cd /Users/mac/Desktop/ttt\ html\ copy/

    Do you know about tabing? Type in a few letters of a name then press the tab key. The computer will type out the rest of the name if it is unique.

    Press the up arrow(s) key to see the previous command(s).

    history to see many previous commands.

    mac $ history
        2  man launchd.conf
        3  history

  • J-e-L-L-o Level 1 (25 points)
    thank you for the tutorial. For some reason my permissions were off, I didn't think to repair them.

    I knew some of that stuff already. I would type in cd desktop/Code and it just WOULD NOT work. all is working fine and dandy now.

    Thanx all for your help, and even more so for the terminal command hints
  • rccharles Level 5 (7,574 points)
    Good to solve that mystery.