Bash

  1. File protection(position and meaning): 
    • 1: File type, -(plain file), d(directory), l(symbolic link), p(named pipe), c(character device), b(block device)
    • 2-4: Owner permissions, read write, and execute
    • 5-7: Group permissions
    • 8-10: World permissions
  2. To see the ownership and permissions of a directory:
    $ ls -ld dirname
    // drwxr-x--- 3 smith staff    4096 Jan 08 15:02 dirname
  3. Wildcard: There are two characters that wildcards cannot match: a lead- ing period, and the directory slash (/). Every command, regardless of its origin, works with wildcards and other shell features:
    Wildcard Meaning
    * 0 or more consecutive character
    ? 1 character
    [set] a set, [aeiouAEIOU]; [A-Z]
    [^set] not in a set, [^A-Z]
    [!set] ! equals ^

    When using character sets, if you want to include a literal dash in the set, put it first or last. To include a literal closing square bracket in the set, put it first. To include a ^ or ! symbol liter- ally, don’t put it first. 

  4. Brace expansion: 
    echo sand{X,YY,ZZZ}wich 
    // matches: sandXwich sandYYwich sandZZZwich
  5. Variable:
    $ MYVAR=3
    $ echo $MYVAR // 3
    $ printenv // print all variables and their values
    $ printenv HOME // one variable and its value
    $ echo $HOME // dito
    // by default, variables are local(known to shell), to make it available to other program:
    $ export MYVAR_2=3;
    // now the variable MYVAR_2 is environment variable, if you want make it available to a specific program only once, prepend variable=value to command line:
    $
    printenv HOME // /Users/smith
    $ HOME=/Users/sally printenv HOME // /Users/sally
    $ printenv HOME // /Users/smith

    shell variable:

    Variable Meaning
    DISPLAY the name of your display for opening X Windwo
    HOME Your home directory
    LOGNAME login name
    MAIL incoming mailbox
    OLDPWD shell's previous directory, prior to the last cd command
    PATH shell search path:directories separated by colons
    PWD current directory
    SHELL the path to your shell
    TERM the type of your terminal
    USER login name
  6. Alias: defines a shorthand for long command:
    $ alias ll='ls -l'  // use ll for ls -l
    $ unalias ll  // remove alias
    // define alias in .profile file to make it available every time you open a shell
    // list all alias:
    $ alias
    #=> ll='ls -l'
  7. Input/output redirection
    // read from a file
    $ some_command < in_file
    // overwrite or create a file
    $ some_command > out_file
    // append to out_file
    $ some_command >> out_file
    // A command that writes to standard error can have its output redirected to a file as well, using the 2> operator:
    $ some command 2> error_file
    // To redirect both standard output and standard error to files, you can supply both > and 2> to redirect them to separate files, or >& to redirect them both to the same file:
    $ some command > outfile 2> errorfile  // seperate file
    $ some command >& outfile  // same file
     
  8. Pipes: redirect the standard output of one command to be the standard input of another(|):
    $ ls -1 | wc -l  // they are one and L
  9. Combining commands: 
    $ command_1; command_2; command3 // as if run in separate shell
    $ command_1 && command_2 && command_3 // the sequence will stop if any command fails
    $ command_1 || command_2 || command_3 // the sequence will stop as soon as one commands succeeds
  10. Quotes: '', "", ``:
    // if a file name contains whitespaces you need to quote
    $ wc "My File" 
    // single quote treat their contents literally
    $ echo 'The variable HOME has value $HOME'
    // The variable HOME has value $HOME
    // double quotes let shell constructs be evaluated:
    $ echo "The variable HOME has value $HOME"
    // The variable HOME has value /Users/smith
    // backquotes cause their contents evaluated as shell command, the contents are then replaced by the standard output of the command
    $ whoami // smith
    $ echo My name is `whoami` // My name is smith
  11. Escaping: used to display character literally: such as \*, \$HOME
  12. Emacs input style:
    ^W: delete backward a word
    ^D: delete next character
    ^U: erase everything from your cursor back to the shell prompt
    1. Command history:
      Command Meaning
      history Print your history
      history N Print the most recent N commands in your history
      history -c Clear your history
      !! Re-run previous command
      !N Re-run command number N in your history
      !-N Re-run command you typed N commands ago
      !$ Represnents the last parameter from the previous command; great for checking that files are present before removing them
      !* Represents all parameters from the previous command
  13. Shell Job Control: 
    jobs List your jobs
    & run your job in the background
    ^Z suspend the currend(foreground job)
    suspend suspend the shell
    fg [% + job number] unsuspend a job: bring it to the foreground
    bg [% + job number] make a suspend job run in the background
      • a job is simply the shell's unit of job
      • Jobs are at a higher level than OS X processes
      • jobs: built-in command jobs lists the jobs running in your current shell
    &
        : placed at the end of a command line, causes the given command to run as a background job:
        • $ emacs myfile &
    ^Z
      : suspend the currend job, but the state is remembered, (^C: killing a current command)
      • $ sleep 20
      • ^Z
      • [1]+ Stopped        sleep 20
    • bg [%jobnumber]: built-in command, sends a suspend job to run in the background, with no arguments, bg operates on most current job
      • bg %2
    • fg: bring a suspend or background job into the foreground
    • suspend: built-in, suspend the current shell
  14. ^J: same as Enter key, but when Enter does not response, it maybe. Type reset and press ^J, will bring your shell back.
  15. ^D: terminate a shell, as exit, Control-D sends an “end of file” signal to any program reading from standard input 
  16. ls: listing command
    • ls -l@: Add the -@ option to -l to display OS X extended attributes of the files in question:
      ls -l@ letter.docx
        -rw-r--r--@ 1 smith users 49269 Nov 19 2011 letter.docx
          com.apple.FinderInfo 32
    • The -d option lists information about a directory itself, rather than descending into the directory to list its files
    • Useful options:
      -a
      -l (Long listing, including file attributes. Add the -h option (human-readable) to print file sizes in kilobytes, megabytes, and gigabytes, instead of bytes)
      -@ (Also display OS X extended attributes. (Combine with -l.))
      -F (Decorate certain filenames with meaningful symbols, indicating their types. Ap- pends “/” to directories, “*” to executables, “@” to symbolic links, “|” to named pipes, and “=” to sockets. These are just visual indicators for you, not part of the filenames)
      -s (Prepend the size of the file in blocks, useful for sorting files by their size)
      -R (If listing a directory, list its contents recursively)
  17. cp: copy, useful options:
    -p

    Copy not only the file contents, but also the file’s permissions, timestamps, and, if you have sufficient permission to do so, its owner and group.

    (Normally the copies will be owned by you, timestamped now, with permissions set by applying your umask to the original permissions.) 

    -a

    Copy a directory hierarchy recursively, preserving all file attributes and links. 

    -R

    Copy a directory hierarchy recursively. This option does not preserve the files’

    attributes such as permissions and timestamps. It does preserve symbolic links. 

    -i

    Interactive mode. Ask before overwriting destination files.

    -f

    Force the copy. If a destination file exists, overwrite it unconditionally 

  18. mv: move, useful options: -i, -f
  19. rm: remove, recursively delete directories and all their subdirectories :
    $ rm -r dir1 dir2  // To remove a file more permanently, say, in a high-security environment,use the srm command instead 
    // for a safer removal, use -i option
    $ rm -i file1
    // save alias rm="/bin/rm -i" to ~/.profile
    • Useful options:
    • -i: ask before delete
    • -f: force delete
    • -r: Recursively remove a directory and its contents. Use with caution 
  20. ln: ln [options] source target
    • Symbolic link(soft link):
      • refers to another file by its path:
        $ ln -s myfile mysoftlink
      • Can point to files on other partitions(since, they are just reference of their path), hard link cannot
      • Can also point to directories, hard link cannot
    • Hard link: is simply a second name for a physical file on disk, if you delete the original file, the link still works
    • Useful options:
      • -s: make symbolic link, the default is hard link; -i, -f
    • find where a link point to:
      $ readlink linkname
      $ ls -l linkname
    • Aliases, however, work only in the Finder, while symbolic links work with all Macintosh applications, including the shell. If you create an alias to a folder, for example, you can open the alias in the Finder (which opens the folder), but you cannot cd into the alias from the shell. cd does follow symbolic links:
  21.   Alias A is displayed with an @ symbol next to its permissions 
  22. Directory operations:
    cd change directory
    pwd  
    basename print the final part of a file path
    dirname print the name of a directory that contains a file
    mkdir  
    rmdir  
    rm -r delete a nonempty directory and its contents
  23. basename /Users/smith/finances/money.txt 
      money.txt
    // If you provide an optional suffix, it gets stripped from the resultbasename /Users/smith/finances/money.txt .txt 
      money
    ➜ dirname /Users/smith/finances/money.txt 
      /Users/smith/finances

     mkdir: useful options:

    • -p: Given a directory path (not just a simple directory name), create any necessary parent directories automatically:
      $ mkdir -p one/two/three
    • -m: Create the directory with the given permissions:
      $ mkdir -m 0755 mydir  // m means mode
  24. rmdir: deletes one or more empty directories, useful options:
    • -p: If you supply a directory path (not just a simple directory name), delete not only the given directory, but the specified parent directories automatically, all of which must be empty: 

      $ rmdir -p one/two/three
    • use $ rm -r to delete directory and its content
  25. File Viewing: 
    cat view files in their entirety
    less view text files one page a time
    head view first lines of a text file
    tail view last lines of a text file 
    nl view text files with their lines numbered
    strings display text that's embedded in a binary file
    od view data in octal(or other formats)
    xxd view data in hexadecimal
    • cat: prints its files to standard output, concatenating them:
      $ cat * | wc -l
      // concatenates files in the current directory and counts the total number of lines

      useful options:

      • -v: print any nonprinting characters (carriage returns, etc.) in a human readable format
      • -t: Same as -v, and also print tabs as ^I
      • -e Same as -v, and also print newlines as $
      • -n: Prepend line numbers to every line.
      • -b Prepend line numbers to nonblank lines.
      • -s Squeeze each sequence of blank lines into a single blank line. 
    • less: useful options:
      • -c: Clear the screen before displaying the next page. This avoids scrolling and may be more comfortable on the eyes 
      • -m: Print a more verbose prompt, displaying the percentage of the file displayed so far 
      • -N: display the line numbers
      • -r: Display control characters literally; normally less converts them to a human- readable format 
      • -s: Squeeze multiple, adjacent blank lines into a single blank line 
      • -S: Truncate long lines to the width of the screen, instead of wrapping 
    • head: print the first 10 lines of a file: $ head * | less // preview all files in current directories; $ grep 'e' myFile | head
    • tail: -f, Keep the file open, and whenever lines are appended to the file, print them. -q, Quiet mode: when processing more than one file, don’t print a banner above each file. Normally tail prints a banner containing the filename.
  26. n
  27. n
  28. n
  29. n
posted @ 2013-08-21 15:34  wxwcase  阅读(202)  评论(0编辑  收藏  举报