卡尼多隨筆

認識自我 • 感受世界 • 創造價值

0%

實用 Linux 指令

記錄一些指令,方便之後快速回憶!

  • man command → manual page
    • h - 秀出快捷鍵,像是要在視窗內 moving、searching、jumping,可以怎麼下
    • /pattern - 往下找符合 pattern 的字串
      • n - 跳到下一個符合 pattern 的字串
      • N - 跳到上一個符合 pattern 的字串
    • Enter - 往下移動一行
    • Space - 往下移動一頁
    • g - 移動到 manual page 的最上方
    • G - 移動到 manual page 的最下方
    • q - 離開 manual page
  • which python3 → /opt/homebrew/bin/python3
    • which - locate a program file in the user’s path
  • mkdir -p newdir/product/reviews
    • -p - Create intermediate directories as required.
  • chmod u=rwx,g+x,o= sales.data
    • (1) ugoa - u for user, g for group, o for other, a for all.
    • (2) +-= (3) rwx
  • chmod 754 file
    • U → rwx → 111 → 7
    • G → r-x → 101 → 5
    • O → r– → 100 → 4
  • umask - a command that determines the settings of a mask that controls how file permissions are set for newly created files.
  • find [path...] [expression] - Recursively finds files in path that match expression.
    • find . -name "Screen*" (-iname 會忽略大小寫)
    • find . -ls - 對找到的檔案們進行 ls
    • find . -mtime num_days - 找出所有 num_days-old 的檔案
      • find . -mtime +10 -mtime -13 - more than 10 days old, but less than 13 days old
    • find . -size +500k -size -10M
    • find . -newer file
    • find . -exec command {} \; - Run command against all the files that are found.
      • find root_path -name '*.ext' -exec wc -l {} \; - Run a command for each file (use {} within the command to access the filename)
    • find . -type d -newer b.txt - find directories that are newer than a given file
  • locate pattern - List files that match pattern.
    • When you run locate it is simply querying the index or database created by updatedb and not looking at each file on the system.
    • This is really, really fast.
    • The down side is that the data is not in real time.
  • 除了 diff 也有 sdiffvimdiff
  • grep -v pattern file - Selected lines are those not matching any of the specified patterns.
    • -v, –invert-match
  • The pipe (|) means take the standard output from the preceding command and pass it as the standard input to the following command.
  • Pipe Output to a Pager
    • strings ~/Documents/test.mp3 | less
  • Redirection
    • standard input → stdin → 0
    • standard output → stdout → 1
    • standard error → stderr → 2
    • > - Redirects standard output to a file, overwriting (truncating) any existing contents of the file. If no file exists, it creates one. ls -lF /opt/apache > files.txt same as ls -lF /opt/apache 1> files.txt
    • >> - Redirects standard output to a file and appends to any existing contents. If no file exists, it creates one. ls -lF /opt/apache >> files.txt
    • < - Redirects input from a file to the command preceding the less-than sign. sort < files.txt same as sort 0< files.txt
    • sort < files.txt > sorted_files.txt - The output of the sort command is then redirected to the sorted_files.txt file.
    • & - Used with redirection to signal that a file descriptor is being used instead of a file name.
    • 2>&1 - Combine standard error and standard output.
    • 2> file - Redirect standard error to a file.
    • ls here not-here 1> out 2> out.err
      • cat out → here
      • cat out.err → ls: not-here: No such file or directory
    • ls here not-here > out.both 2>&1
      • cat out.both → ls: not-here: No such file or directory \n here
    • ls here not-here > /dev/null 2>&1 - Redirect output to nowhere.
  • scp source destination - Copy source to destination.
    • scp test.txt linuxsvr1:~/
  • echo 'export xxx="ooo"' >> ~/.bash_profile - To make your xxx persist between logins, add the value to your personal initialization files.
  • alias ll='ls -l' (add them to one of your personal initialization files, like .zshrc, to make them persist between sessions)
    • You could create these shortcuts to help you feel more at home.
  • .zshrc 編輯完後可 source .zshrc,就不用得重開一個新的 terminal 才會有新的設定
  • history (可設定環境變數 HISTSIZE,值越大,保存越多 command history)
  • Tab Completion
  • Dealing with Long Shell Commands: The backslash (\) is the line continuation character.
  • printenv – print out the environment variables (env 也可以)
  • crontab - Schedule cron jobs to run on a time interval for the current user.