比man更强悍的命令行工具cheat,cheat


经常使用命令行,比如 curl 测试接口响应时间,

  1. for i in {1..10};do curl -o /dev/null -s   -w "$i | time_namelookup: %{time_namelookup} | time_connect: %{time_connect} | time_starttransfer: %{time_starttransfer} | time_total: %{time_total} 
  2. " "http://httpbin.org/ip";done 
  3.  
  4. 1 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.125000 | time_total: 0.141000 
  5. 2 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.094000 | time_total: 0.109000 
  6. 3 | time_namelookup: 0.016000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000 
  7. 4 | time_namelookup: 0.015000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000 
  8. 5 | time_namelookup: 0.031000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000 
  9. 6 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.094000 | time_total: 0.109000 
  10. 7 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.125000 | time_total: 0.125000 
  11. 8 | time_namelookup: 0.000001 | time_connect: 0.016000 | time_starttransfer: 0.141000 | time_total: 0.141000 
  12. 9 | time_namelookup: 0.015000 | time_connect: 0.015000 | time_starttransfer: 0.093000 | time_total: 0.109000 
  13. 10 | time_namelookup: 0.000001 | time_connect: 0.015000 | time_starttransfer: 0.109000 | time_total: 0.125000 

奈何命令行参数太多,记不住怎么办?这时候你需要个男人,它就是 man。

man

  1. #man curl  
  2. curl(1)                                      Curl Manual                                     curl(1) 
  3.  
  4. NAME 
  5.        curl - transfer a URL 
  6.  
  7. SYNOPSIS 
  8.        curl [options] [URL...] 
  9.  
  10. DESCRIPTION 
  11.        curl  is  a  tool  to transfer data from or to a server, using one of the supported protocols 
  12.        (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP,  LDAPS,  POP3,  POP3S,  RTMP, 
  13.        RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP).  The command is designed to work without user 
  14.        interaction. 
  15.  
  16.        curl offers a busload of useful tricks like proxy support, user authentication,  FTP  upload, 
  17.        HTTP  post,  SSL  connections, cookies, file transfer resume, Metalink, and more. As you will 
  18.        see below, the number of features will make your head spin! 
  19.  
  20.        curl is powered by libcurl for all transfer-related features. See libcurl(3) for details. 
  21.  
  22. URL 
  23.        The URL syntax is protocol-dependent. You'll find a detailed description in RFC 3986. 
  24.  
  25.        You can specify multiple URLs or parts of URLs by writing part sets within braces as in: 
  26.  
  27.         http://site.{one,two,three}.com 
  28.  
  29.        or you can get sequences of alphanumeric series by using [] as in: 
  30.  
  31.         ftp://ftp.numericals.com/file[1-100].txt 
  32.         ftp://ftp.numericals.com/file[001-100].txt    (with leading zeros) 
  33.         ftp://ftp.letters.com/file[a-z].txt 

男人的确很强悍,给出了这么多提示,但没有我真正想要的。。。还是不知道怎么用。

相信你在技术文章里经常会看到 TL;DR 即Too Long; Didn’t Read. 太长不看, man curl 的内容就是太长了,我不看。

就是这个更强壮的男人 tldr,它一个命令行工具,直接使用 npm install -g tldr 来安装。

tldr

  1. [root@VM_0_14_centos ~]# npm install -g tldr 
  2. /usr/local/n/versions/node/11.4.0/bin/tldr -> /usr/local/n/versions/node/11.4.0/lib/node_modules/tldr/bin/tldr 
  3. + tldr@3.2.7 
  4. added 113 packages from 103 contributors in 60.759s 
  5.  
  6.  
  7.    ╭───────────────────────────────────────────────────────────────╮ 
  8.    │                                                               │ 
  9.    │       New minor version of npm available! 6.4.1 → 6.9.0       │ 
  10.    │   Changelog: https://github.com/npm/cli/releases/tag/v6.9.0   │ 
  11.    │               Run npm install -g npm to update!               │ 
  12.    │                                                               │ 
  13.    ╰───────────────────────────────────────────────────────────────╯ 

安装完后先看看 tldr 自己怎么用吧。

  1. $ tldr tldr 
  2.  
  3.   tldr 
  4.  
  5.   Simplified man pages. 
  6.   More information: . 
  7.  
  8.   - Get typical usages of a command (hint: this is how you got here!): 
  9.     tldr command 
  10.  
  11.   - Show the tar tldr page for linux: 
  12.     tldr -p linux tar 
  13.  
  14.   - Get help for a git subcommand: 
  15.     tldr git checkout 

小试牛刀下

  1. $ tldr -p linux tar 
  2.  
  3.   tar 
  4.  
  5.   Archiving utility. 
  6.   Often combined with a compression method, such as gzip or bzip. 
  7.   More information: . 
  8.  
  9.   - Create an archive from files: 
  10.     tar -cf target.tar file1 file2 file3 
  11.  
  12.   - Create a gzipped archive: 
  13.     tar -czf target.tar.gz file1 file2 file3 
  14.  
  15.   - Extract an archive in a target directory: 
  16.     tar -xf source.tar -C directory 
  17.  
  18.   - Extract a gzipped archive in the current directory: 
  19.     tar -xzf source.tar.gz 
  20.  
  21.   - Extract a bzipped archive in the current directory: 
  22.     tar -xjf source.tar.bz2 
  23.  
  24.   - Create a compressed archive, using archive suffix to determine the compression program: 
  25.     tar -caf target.tar.xz file1 file2 file3 
  26.  
  27.   - List the contents of a tar file: 
  28.     tar -tvf source.tar 
  29.  
  30.   - Extract files matching a pattern: 
  31.     tar -xf source.tar --wildcards "*.html" 

嗯,很简洁,直接给出了tar需要的参数,再来看curl

  1. λ tldr curl 
  2.  
  3.   curl 
  4.  
  5.   Transfers data from or to a server. 
  6.   Supports most protocols, including HTTP, FTP, and POP3. 
  7.  
  8.   - Download the contents of an URL to a file: 
  9.     curl http://example.com -o filename 
  10.  
  11.   - Download a file, saving the output under the filename indicated by the URL: 
  12.     curl -O http://example.com/filename 
  13.  
  14.   - Download a file, following [L]ocation redirects, and automatically [C]ontinuing (resuming) a previous file transfer: 
  15.     curl -O -L -C - http://example.com/filename 
  16.  
  17.   - Send form-encoded data (POST request of type application/x-www-form-urlencoded): 
  18.     curl -d 'name=bob' http://example.com/form 
  19.  
  20.   - Send a request with an extra header, using a custom HTTP method: 
  21.     curl -H 'X-My-Header: 123' -X PUT http://example.com 
  22.  
  23.   - Send data in JSON format, specifying the appropriate content-type header: 
  24.     curl -d '{"name":"bob"}' -H 'Content-Type: application/json' http://example.com/users/1234 
  25.  
  26.   - Pass a user name and password for server authentication: 
  27.     curl -u myusername:mypassword http://example.com 
  28.  
  29.   - Pass client certificate and key for a resource, skipping certificate validation: 
  30.     curl --cert client.pem --key key.pem --insecure https://example.com 

这个男人果然更强悍,常用的curl命令都包括了,我喜欢。

除了自带的命令,安装的命令也可以

  1. [root@VM_0_14_centos ~]# tldr python 
  2.  
  3.   python 
  4.  
  5.   Python language interpreter. 
  6.   More information: https://www.python.org. 
  7.  
  8.   - Call a Python interactive shell (REPL): 
  9.     python 
  10.  
  11.   - Execute script in a given Python file: 
  12.     python script.py 
  13.  
  14.   - Execute script as part of an interactive shell: 
  15.     python -i script.py 
  16.  
  17.   - Execute a Python expression: 
  18.     python -c "expression" 
  19.  
  20.   - Run library module as a script (terminates option list): 
  21.     python -m module arguments 
  22.  
  23.   - Interactively debug a Python script: 
  24.     python -m pdb script.py 

除了node 还有其他版本 https://github.com/tldr-pages/tldr

比如Python,直接pip install tldr安装。

如果你不想安装tldr,也可以直接使用网页在线查看https://tldr.sh/

有了tldr,妈妈再也不用担心我记不住命令行参数了,还有没有比 tldr更强悍的男人呢,有,比如cheat https://github.com/cheat/cheat ,直接使用pip install cheat安装。

cheat

看看 cheat 怎么用吧

  1. $ cheat cheat 
  2. # To see example usage of a program: 
  3. cheat <command> 
  4.  
  5. # To edit a cheatsheet 
  6. cheat -e <command> 
  7.  
  8. # To list available cheatsheets 
  9. cheat -l 
  10.  
  11. # To search available cheatsheets 
  12. cheat -s <command> 
  13.  
  14. # To get the current `cheat' version 
  15. cheat -v 

