Linux Notes

- 4 mins

Alias

  1. create
     alias foo = "cd ..; ls; cd ..; ls"
    
  2. delete
     unalias foo
    
  3. Alias will be gone if the terminal is closed. To make these alias persistent, store them in bashrc/zshrc.
     nano .zshrc
    

History

  1. history of inputs
     history
     history | grep ls
    
  2. control + r to enter query mode, and control + c to exit.

Details of a command

  1. type of a command
     type ls
    
  2. manual page of a command
     man ls
    
  3. whatis
     whatis ls
    

I/O Redirection

  1. store output, if there is error, error will show in terminal and output.txt will not be written
     ls -l > output.txt
    
  2. > is used to overwrite a file and >> is used to append a file
     ls -l >> output.txt
    

Error Redirection

Types of I/O:

Error messages are printed to standard error. > only redirects standard output, so standard error is still shown on the terminal. To redirect them:

  1. stdout to one file and stderr to another file
     command >output.txt 2>error.txt
    
  2. redirect stderr to stdout(&1)
     command >output.txt 2>&1
    
  3. redirect both to a file
     command &> output.txt
    

cat

  1. open a file
     cat file.txt
    
  2. create and write to a file, type control + d twice to exit.
     cat > test.txt
    

pipelines and grep

  1. the pipeline makes the last output as the input for next command.
     stdin | stdout
    
  2. uniq
     ls | uniq
    
  3. sort
     ls | uniq | sort
    
  4. wc - word count
     wc -l : prints the number of lines in a file.
     wc -w : prints the number of words in a file.
     wc -c : prints the count of bytes in a file.
     wc -m : prints the count of characters from a file.
     wc -L : prints only the length of the longest line in a file.
    
  5. grep
     grep "pattern" sample.txt
     ls | grep txt
    
  6. head/tail -n shows first/last n lines
     head -n 5
     tail -n 5
    

Echo

  1. Echo will print matching files in current dir
     echo * : prints all files under current dir.
     echo D* : prints all files starting with D.
     echo [[:upper:]]* : all files starting with UpperCase.
     echo Five Divided By Two is $((5 / 2)) : Five Divided By Two is 2.
    
  2. {}
     echo here {1..9}
     echo hey {Z..A}
    
  3. $
     echo $(ls)
    

    More about Dollar Sign $

Quotes and Exceptions

  1. Exceptions
     `
     \
     $
    
  2. "" and ''
     echo example ~/P* $(echo test) $((2 + 2)) $USER
     Output: example /Users/yiyang/Parallels /Users/yiyang/Pictures /Users/yiyang/Public test 4 yiyang
    
     echo "example ~/P* $(echo test) $((2 + 2)) $USER"
     Output: example ~/P* test 4 yiyang
    
     echo 'example ~/P* $(echo test) $((2 + 2)) $USER'
     Output: example ~/P* $(echo test) $((2 + 2)) $USER
    

Processes

  1. list all processes
     ps     : prints all processes running in the terminal
     ps x   : prints all processes in the machine
     ps aux : prints all processes in the machine with detailed info
    
  2. top lists top processes sorted by %CPU
     top
    
  3. kill the specified pid
     kill 45461
    

Environment Variables

  1. printenv shows environment variables
  2. export
     export intern=yy
     echo $intern
    
  3. unset removes the env var
     unset intern
    
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora