美文网首页shell程序员
Shell使用技巧和常用命令

Shell使用技巧和常用命令

作者: swapmem | 来源:发表于2017-02-03 21:22 被阅读24次

    shell usage tips

    • ctrl + w
      delete a word before cursor

    • ctrl + a
      cursor move to beginning

    • ctrl + r
      search for history command

    • ctrl + k
      delete characters after cursor

    • ctrl + u
      delete the whole line

    • ctrl + e
      move to the end of line

    • ctrl + b
      move back one character

    • ctrl + f
      move forward one character

    • use bash -x scriptname to debug and see what is under going

    shell command

    • find
      commonly used switches:
      [ -name , -iname] -iname ignoring case sensitive
      [-size] +20M, -5G
      [-atime, -ctime, -mtime] unit is day +2 1 -3
      [-amin, -cmin, -mmin] unit is minute +2 1 -3
      [-type] f: regular file d: directory
      [-print0] process with filename contains null character
      [-maxdepth, -mindepth] find at most/least level

    • xargs
      [-0] process with filename contains null character
      [-Ireplace_str] eg: xargs -Ifile ls -l file
      [-P] how many processed to execute command, if specified with 0, run as many process as possible to deal with.
      [-r] don't exec command if standard input is empty

    • combine find and xargs
      eg:
      find /tmp -maxdepth 1 -type f -name \* -print0 | xargs -0 -P 0 -Ifile cp file ~/jk
      descrption:
      find all regular files in /tmp(not include subdirs) and copy them to jk directory as fast as possible

    • grep
      [-r] search recursively(include subdirs)
      [-i] ignore case sensitive
      [-w] match the whole world
      [-n] print the line number that matches
      [-v] invert match
      [-c] count how many matches

    • lsof

    [-u] list all files that a user opened
    [-i] list all processes that use a specific port. eg: ls -i TCP:22
    [-p] list all opened files by process ID
    [-c] find out files opened by some particular daemon
    [lsof /path/to/file] list processes opened by specific file

    • netstat
      [-a] list all connections
      [-t] list tcp connections
      [-u] list udp connections
      [-l] list sockets in listening state
      [-p] show the PID and name of the program to which each socket belongs.
      [netstat -tulpn] frequently used combinations
    • du
      [-sh] the size of a file. eg: du -sh Documents

    reference:

    1. http://www.thegeekstuff.com/2012/08/lsof-command-examples
    2. http://www.ibm.com/developerworks/aix/library/au-lsof.html
    3. http://webcache.googleusercontent.com/search?q=cache:http://www.unixmantra.com/2013/12/xargs-all-in-one-tutorial-guide.html

    相关文章

      网友评论

        本文标题:Shell使用技巧和常用命令

        本文链接:https://www.haomeiwen.com/subject/rhhvpttx.html