Home  >  Article  >  System Tutorial  >  Share 5 modern alternatives to basic Linux command line tools

Share 5 modern alternatives to basic Linux command line tools

WBOY
WBOYforward
2024-02-05 12:10:12930browse

When we use Linux/Unix systems daily, we often use various command line tools to manage the system and understand the operation of the system. For example, the du command is used to monitor disk utilization, and the top command is used to display system resources. Some of these tools have been around for a long time, like the top command, which was first released in 1984, and the du command, which was first released in 1971.

After years of development, these tools have gradually evolved from their original forms into modern versions and been ported to different systems. However, despite modern changes, these tools still retain their original ideas and user experience.

These tools are very important and indispensable to the system administrator's workflow. However, over the past few years, the open source community has developed alternative tools that bring additional benefits. Some of the new tools may be flashy, but others greatly improve usability, making them the best choice on modern systems. Here are five alternatives to standard Linux command line tools.

1. ncdu as a replacement for du

NCurses The disk utilization (ncdu) tool provides du-like functionality, but in the form of a curses-based interactive interface. It mainly focuses on directories that take up more disk space.

Use ncdu, it will take some time to analyze the disk and display the results based on the directories or files you use frequently, as shown below:

ncdu 1.14.2 ~ Use the arrow keys to navigate, press ? for help
--- /home/rgerardi ------------------------------------------------------------
   96.7 GiB [##########] /libvirt
   33.9 GiB [###       ] /.crc
    7.0 GiB [          ] /Projects
.   4.7 GiB [          ] /Downloads
.   3.9 GiB [          ] /.local
    2.5 GiB [          ] /.minishift
    2.4 GiB [          ] /.vagrant.d
.   1.9 GiB [          ] /.config
.   1.8 GiB [          ] /.cache
    1.7 GiB [          ] /Videos
    1.1 GiB [          ] /go
  692.6 MiB [          ] /Documents
. 591.5 MiB [          ] /tmp
  139.2 MiB [          ] /.var
  104.4 MiB [          ] /.oh-my-zsh
   82.0 MiB [          ] /scripts
   55.8 MiB [          ] /.mozilla
   54.6 MiB [          ] /.kube
   41.8 MiB [          ] /.vim
   31.5 MiB [          ] /.ansible
   31.3 MiB [          ] /.gem
   26.5 MiB [          ] /.VIM_UNDO_FILES
   15.3 MiB [          ] /Personal
    2.6 MiB [          ]  .ansible_module_generated
    1.4 MiB [          ] /backgrounds
  944.0 KiB [          ] /Pictures
  644.0 KiB [          ]  .zsh_history
  536.0 KiB [          ] /.ansible_async
 Total disk usage: 159.4 GiB  Apparent size: 280.8 GiB  Items: 561540

Use the arrow keys to navigate to each entry. If you press Enter on a directory entry, ncdu will display the contents of that directory:

--- /home/rgerardi/libvirt ----------------------------------------------------
                         /..
   91.3 GiB [##########] /images
    5.3 GiB [          ] /media

You can use this to drill down into directories and find which files are taking up the most disk space, using the left arrow key to go back one level up. By default, you can use ncdu to delete files by pressing the d key, and it will confirm the deletion request with you before deleting. If you want to disable this behavior to prevent accidents, use the -r option for read-only access: ncdu -r.

ncdu is available on many platforms and Linux distributions. For example, you can install it on Fedora directly from the official repository using dnf:

$ sudo dnf install ncdu

To learn more about this tool, check out the ncdu page.

https://dev.yorhel.nl/ncdu

2htop as a replacement for top

htop is an interactive process viewer similar to top, but it provides a better user experience out of the box. By default, htop displays the same indicators as top in pleasant colors.

By default, htop is like this:

Share 5 modern alternatives to basic Linux command line tools

In contrast, top defaults to this:

Share 5 modern alternatives to basic Linux command line tools

In addition, htop provides system overview information at the top and a command bar at the bottom. You can use function keys to trigger commands. Moreover, you can also press F2 to enter the settings interface to customize it. You can change its color, add or remove indicators, or change the overview bar's display options.

Although with the latest version of top, you can also achieve similar results through configuration, htop provides a more reasonable default configuration, which makes it a beautiful and easy-to-use process viewer.

To learn more about this project, check out the htop home page.

https://hisham.hm/htop/

3 tldr as a replacement for man

tldr command line tool displays simplified command usage information, mainly including examples. It serves as a client for the community project tldr pages.

This tool cannot replace man. Man pages remain a canonical and complete source of information for many tools. However, in some cases, man provides too much information. Sometimes, you don't need all the information about a command; you just try to remember the basic options. For example, the curl command's man page is almost 3000 lines long. In contrast, curl's tldr page is only 40 lines long and looks like this:

$ tldr curl
# curl
  Transfers data from or to a server.
  Supports most protocols, including HTTP, FTP, and POP3.
  More information: .
- 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 OUTPUT

TLDR is the abbreviation of Internet slang “too long; didn’t read”, which refers to a summary of a very long article. This name is appropriate for this tool because man pages, while useful, are sometimes too long.

In Fedora, the tldr client is written in Python. You can install it using dnf. To learn about other client options, see the tldr pages project page.

https://tldr.sh/

Generally speaking, tldr tools require access to the Internet to consult tldr pages. The Python client in Fedora allows you to download and cache these pages for offline access.

To learn more about tldr, you can use tldr tldr.

4jq as a sed/grep replacement

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 提供的全部功能;相反,它提供了一些合理的默认值,在某些情况下非常有用。

例如,在包含 Git 存储库的目录中搜索源代码文件时,fd 会自动排除隐藏的文件和目录,包括. .git 目录,并忽略.gitignore 文件中的模式。一般来说,它的搜索速度更快,而且第一次搜索时提供了更多相关的结果。

默认情况下,fd 在当前目录中执行不区分大小写的模式搜索,输出是彩色的。使用 find 进行相同的搜索需要你提供额外的命令行参数。例如,在当前目录中搜索所有的 markdown 文件(.md 或.MD ),find 命令如下:

$ find . -iname "*.md"

使用 fd 进行同样的搜索:

$ fd .md

在某些情况下,fd 需要额外的选项;例如,如果你想包含隐藏文件和目录,则必须使用选项 -H,而这在 find 中是不需要的。

fd 可用于许多 Linux 发行版。执行以下命令,可以使用标准存储库在 Fedora 中安装:

$ sudo dnf install fd-find

要了解更多信息,请查阅 fd GitHub 存储库。

https://github.com/sharkdp/fd/

6其他替代品

虽然我仍然经常使用所有旧的基本工具,特别是在远程连接到服务器时,但这些替代品额外提供了一些在许多场景中都非常有用的好处。特别是,它们对我在 Linux 台式机和笔记本电脑上的工作帮助很大。

你的工作流中是否使用了其他工具?欢迎在下面的评论区补充。

7作者介绍

Ricardo Gerardi 是加拿大红帽公司的高级顾问,他专门从事基于 Ansible 和 Openshift 的 IT 自动化。他有丰富的电信行业经验,曾在 TELUS 担任高级架构师,并曾在 IBM 巴西和 IBM 加拿大担任网络管理解决方案的高级顾问和售前专家长达 13 年。Ricardo 是一个超过 20 年的 Linux 爱好者。他目前感兴趣的是用 Go 编程来破解东西……

The above is the detailed content of Share 5 modern alternatives to basic Linux command line tools. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete