Improve old command line tools with new alternatives.

In daily use of Linux/Unix systems, we need to use a number of command-line tools to get things done, as well as to understand and manage our systems, such as using DU to monitor disk utilization and top to display system resources. Some tools have a long history. For example, TOP was first published in 1984, while DU dates back to 1971.

These tools have been modernized and ported to different systems over the years, but by and large they retain the original idea, look and feel.

These tools are excellent and essential to many systems administrator workflows. But in recent years, the open source community has developed a number of more advantageous alternatives. While some tools are gimmicky, others have greatly improved their usefulness and are excellent choices for modern systems. In this article, I present five alternatives to standard Linux command-line tools.

Alternatives to DU: NCDU

The NCurses DiskUsage (OR NCDU) tool provides similar results to DU, but its Curses-based interactive interface shows directories that are consuming most of the disk space.

First Ncdu needs to spend some time analyzing the disk and then display the directories or files in the most common order, as follows:

Ncdu 1.14.2 ~ Use the arrow keys to navigate, press? For help - / home/rgerardi -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 96.7 GiB [# # # # # # # # # #] / libvirt CRC 7.0 GiB [] /Projects. 4.7 GiB [] /Downloads. 3.9 GiB [] /.local 2.5 GiB [] /.minishift 2.4 GiB [] /.vagrant.d.1.9gib [] /.config.1.9gib [] /.cache 1.9gib [] /.cache 1.9gib [] /Videos 1.1gib [] /go 692.6mib [] /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 [] /Pictures 644.0kib [].zsh_history 536.0kib [] /. Ansible_async Total Disk Usage: 159.4 GiB Apparent size: 280.8 GiB Items: 561540Copy the code

You can navigate to each result using the arrow keys. If you press Enter on the selected result, NCDU will display the contents of the directory:

--- /home/rgerardi/libvirt ---------------------------------------------------- /.. 91.3 GiB [##########] /images 5.3 GiB [] /mediaCopy the code

You can drill down into each directory to find out which files are taking up the most disk space. Use the left arrow key to return to the previous directory. By default, you can delete files by pressing d, but NCDU will ask for confirmation before deleting files. If you want to disable delete operations to prevent accidents, you can set read-only access with the -r option: ncdu -r.

Ncdu is available on many platforms and Linux distributions. For example, you can install it directly on Fedora using DNF from the official code base:

$ sudo dnf install ncdu
Copy the code

For more information about the tool, see dev.yorhel.nl/ncdu.

Alternatives to top: htop

Htop is an interactive process browser similar to Top that provides a better user experience. By default, hTOP displays the same metrics as TOP, in nice colors.

The default hTOP is as follows:

Compared to the default top:

In addition, the top of htop provides an overview of the system, and the command bar at the bottom shows the function key commands. You can also press F2 to enter the Settings page to customize. From the Settings page, you can change its color, add or remove metrics, and change the display options for the overview bar.

While the latest version of Top can be configured to achieve similar results, hTOP provides a more reasonable default configuration and is easier to use as a process browser.

For more information about the project, check out the HTOP home page (hisham.hm/ hTOP /).

Spring Boot: github.com/javastacks/…

Alternative to MAN: TLDR

The TLDR command line tool displays command documentation that can be simplified, and most documents provide examples. It is the client for the TLDR Pages project (TLdr. sh/).

This tool is not a replacement for MAN. Man Pages remains the specification for many tools and a complete source of information. But, in some cases, man is too much. Sometimes, you don’t need all the information about a command. You just have to remember the basic options. For example, the man page of the curl command has almost 3,000 lines. By comparison, Curl’s TLDR is just 40 lines, as shown below:

$ tldr curl

# curl
  Transfers 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 OUTPUT
Copy the code

TLDR stands for “Too long; Didn’t tread “(too long; Unread), a catchphrase on the Internet that means the article is too long. The name is a good fit for the summary tool because ManPages, while useful, is too long.

In Fedora, the TLDR client is written in Python. You can install using DNF. For additional client options, see the TLDR Pages project (TLdr.sh /).

Typically, TLDR tools need to access the Internet to query TLDR pages. The Python client in Fedora allows you to download and cache these pages for offline access.

For more information about TLDR, run TLDR TLDR. In addition, Linux series of interview questions and answers are all sorted out, wechat search Java technology stack, sent in the background: interview, you can read online.

Find JSON data with sed/grep instead: jq

Jq is the command line processor for JSON. Like sed or grep, but mostly for JSON data processing. If you’re a developer or system administrator who needs to use JSON for your daily tasks, this is an essential tool in your toolbox.

The main advantage of JQ over general-purpose text processing tools such as grep and sed is that it understands JSON data structures and you can create complex queries from an expression.

For example, suppose you are trying to find the name of a container in the following JSON file:

{
  "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"
  }
}
Copy the code

If you query name directly with grep, the result is:

$ grep name k8s-pod.json
        "name": "myapp",
        "namespace": "project1"
                "name": "busybox"
                "name": "nginx",
Copy the code

Grep returns all lines containing the word name. You can add some other options in grep to restrict the query results and find the container name through regular expression operations. To get the desired result using JQ, you just need to use an expression that simulates the downward navigation data structure, as follows:

$ jq '.spec.containers[].name' k8s-pod.json
"busybox"
"nginx"
Copy the code

This command returns the names of both containers. If you only want to see the name of the second container, you can add an array element index to the expression:

$ jq '.spec.containers[1].name' k8s-pod.json
"nginx"
Copy the code

Because JQ understands data structures, it can provide the same results even if the file format changes slightly. But for grep and SE, a slight change in format can provide completely different results.

Jq has so many more features that I’ll need to write another article to cover all of them. For more information, see the JQ Project page (Stedolan.github. IO/jQ /), Manpag… Jq.

Alternative to find: fd

Fd is a quick and easy alternative to the find command. It is not intended to replace the functionality of Find, but rather to provide reasonable defaults that can be useful in some cases.

For example, when searching for source files in a directory containing a Git code base, FD automatically excludes hidden files and directories (including the.git directory) and ignores patterns in the.gitignore file. In general, it enables faster queries and provides more relevant results.

By default, fd performs a case-insensitive pattern search against the current directory and outputs colored results. For the same search using find, you need to provide additional command-line arguments. For example, search all markdown files in the current directory (i.e..md or. MD file), the find command is as follows:

$ find . -iname "*.md"$ find . -iname "*.md"
Copy the code

The following fd search returns the same result:

$ fd .md
Copy the code

In some cases, fd also requires other options. For example, if you want your search to include hidden files and directories, you must use the -h option, while find does not.

Fd is available with many Linux distributions. To install fd in Fedora, use the standard codebase:

$ sudo dnf install fd-find
Copy the code

For more information, see fd’s GitHub codebase (github.com/sharkdp/fd/…

Excellent alternative tools with proven utilities

While I’m still used to using the old tools, especially when remotely connecting to a server, the new alternatives offer additional benefits and can be very helpful in many cases. In particular, it helps me manage and work on Linux desktops and laptops.

Do you use other tools to handle workflow? Leave a comment below.

Original text: opensource.com/article/20/… Translated by Ricardo Gerardi and edited by Tu Min CSDN (ID: CSDNnews)

Recent hot articles recommended:

1.1,000+ Java Interview Questions and Answers (2021)

2. Don’t use if/ else on full screen again, try strategy mode, it smells good!!

3. Oh, my gosh! What new syntax is xx ≠ null in Java?

4.Spring Boot 2.5 is a blockbuster release, and dark mode is exploding!

5. “Java Development Manual (Songshan version)” the latest release, quick download!

Feel good, don’t forget to click on + forward oh!