资讯专栏INFORMATION COLUMN

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

Achilles / 2389人阅读

摘要:经常使用命令行,比如测试接口响应时间奈何命令行参数太多,记不住怎么办这时候你需要个男人,它就是男人的确很强悍,给出了这么多提示,但没有我真正想要的。。。就是这个更强壮的男人,它一个命令行工具,直接使用来安装。

经常使用命令行,比如 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. 1 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.125000 | time_total: 0.141000
  4. 2 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.094000 | time_total: 0.109000
  5. 3 | time_namelookup: 0.016000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000
  6. 4 | time_namelookup: 0.015000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000
  7. 5 | time_namelookup: 0.031000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000
  8. 6 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.094000 | time_total: 0.109000
  9. 7 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.125000 | time_total: 0.125000
  10. 8 | time_namelookup: 0.000001 | time_connect: 0.016000 | time_starttransfer: 0.141000 | time_total: 0.141000
  11. 9 | time_namelookup: 0.015000 | time_connect: 0.015000 | time_starttransfer: 0.093000 | time_total: 0.109000
  12. 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. NAME
  4. curl - transfer a URL
  5. SYNOPSIS
  6. curl [options] [URL...]
  7. DESCRIPTION
  8. curl is a tool to transfer data from or to a server, using one of the supported protocols
  9. (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP,
  10. RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user
  11. interaction.
  12. curl offers a busload of useful tricks like proxy support, user authentication, FTP upload,
  13. HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more. As you will
  14. see below, the number of features will make your head spin!
  15. curl is powered by libcurl for all transfer-related features. See libcurl(3) for details.
  16. URL
  17. The URL syntax is protocol-dependent. You"ll find a detailed description in RFC 3986.
  18. You can specify multiple URLs or parts of URLs by writing part sets within braces as in:
  19. http://site.{one,two,three}.com
  20. or you can get sequences of alphanumeric series by using [] as in:
  21. ftp://ftp.numericals.com/file[1-100].txt
  22. ftp://ftp.numericals.com/file[001-100].txt (with leading zeros)
  23. 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. New minor version of npm available! 6.4.1 → 6.9.0
  8. Changelog: https://github.com/npm/cli/releases/tag/v6.9.0
  9. │ Run npm install -g npm to update! │
  10. │ │
  11. ╰───────────────────────────────────────────────────────────────╯

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

</>复制代码

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

小试牛刀下

</>复制代码

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

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

</>复制代码

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

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

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

</>复制代码

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

除了node 还有其他版本 https://github.com/tldr-pages...
比如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
  4. # To edit a cheatsheet
  5. cheat -e
  6. # To list available cheatsheets
  7. cheat -l
  8. # To search available cheatsheets
  9. cheat -s
  10. # To get the current `cheat" version
  11. cheat -v

试试 curl

</>复制代码

  1. $ cheat curl
  2. # Download a single file
  3. curl http://path.to.the/file
  4. # Download a file and specify a new filename
  5. curl http://example.com/file.zip -o new_file.zip
  6. # Download multiple files
  7. curl -O URLOfFirstFile -O URLOfSecondFile
  8. # Download all sequentially numbered files (1-24)
  9. curl http://example.com/pic[1-24].jpg
  10. # Download a file and pass HTTP Authentication
  11. curl -u username:password URL
  12. # Download a file with a Proxy
  13. curl -x proxysever.server.com:PORT http://addressiwantto.access
  14. # Download a file from FTP
  15. curl -u username:password -O ftp://example.com/pub/file.zip
  16. # Get an FTP directory listing
  17. curl ftp://username:password@example.com
  18. # Resume a previously failed download
  19. curl -C - -o partial_file.zip http://example.com/file.zip
  20. # Fetch only the HTTP headers from a response
  21. curl -I http://example.com
  22. # Fetch your external IP and network info as JSON
  23. curl http://ifconfig.me/all/json
  24. # Limit the rate of a download
  25. curl --limit-rate 1000B -O http://path.to.the/file
  26. # Get your global IP
  27. curl httpbin.org/ip
  28. # Get only the HTTP status code
  29. curl -o /dev/null -w "%{http_code}
  30. " -s -I URL
  31. $ cheat python
  32. # Desc: Python is a high-level programming language.
  33. # Basic example of server with python
  34. # Will start a Web Server in the current directory on port 8000
  35. # go to http://127.0.0.1:8000
  36. # Python v2.7
  37. python -m SimpleHTTPServer
  38. # Python 3
  39. python -m http.server 8000
  40. # SMTP-Server for debugging, messages will be discarded, and printed on stdout.
  41. python -m smtpd -n -c DebuggingServer localhost:1025
  42. # Pretty print a json
  43. python -mjson.tool

比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. # Download a file and specify a new filename
  5. curl http://example.com/file.zip -o new_file.zip
  6. # Download multiple files
  7. curl -O URLOfFirstFile -O URLOfSecondFile
  8. # Download all sequentially numbered files (1-24)
  9. curl http://example.com/pic[1-24].jpg
  10. # Download a file and follow redirects
  11. curl -L http://example.com/file
  12. # Download a file and pass HTTP Authentication
  13. curl -u username:password URL
  14. # Download a file with a Proxy
  15. curl -x proxysever.server.com:PORT http://addressiwantto.access
  16. # Download a file from FTP
  17. curl -u username:password -O ftp://example.com/pub/file.zip
  18. # Get an FTP directory listing
  19. curl ftp://username:password@example.com
  20. # Resume a previously failed download
  21. curl -C - -o partial_file.zip http://example.com/file.zip
  22. # Fetch only the HTTP headers from a response
  23. curl -I http://example.com
  24. # Fetch your external IP and network info as JSON
  25. curl http://ifconfig.me/all/json
  26. # Limit the rate of a download
  27. curl --limit-rate 1000B -O http://path.to.the/file
  28. # POST to a form
  29. curl -F "name=user" -F "password=test" http://example.com
  30. # POST JSON Data
  31. curl -H "Content-Type: application/json" -X POST -d "{"user":"bob","pass":"123"}" http://example.com
  32. # POST data from the standard in / share data on sprunge.us
  33. 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. #
  4. # You have to create Session (http://www.python-
  5. # requests.org/en/latest/api/requests.Session) object and set
  6. # max_redirects variable to 3
  7. session = requests.Session()
  8. session.max_redirects = 3
  9. session.get(url)
  10. # TooManyRedirects exception will be raised if a requests exceeds
  11. # maximum number of redirects.
  12. #
  13. # Related github issue discussing why you can not set max_redirects per
  14. # request https://github.com/kennethreitz/requests/issues/1300
  15. #
  16. # [Alik] [so/q/31552627] [cc by-sa 3.0]

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

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

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

资源

微软最爽命令行工具
Linux命令大全搜索工具
Linux工具快速教程
有趣的Linux命令行工具
命令行的艺术
假装很忙的三个命令行工具
28个UNIX/LINUX的命令行神器
命令行工具
PHPer 必知必会的 Linux 命令

推荐阅读:

那些你可能不知道的浏览器奇技淫巧

那些你可能不知道的微信奇技淫巧

那些你可能不知道的微博奇技淫巧

那些你可能不知道的网易云音乐奇技淫巧

那些你可能不知道的搜索奇技淫巧

那些你可能不知道的视频下载奇技淫巧

那些你可能不知道的免费观看 VIP 视频奇技淫巧

公众号:苏生不惑

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44152.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<