Skip to content

Latest commit

 

History

History
318 lines (206 loc) · 8.72 KB

02_navigate.md

File metadata and controls

318 lines (206 loc) · 8.72 KB

Navigating

  1. When you log in, you start out in your home directory, the same one you get in the Finder when you choose Go ⇒ Home. (Directories are often called folders when they appear in a graphic interface.) This command lists files and subdirectories, which are folders nested within other folders. It's very common to ls after you navigate to a directory, to see its contents. Type in the ls part, then a carriage return:

    $ ls
    

    The $ that appears above is just a common convention to show when you're typing commands in a shell. (The bash shell displays a $ prompt, while the alternative C shell uses a % prompt.)

    • shell
    • utility / command
    • home directory
    • subdirectory
    • directory / folder
  2. This command creates an empty file, or updates an existing file:

    $ touch harry.txt
    

    Run ls again to see it:

    $ ls
    

    Avoid any whitespace characters and most special non-alphanumeric characters in filenames. Use underscore characters to visually separate word components:

    $ touch harry_stiles.txt
    

    The touch is the command, and the rest is called the argument. Here's how to create several files at once with several arguments:

    $ touch harry.txt liam.txt zayn.txt niall.txt louis.txt
    

    Lowercase is optional, but a common file-naming convention. The filename extension suffix is optional, but often gives hints for which application is appropriate to open it, in this case TextEdit. (The Mac Finder may be configured to hide the extension from its listings.)

    • file
    • whitespace
    • alphanumeric
    • command
    • argument
    • extension
  3. Most Unix systems such as Linux are case-sensitive, but the Mac overrides this. Running this command only produces a single file:

    $ touch harry.txt HARRY.txt Harry.txt
    $ ls
    
    • case-sensitive filenames
  4. List a subset of files, in this case all ending with .txt:

    $ ls *.txt
    

    The * is a special wildcard that translates to one or more character. This lists any text file starting with lowercase l:

    $ ls l*.txt
    

    ...or any file starting with l:

    $ ls l*
    
    • wildcards
  5. Not as common, the ? wildcard matches a single character. This lists text files that have single-character filenames except for the extension:

    $ ls ?.txt
    
  6. Specify alternate characters. This is a character class matching any text file starting with h or z:

    $ ls [hz]*.txt
    

    Adding ^ at the start reverses the set, so this means any text file not starting with those characters:

    $ ls [^hz]*.txt
    
    • character class
  7. Specify ranges. This yields any text file starting from n to z:

    $ ls [n-z]*.txt
    

    Here's how to specify more than one range, in this case any file starting with alphanumeric, or an underscore or dash. (The dash is considered literally when it appears at the end.)

    $ ls [a-zA-Z0-9_-]*
    
    • character range
  8. Tab completion can save you a lot of time. Type this, but without immediately following it with a carriage return:

    $ ls l
    

    Now type a TAB character. You see liam.txt and louis.txt listed. Then add an o:

    $ ls lo
    

    Type another TAB and it finishes the filename for you:

    $ ls louie.txt
    

    If you're in a directory that only contains a single item, you can type ls, then a TAB to get the only possible completion.

    • tab completion
  9. List a directory's contents:

    $ ls Documents
    

    (Directories named with Initial Cap are specially built in for the Mac.) This lists all files and subdirectories in separate batches:

    $ ls *
    
  10. Modify a command with an option: If commands are verbs and the arguments are nouns, think of the options as adverbs. This shows all files, including hidden files in the directory whose filenames start with a dot:

    $ ls -a
    

    Notice the . and .. listings.

    • command options
    • hidden files
  11. This shows a long listing of inscrutable (for now) details about the file:

    $ ls -l
    

    You can often combine arguments using this shorthand:

    $ ls -al
    

    Otherwise you can always express them separately:

    $ ls -a -l
    
  12. Show your current working directory, where YOURLOGIN stands for the short name you use to log into your Mac:

    $ pwd
    /Users/YOURLOGIN
    

    This is called an absolute path. It tells you how to navigate to where you are from the file system's root (the first /) through each subdirectory. Subsequent slash characters are known as path delimiters.

    • root
    • absolute path
    • working directory (current)
    • path delimiter
  13. Go to the upper parent directory:

    $ cd ..
    $ pwd
    /Users
    

    Then go back down:

    $ cd YOURLOGIN
    

    Whenever you specify paths that don't start with a slash, they're called relative paths. In every full directory listing, .. refers to the parent directory, and . refers to the current directory. You could have also expressed that last command this way:

    $ cd ./YOURLOGIN
    
    • relative path
  14. Go to the root, or the top of the directory tree as it's oddly referred to:

    $ cd /
    $ ls
    

    Now use a tilde to view files in your home directory:

    $ ls ~/harry.txt
    

    Your Mac probably isn't set up with more than one user, but if it were, you could use this to distinguish whose home directory:

    $ ls ~SOMELOGIN/harry.txt
    
    • user
    • directory tree
  15. Go up and over, but first go home, the cd command's default behavior:

    $ cd
    

    This goes up one directory and down one, in this case to the same directory, but you could go to other directories as well:

    $ cd ../YOURLOGIN
    

    This one goes up two, then down again:

    $ cd ../../Users/YOURLOGIN
    
    • default behavior
  16. On the Mac, the open command opens folders in the Finder:

    $ open .
    $ open ~/Desktop
    $ open /
    
  17. You can open other kinds of files in various Mac applications. This creates an empty HTML file and opens it with your default browser:

    $ touch file.html
    $ open file.html
    
  18. You can join separate commands on the same line with semicolons, in which case they're often referred to as statements:

    $ touch file.html; open file.html
    

    Now pretend for a moment that the touch command is something complicated that may produce the file, but may also fail for some reason. This syntax opens it only if the first command succeeds:

    $ touch file.html && open file.html
    

    This variation runs the second command only if the first fails:

    $ touch file.html || open error.html
    

    Think of these as and/or statements. You can use them as handy one-line variations on if/then and unless/then conditional logic. (If that sounds far-fetched, consider these two sentences: Be nice to me, and I will be your friend. Be nice to me, or I will punch you in the nose.)

    • statement
    • conditional
  19. Open anything with anything. Suppose you have an HTML file, but you want to edit the raw source with your default text editor:

    $ open -e file.html
    

    The -a option allows you to choose any application, which you can use autocomplete to access. This is like dragging a file onto an app:

    $ open -a /Applications/Firefox.app/ file.html
    

    Notice that the -a option requires its own argument, in this case a path to an application. Some options are standalone, and some aren't.

    • application
  20. Learn more about commands. Some commands have so many hard-to-remember options that they appear in this longer double-dash format:

    $ command --spelled-out argument
    

    Some commands have a special option to tell you about all the other options available:

    $ command -h
    $ command --help
    

    Otherwise the man command may show you embedded manual-page documentation.

    $ man open
    

    The documentation displays in a paging mode. Type a space character to go to the next screen, or type q to quit out of it.

    The man documentation is often very detailed and obscure, so don't worry if it's difficult to read. It's mostly a great way to learn about useful options, such as this one which simply opens the containing folder in the Finder and selects the file as if you had clicked on it:

    $ open -R file.html
    
    • manual page
    • paging