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

2021-12-24 良許Linux

☞ 程式設計師進階架構師必備資源免費送 ☜

經常使用命令行,比如 curl 測試接口響應時間

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}
" "http://httpbin.org/ip";done

1 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.125000 | time_total: 0.141000
2 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.094000 | time_total: 0.109000
3 | time_namelookup: 0.016000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000
4 | time_namelookup: 0.015000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000
5 | time_namelookup: 0.031000 | time_connect: 0.031000 | time_starttransfer: 0.109000 | time_total: 0.109000
6 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.094000 | time_total: 0.109000
7 | time_namelookup: 0.016000 | time_connect: 0.016000 | time_starttransfer: 0.125000 | time_total: 0.125000
8 | time_namelookup: 0.000001 | time_connect: 0.016000 | time_starttransfer: 0.141000 | time_total: 0.141000
9 | time_namelookup: 0.015000 | time_connect: 0.015000 | time_starttransfer: 0.093000 | time_total: 0.109000
10 | time_namelookup: 0.000001 | time_connect: 0.015000 | time_starttransfer: 0.109000 | time_total: 0.125000

奈何命令行參數太多,記不住怎麼辦?這時候你需要個男人,它就是 man

man

#man curl 
curl(1)                                      Curl Manual                                     curl(1)

NAME
       curl - transfer a URL

SYNOPSIS
       curl [options] [URL...]

DESCRIPTION
       curl  is  a  tool  to transfer data from or to a server, using one of the supported protocols
       (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP,  LDAPS,  POP3,  POP3S,  RTMP,
       RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP).  The command is designed to work without user
       interaction.

       curl offers a busload of useful tricks like proxy support, user authentication,  FTP  upload,
       HTTP  post,  SSL  connections, cookies, file transfer resume, Metalink, and more. As you will
       see below, the number of features will make your head spin!

       curl is powered by libcurl for all transfer-related features. See libcurl(3) for details.

URL
       The URL syntax is protocol-dependent. You'll find a detailed description in RFC 3986.

       You can specify multiple URLs or parts of URLs by writing part sets within braces as in:

        http://site.{one,two,three}.com

       or you can get sequences of alphanumeric series by using [] as in:

        ftp://ftp.numericals.com/file[1-100].txt
        ftp://ftp.numericals.com/file[001-100].txt    (with leading zeros)
        ftp://ftp.letters.com/file[a-z].txt

男人的確很強悍,給出了這麼多提示,但沒有我真正想要的。。。還是不知道怎麼用。

相信你在技術文章裡經常會看到 TL;DR 即Too Long; Didn’t Read. 太長不看, man curl 的內容就是太長了,我不看。

就是這個更強壯的男人 tldr,它一個命令行工具,直接使用 npm install -g tldr 來安裝。

tldr

[root@VM_0_14_centos ~]# npm install -g tldr
/usr/local/n/versions/node/11.4.0/bin/tldr -> /usr/local/n/versions/node/11.4.0/lib/node_modules/tldr/bin/tldr
+ tldr@3.2.7
added 113 packages from 103 contributors in 60.759s


   ╭───────────────────────────────────────────────────────────────╮
   │                                                               │
   │       New minor version of npm available! 6.4.1 → 6.9.0       │
   │   Changelog: https:
   │               Run npm install -g npm to update!               │
   │                                                               │
   ╰───────────────────────────────────────────────────────────────╯

安裝完後先看看 tldr 自己怎麼用吧。

$ tldr tldr

  tldr

  Simplified man pages.
  More information: .

  - Get typical usages of a command (hint: this is how you got here!):
    tldr command

  - Show the tar tldr page for linux:
    tldr -p linux tar

  - Get help for a git subcommand:
    tldr git checkout

小試牛刀下

$ tldr -p linux tar

  tar

  Archiving utility.
  Often combined with a compression method, such as gzip or bzip.
  More information: .

  - Create an archive from files:
    tar -cf target.tar file1 file2 file3

  - Create a gzipped archive:
    tar -czf target.tar.gz file1 file2 file3

  - Extract an archive in a target directory:
    tar -xf source.tar -C directory

  - Extract a gzipped archive in the current directory:
    tar -xzf source.tar.gz

  - Extract a bzipped archive in the current directory:
    tar -xjf source.tar.bz2

  - Create a compressed archive, using archive suffix to determine the compression program:
    tar -caf target.tar.xz file1 file2 file3

  - List the contents of a tar file:
    tar -tvf source.tar

  - Extract files matching a pattern:
    tar -xf source.tar --wildcards "*.html"

嗯,很簡潔,直接給出了tar需要的參數,再來看curl

