5个Linux常用命令的现代化替代品,用着超赞!( 二 )


$ tldr curl# curlTransfers data from or to a server.Supports most protocols, including HTTP, FTP, and POP3.More information: <https://curl.haxx.se>.- Download the contents of an URL to a file:curl http://example.com -o filename- Download a file, saving the output under the filename indicated by the URL:curl -O http://example.com/filename- Download a file, following [L]ocation redirects, and automatically [C]ontinuing (resuming) a previous file transfer:curl -O -L -C - http://example.com/filename- Send form-encoded data (POST request of type `application/x-www-form-urlencoded`):curl -d 'name=bob' http://example.com/form- Send a request with an extra header, using a custom HTTP method:curl -H 'X-My-Header: 123' -X PUT http://example.com- Send data in JSON format, specifying the appropriate content-type header:curl -d '{"name":"bob"}' -H 'Content-Type: application/json' http://example.com/users/1234... TRUNCATED OUTPUTTLDR 是网络俚语“too long; didn't read”的缩写 , 指对一篇很长的文章进行总结 。这个名称很适合这个工具 , 因为 man 页面虽然有用 , 但有时太长了 。
在 Fedora 中 , tldr 客户端是用 Python/ target=_blank class=infotextkey>Python 编写的 。你可以使用 dnf 安装它 。要了解其他客户端选项 , 请参阅 tldr pages 项目页面 。
https://tldr.sh/
一般来说 , tldr 工具需要访问互联网以查阅 tldr pages 。Fedora 中的 Python 客户端允许你下载并缓存这些页面以供脱机访问 。
要了解有关 tldr 的更多信息 , 可以使用 tldr tldr 。
4作为 sed/grep 替代品的 jq
jq 是一个命令行 JSON 处理器 , 类似于 sed 或 grep , 但专门设计用于处理 JSON 数据 。如果你是在日常任务中会用到 JSON 的开发人员或系统管理员 , 那么这是你工具箱中必不可少的工具 。
与 grep 和 sed 等通用文本处理工具相比 , jq 的主要优点是它理解 JSON 的数据结构 , 允许使用单个表达式创建复杂的查询 。
举例来说 , 假设你试图在这个 JSON 文件中查找容器的名称:
{"apiVersion": "v1","kind": "Pod","metadata": {"labels": {"app": "myapp"},"name": "myapp","namespace": "project1"},"spec": {"containers": [{"command": ["sleep","3000"],"image": "busybox","imagePullPolicy": "IfNotPresent","name": "busybox"},{"name": "Nginx","image": "nginx","resources": {},"imagePullPolicy": "IfNotPresent"}],"restartPolicy": "Never"}}如果你直接使用 grep 查找 name , 则写法如下:
$ grep name k8s-pod.json"name": "myapp","namespace": "project1""name": "busybox""name": "nginx",grep 返回包含单词 name 的所有行 。你可以向 grep 添加更多选项来限制它 , 借助一些正则表达式操作来找到容器的名称 。如果是使用 jq 获得你想要的结果 , 则可以使用一个表达式来模拟向下导航数据结构 , 如下所示:
$ jq '.spec.containers[].name' k8s-pod.json"busybox""nginx"这个命令提供这两个容器的名称 。如果你只是在查找第二个容器的名称 , 则可以在表达式中添加数组元素索引:
$ jq '.spec.containers[1].name' k8s-pod.json"nginx"因为 jq 知道数据结构 , 所以即使文件格式稍有变化 , 它也能提供相同的结果 。而只要格式稍有变化 , grep 和 sed 就可能提供不同的结果 。
jq 有许多特性 , 要介绍所有这些特性的话 , 需要另写一篇文章 。要了解更多信息 , 请参阅 jq 项目页、man 页面或 tldr jq 。
https://stedolan.Github.io/jq/
5作为 find 替代品的 fd
fd 是 find 命令的一个简单而快速的替代品 。它的目的不是取代 find 提供的全部功能;相反 , 它提供了一些合理的默认值 , 在某些情况下非常有用 。
【5个Linux常用命令的现代化替代品,用着超赞!】例如 , 在包含 Git 存储库的目录中搜索源代码文件时 , fd 会自动排除隐藏的文件和目录 , 包括. .git 目录 , 并忽略.gitignore 文件中的模式 。一般来说 , 它的搜索速度更快 , 而且第一次搜索时提供了更多相关的结果 。


推荐阅读