Haste makes waste

linux-08-shell与shell script-便利そうなコマンドまとめ

Posted on By lijun

1. Shortcut

  • Ctrl-a Move to the begining of the line
  • Ctrl-e Move to the end of the line
  • Ctrl-u Deletes from cursor to beginning of line
  • Ctrl-k Deletes from cursor to end of line
  • Ctrl-w Deletes from cursor one word before
  • Ctrl-l same as clear command

2. SystemInformation

  • free:memory information
  • cat /proc/cpuinfo:CPU
  • df:harddisk
  • du:folder

-hを付けてHelp情報を調べる

3. ファイルを一括作成

  • touch {apple,orange,banana}
  • touch file_{001..100}
  • echo {a..Z}
  • touch {apple,banana,cherry}_{01..100}{w..d}
  • ls -l | wc -l:计算行数,即得到当前文件夹内文件数目

4. pipe

  • cp -v * ../otherfolder 1>../success.txt 2>../error.txt:普通的出力输出到sucess.txt,error输出到error.txt
  • cp -v * ../otherfolder &>../log.txt:普通的出力和error都输出到log.txt
  • ls > /dev/null:不输出,丢到类似回收站的地方
  • echo "Some text" > file.txt:写入文件
  • echo "Some more text" >> file.txt:追加模式写入文件
  • > file.txt:覆盖模式,清空文件

5. grep

  • grep error auth.log
  • grep --color=auto error auth.log
  • export GREP_OPTIONS='--color=auto'
  • grep -i break-in auth.log
  • grep -i break-in auth.log | awk {'print $12'}
  • ping -c 1 example.com | grep 'bytes from' | cut -d = -f 4

6. 环境变量

  • $HOME
  • $PWD
  • $MACHTYPE
  • $HOSTNAME
  • $BASH_VERSION