λ tldr curl

  curl

  Transfers data from or to a server.
  Supports most protocols, including HTTP, FTP, and POP3.

  - Download the contents of an URL to a file:
    curl http:

  - Download a file, saving the output under the filename indicated by the URL:
    curl -O http:

  - Download a file, following [L]ocation redirects, and automatically [C]ontinuing (resuming) a previous file transfer:
    curl -O -L -C - http:

  - Send form-encoded data (POST request of type application/x-www-form-urlencoded):
    curl -d 'name=bob' http:

  - Send a request with an extra header, using a custom HTTP method:
    curl -H 'X-My-Header: 123' -X PUT http:

  - Send data in JSON format, specifying the appropriate content-type header:
    curl -d '{"name":"bob"}' -H 'Content-Type: application/json' http:

  - Pass a user name and password for server authentication:
    curl -u myusername:mypassword http:

  - Pass client certificate and key for a resource, skipping certificate validation:
    curl --cert client.pem --key key.pem --insecure https:

這個男人果然更強悍,常用的curl命令都包括了,我喜歡。

除了自帶的命令,安裝的命令也可以

[root@VM_0_14_centos ~]# tldr python

  python

  Python language interpreter.
  More information: https:

  - Call a Python interactive shell (REPL):
    python

  - Execute script in a given Python file:
    python script.py

  - Execute script as part of an interactive shell:
    python -i script.py

  - Execute a Python expression:
    python -c "expression"

  - Run library module as a script (terminates option list):
    python -m module arguments

  - Interactively debug a Python script:
    python -m pdb script.py

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

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

如果你不想安裝tldr,也可以直接使用網頁在線查看https://tldr.sh/

image.png

有了tldr,媽媽再也不用擔心我記不住命令行參數了,還有沒有比 tldr更強悍的男人呢,有,比如cheat https://github.com/cheat/cheat ,直接使用pip install cheat安裝。

cheat

看看 cheat 怎麼用吧

$ cheat cheat
# To see example usage of a program:
cheat <command>

# To edit a cheatsheet
cheat -e <command>

# To list available cheatsheets
cheat -l

# To search available cheatsheets
cheat -s <command>

# To get the current `cheat' version
cheat -v

試試 curl

$ cheat curl
# Download a single file
curl http:

# Download a file and specify a new filename
curl http:

# Download multiple files
curl -O URLOfFirstFile -O URLOfSecondFile

# Download all sequentially numbered files (1-24)
curl http:

# Download a file and pass HTTP Authentication
curl -u username:password URL

# Download a file with a Proxy
curl -x proxysever.server.com:PORT http:

# Download a file from FTP
curl -u username:password -O ftp:

# Get an FTP directory listing
curl ftp:

# Resume a previously failed download
curl -C - -o partial_file.zip http:

# Fetch only the HTTP headers from a response
curl -I http:

# Fetch your external IP and network info as JSON
curl http:

# Limit the rate of a download
curl --limit-rate 1000B -O http:

# Get your global IP
curl httpbin.org/ip

# Get only the HTTP status code
curl -o /dev/null -w '%{http_code}
' -s -I URL


比tldr更詳細,如果你也不想安裝可以直接使用curl

cht.sh

[root@VM_0_14_centos ~]# curl cht.sh/curl
# Download a single file
curl http:

# Download a file and specify a new filename
curl http:

# Download multiple files
curl -O URLOfFirstFile -O URLOfSecondFile

# Download all sequentially numbered files (1-24)
curl http:

# Download a file and follow redirects
curl -L http:

# Download a file and pass HTTP Authentication
curl -u username:password URL

# Download a file with a Proxy
curl -x proxysever.server.com:PORT http:

# Download a file from FTP
curl -u username:password -O ftp:

# Get an FTP directory listing
curl ftp:

# Resume a previously failed download
curl -C - -o partial_file.zip http:

# Fetch only the HTTP headers from a response
curl -I http:

# Fetch your external IP and network info as JSON
curl http:

# Limit the rate of a download
curl --limit-rate 1000B -O http:

# POST to a form
curl -F "name=user" -F "password=test" http:

# POST JSON Data
curl -H "Content-Type: application/json" -X POST -d '{"user":"bob","pass":"123"}' http:

# POST data from the standard in / share data on sprunge.us
curl -F 'sprunge=<-' sprunge.us

看看Python的requests怎麼用

[root@VM_0_14_centos ~]# curl cheat.sh/python/requests
#  python-requests: Limit Number of Redirects Followed
#
#  You have to create Session (http:
#  requests.org/en/latest/api/requests.Session) object and set
#  max_redirects variable to 3