试试 curl

  1. $ cheat curl 
  2. # Download a single file 
  3. curl http://path.to.the/file 
  4.  
  5. # Download a file and specify a new filename 
  6. curl http://example.com/file.zip -o new_file.zip 
  7.  
  8. # Download multiple files 
  9. curl -O URLOfFirstFile -O URLOfSecondFile 
  10.  
  11. # Download all sequentially numbered files (1-24) 
  12. curl http://example.com/pic[1-24].jpg 
  13.  
  14. # Download a file and pass HTTP Authentication 
  15. curl -u username:password URL 
  16.  
  17. # Download a file with a Proxy 
  18. curl -x proxysever.server.com:PORT http://addressiwantto.access 
  19.  
  20. # Download a file from FTP 
  21. curl -u username:password -O ftp://example.com/pub/file.zip 
  22.  
  23. # Get an FTP directory listing 
  24. curl ftp://username:password@example.com 
  25.  
  26. # Resume a previously failed download 
  27. curl -C - -o partial_file.zip http://example.com/file.zip 
  28.  
  29. # Fetch only the HTTP headers from a response 
  30. curl -I http://example.com 
  31.  
  32. # Fetch your external IP and network info as JSON 
  33. curl http://ifconfig.me/all/json 
  34.  
  35. # Limit the rate of a download 
  36. curl --limit-rate 1000B -O http://path.to.the/file 
  37.  
  38. # Get your global IP 
  39. curl httpbin.org/ip 
  40.  
  41. # Get only the HTTP status code 
  42. curl -o /dev/null -w '%{http_code} 
  43. ' -s -I URL 

比tldr更详细,如果你也不想安装可以直接使用curl

cht.sh

  1. [root@VM_0_14_centos ~]# curl cht.sh/curl 
  2. # Download a single file 
  3. curl http://path.to.the/file 
  4.  
  5. # Download a file and specify a new filename 
  6. curl http://example.com/file.zip -o new_file.zip 
  7.  
  8. # Download multiple files 
  9. curl -O URLOfFirstFile -O URLOfSecondFile 
  10.  
  11. # Download all sequentially numbered files (1-24) 
  12. curl http://example.com/pic[1-24].jpg 
  13.  
  14. # Download a file and follow redirects 
  15. curl -L http://example.com/file 
  16.  
  17. # Download a file and pass HTTP Authentication 
  18. curl -u username:password URL 
  19.  
  20. # Download a file with a Proxy 
  21. curl -x proxysever.server.com:PORT http://addressiwantto.access 
  22.  
  23. # Download a file from FTP 
  24. curl -u username:password -O ftp://example.com/pub/file.zip 
  25.  
  26. # Get an FTP directory listing 
  27. curl ftp://username:password@example.com 
  28.  
  29. # Resume a previously failed download 
  30. curl -C - -o partial_file.zip http://example.com/file.zip 
  31.  
  32. # Fetch only the HTTP headers from a response 
  33. curl -I http://example.com 
  34.  
  35. # Fetch your external IP and network info as JSON 
  36. curl http://ifconfig.me/all/json 
  37.  
  38. # Limit the rate of a download 
  39. curl --limit-rate 1000B -O http://path.to.the/file 
  40.  
  41. # POST to a form 
  42. curl -F "name=user" -F "password=test" http://example.com 
  43.  
  44. # POST JSON Data 
  45. curl -H "Content-Type: application/json" -X POST -d '{"user":"bob","pass":"123"}' http://example.com 
  46.  
  47. # POST data from the standard in / share data on sprunge.us 
  48. curl -F 'sprunge=<-' sprunge.us 

看看Python的requests怎么用

  1. [root@VM_0_14_centos ~]# curl cheat.sh/python/requests 
  2. #  python-requests: Limit Number of Redirects Followed 
  3. #  You have to create Session (http://www.python- 
  4. #  requests.org/en/latest/api/requests.Session) object and set 
  5. #  max_redirects variable to 3 
  6.  
  7. session = requests.Session() 
  8. session.max_redirects = 3 
  9. session.get(url) 
  10.  
  11. #  TooManyRedirects exception will be raised if a requests exceeds 
  12. #  maximum number of redirects. 
  13. #  Related github issue discussing why you can not set max_redirects per 
  14. #  request https://github.com/kennethreitz/requests/issues/1300 
  15. #  [Alik] [so/q/31552627] [cc by-sa 3.0] 

它也有网页版 http://cht.sh/curl

有了tldr和cheat,再也不用记那么多命令行参数了。

win下执行命令行的工具我常用cmder,如果你用的win10,可以尝试下微软最新发布的Terminal https://github.com/microsoft/Terminal

相关内容