session = requests.Session()
session.max_redirects = 3
session.get(url)

#  TooManyRedirects exception will be raised if a requests exceeds
#  maximum number of redirects.
#
#  Related github issue discussing why you can not set max_redirects per
#  request https:
#
#  [Alik] [so/q/31552627] [cc by-sa 3.0]

它也有網頁版 http://cht.sh/curl

image.png

有了tldr和cheat,再也不用記那麼多命令行參數了。

win下執行命令行的工具我常用cmder,如果你用的win10,可以嘗試下微軟最新發布的Terminal  https://github.com/microsoft/Terminal

 還有哪些更實用的命令行工具,可以留言分享下。

打卡送書活動

活動介紹:自律改變自我!打卡送書活動啟動!

活動獎品:技術書籍 × 7

贊助商:清華大學出版社

《Java項目開發全程實錄(第4版)》是屢獲殊榮、經久不衰的暢銷書《Java從入門到精通》的升級進階版。集Java核心技術、Java高級編程、Java項目開發,Java視頻教程於一體。

本公眾號全部博文已整理成一個目錄,請在公眾號裡回復「m」獲取!

推薦閱讀:

Linux下查看壓縮文件內容的 10 種方法

警告!你的隱私正在被上億網友圍觀偷看!

Vim高手,從來不用滑鼠

5T技術資源大放送!包括但不限於:C/C++,Linux,Python,Java,PHP,人工智慧,單片機,樹莓派,等等。在公眾號內回復「1024」,即可免費獲取!!

相關焦點

  • 如何在Linux中用命令行工具管理KVM虛擬環境
    在我們KVM系列專題的第四部分,我們將會一起討論下在命令行界面下來管理KVM環境。我們分別用『virt-install』和virsh命令行工具來創建並配置虛擬機和存儲池,用qemu-img命令行工具來創建並管理磁碟映像。在這篇文章裡沒有什麼新的概念,我們只是用命令行工具重複之前所做過的事情,也沒有什麼前提條件,都是相同的過程,之前的文章我們都討論過。第一步: 配置存儲池Virsh命令行工具是一款管理virsh客戶域的用戶界面。vi
  • Mac OS X中的Gmail命令行工具Mutt
    新的一天,我決定開始在命令行上做一項新的任務。在使用Twitter CLI設置把自己出賣了並在不久放棄後(我覺得命令行降低了Twitter的魅力所在),我轉移到了Gmail上,我對於快速發送和接收電子郵件的前景感到興奮,而且很少打斷開發流程。介紹一下Mutt,這個令人驚訝的命令行電子郵件客戶端讓一切回到了1998年!
  • 每天學一個 Linux 命令(15):man
    命令簡介man 命令用於查看、顯示 Linux 中命令的幫助信息,顯示的幫助信息,可上下滾動,搜索特定文本的出現以及其他有用的功能。man 命令是 Linux 系統下的幫助命令,通過 man 命令可以查看 Linux 系統中的命令幫助、配置文件幫助和編程幫助等信息,並且格式化顯示出來所有的信息。
  • 命令行基礎工具的更佳替代品
    命令行聽起來有時候會很嚇人,特別是在剛剛接觸的時候,你甚至可能做過有關命令行的噩夢。
  • 【松勤軟體測試基礎】Linux | 常用命令之bc、man、shutdown...
    一、bc (Linux系統下的一個簡單計算器)在windows系統裡,裡面自帶了一個計算器的工具,我們可以用其進行各種計算,在Linux系統下也有一個簡單的計算器功能,我們通過 bc 命令就可以用其來進行一些簡單的計算。
  • 18個Linux命令行工具
    基於此原因,我們已編寫了最常使用的18個命令行工具列表,這些工具將有助於每個Linux/Unix 系統管理員的工作。這些命令行工具可以在各種Linux系統下使用,可以用於監控和查找產生性能問題的原因。這個命令行工具列表提供了足夠的工具,您可以挑選適用於您的監控場景的工具。
  • Ubuntu Server 第二章 命令行基礎
    第二章 命令行基礎說明,此筆記來源於苑老師的Ubuntu Server入門到精通在企業Linux Server 壞境中,經常是沒有圖形環境,只有一個終端窗口(黑乎乎的命令行和閃動的提示符)。Linux的前身是Unix,保留著很多Unix下的使用習慣,所以大多數命令也適用於其他UNIX系統,比如我比較熟悉的思科路由器底層。
  • JVM 常用命令行工具
    Java 開發人員肯定都知道 JDK 的 bin 目錄下有許多小工具,這些小工具除了用於編譯和運行 Java 程序外,打包、部署、籤名、調試、監控、運維等各種場景都可能會見到它們的影子本文主要介紹的是用於監視虛擬機運行狀態和進行故障處理的工具,根據軟體可用性和授權的不同,可以分成三類:二、虛擬機進程狀況工具
  • Logcat命令行工具
    這裡先說一下logcat先給官方定義Logcat 是一個命令行工具,用於轉儲系統消息日誌,包括設備拋出錯誤時的堆棧軌跡,以及從您的應用中使用 Log 類寫入的消息。代碼位置:system/core/logcat/,可執行文件位於:/system/bin/logcat,每次執行adb logcat命令後,系統會新起一個logcat進程,用來處理命令,父進程是adbd進程。adb logcat命令退出後,進程退出。
  • 用Click編寫Python命令行工具
    這意味著很大一部分Python代碼被編寫為腳本和命令行界面(CLI)。構建這些命令行界面和工具是非常強大的,因為它使得幾乎所有的東西都可以自動化。 因此,隨著時間的推移,CLI可能變得相當複雜。通常從一個非常簡單的腳本開始,運行這些python代碼來完成一件特定的事情。例如:訪問web API並將輸出列印到控制臺:
  • 如何在Linux命令行中優化和壓縮JPEG或PNG圖像
    但是,這裡有兩個簡單的命令行實用程序可以優化圖像,它們是:使用這兩個工具,您可以一次優化單個或多個圖像。從命令行壓縮或優化JPEG圖像jpegoptim是一個命令行工具,可用於優化和壓縮JPEG,JPG和JFIF文件,而不會損失其實際質量。
  • Go 語言實戰:命令行(3)CLI 框架
    經過前面兩期的介紹,相信大家已經可以寫簡單的命令行程序,並且能夠使用命令行參數。即使遇到一些困難,建立直觀認識和了解關鍵詞之後,在網絡上搜索答案也變得相對容易。接下來介紹 CLI 框架。命令行程序的前兩期:命令行框架 對於簡單的功能,單個 go 文件,幾個函數,完全是足夠的。沒有必要為了像那麼回事,硬要分很多個包,每個文件就兩行代碼。為了框架而框架,屬於過早優化。
  • 20個命令行工具監控 Linux 系統性能
    為此,我們編寫了對於 Linux/Unix 系統管理員非常有用的並且最常用的20個命令行系統監視工具。這些命令可以在所有版本的 Linux 下使用去監控和查找系統性能的實際原因。這些監控命令足夠你選擇適合你的監控場景。
  • Node.js 中如何收集和解析命令行參數
    前言  在開發 CLI(Command Line Interface)工具的業務場景下,離不開命令行參數的收集和解析。  接下來,本文介紹如何收集和解析命令行參數。收集命令行參數  在 Node.js 中,可以通過 process.argv 屬性收集進程被啟動時傳入的命令行參數:  // .
  • sqlite-utils:用於構建SQLite資料庫的Python庫和命令行工具
    它是我的Datasette項目構建的工具生態系統的一部分。我花了整個周末為它添加各種令人興奮的命令行選項,現在已經準備完畢。一個用於快速創建資料庫的python庫Datasette背後的一個核心思想是,SQLite是發布各種有趣的結構化數據的理想格式。
  • 六個優雅的 Linux 命令行技巧
    一些非常有用的命令能讓命令行的生活更滿足使用 Linux 命令工作可以獲得許多樂趣,但是如果您使用一些命令,它們可以減少您的工作或以有趣的方式顯示信息時
  • 最常用的20個監控Linux系統性能的命令行工具
    為此,我們編寫了對於 Linux/Unix 系統管理員非常有用的並且最常用的20個命令行系統監視工具。這些命令可以在所有版本的 Linux 下使用去監控和查找系統性能的實際原因。這些監控命令足夠你選擇適合你的監控場景。
  • Chepy:一款基於CyberChef工具的Python庫&命令行實現
    Chepy是一款基於CyberChef工具的Python庫&命令行實現,它是一個Python庫/命令行,實現了跟CyberChef工具相同的功能
  • 適用於linux的5個最佳命令行歸檔工具
    # tar -zcvf name_of_tar.tar.gz /path/to/folder解壓一個 tar 存檔文件。# shar file_name.extension > filename.shar提取一個shar歸檔文件。
  • KITT-Lite:基於Python實現的輕量級命令行滲透測試工具集
    KITT滲透測試框架-輕量級版本KITT滲透測試框架是一種基於Python實現的輕量級命令行滲透測試工具集,本質上上來說,它就是一個針對滲透測試人員設計的開源解決方案