
IT Service Circle
An IT self-media platform with warmth and attitude

Using new alternative tools to improve old command line tools.
The following is the translation:
In the daily use of Linux/Unix systems, we need to use many command line tools to complete our work and understand and manage our systems, such as using du to monitor disk usage and top to display system resources. Some tools have a long history. For example, top was first released in 1984, and du dates back to 1971.
Over the years, these tools have been modernized and ported to different systems, but overall, they still retain the original ideas, appearance, and feel.
These tools are excellent and essential for the workflow of many system administrators. However, in recent years, the open-source community has developed several more advantageous alternative tools. While some tools are flashy but impractical, others greatly enhance usability and become excellent choices for modern systems. In this article, I will introduce five alternatives to standard Linux command line tools.
Alternative to du: ncdu
The NCurses Disk Usage (ncdu) tool provides results similar to du, but its curses-based interactive interface can display the directories that consume most of the disk space.
First, ncdu takes some time to analyze the disk, then displays directories or files in the most commonly used order, as follows:
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
You can navigate to each result using the arrow keys. If you press Enter on the selected result, ncdu will display the contents of that directory:
--- /home/rgerardi/libvirt ----------------------------------------------------
/..
91.3 GiB [##########] /images
5.3 GiB [ ] /media
You can drill down into each directory to find out which files are consuming the most disk space. Use the left arrow key to return to the previous directory. By default, you can press d to delete files, but ncdu will ask for confirmation before deleting. If you want to prevent deletion operations to avoid 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 from the official code repository on Fedora using dnf:
$ sudo dnf install ncdu
For more information about this tool, see: https://dev.yorhel.nl/ncdu.
Alternative to top: htop
htop is an interactive process viewer similar to top, offering a better user experience. By default, htop displays metrics similar to top, but in a beautiful color.
The default htop looks like this:
Compared to the default top:
Additionally, htop provides an overview of the system at the top, and the command bar at the bottom displays function key commands. You can also press F2 to enter the settings page to customize the interface. In the settings page, you can change its colors, add or remove metrics, and change the display options for the overview bar.
While the latest version of top can also achieve similar results through configuration, the default configuration provided by htop is more reasonable and user-friendly as a process browser.
If you want to learn more about this project, check the htop homepage (https://hisham.hm/htop/).
Alternative to man: tldr
tldr command line tool displays simplified command documentation, and most documents provide examples. It is the client of the tldr pages project (https://tldr.sh/).
This tool is not a replacement for man. man pages are still the standard and complete information source for many tools. However, in some cases, the content of man is too lengthy. Sometimes, you don’t need all the information about a command. You just need to remember the basic options. For example, the man page for the curl command is almost 3,000 lines long. In contrast, the tldr for curl is only 40 lines, as follows:
$ 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
The full name of TLDR is “too long; didn’t read”, a popular internet phrase indicating that an article is too long. This name is very suitable for this summary tool because while man pages are very useful, their content is too long.
In Fedora, the tldr client is written in Python. You can install it using dnf. For other client options, see the tldr pages project (https://tldr.sh/).
Typically, the tldr tool requires internet access 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.
Alternative to sed/grep for finding JSON data: jq
jq is a command line processor for JSON. Like sed or grep, but primarily for handling JSON data. If you are a developer or system administrator who needs to use JSON for daily tasks, this is an essential tool in your toolbox.
The main advantage of jq compared to general text processing tools (like grep and sed) is that it understands the JSON data structure, allowing you to create complex queries with a single 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"
}
}
If you query name directly with grep, the result will be:
$ grep name k8s-pod.json
"name": "myapp",
"namespace": "project1"
"name": "busybox"
"name": "nginx",
grep will return all lines containing the word name. You can add some other options in grep to limit the query results and find the container’s name using regular expressions. When using jq to get the desired result, you only need to use an expression that simulates navigating down the data structure, as follows:
$ jq '.spec.containers[].name' k8s-pod.json
"busybox"
"nginx"
This command will return the names of the two containers. If you only want to see the name of the second container, you can add the array element index in the expression:
$ jq '.spec.containers[1].name' k8s-pod.json
"nginx"
Because jq understands the data structure, it can provide the same results even if the file format changes slightly. But for grep and sed, a slight change in format will yield completely different results.
jq has many features, and I would need to write another article to introduce all its functionalities. For more information, see the jq project page (https://stedolan.github.io/jq/), man pages, or tldr jq.
Alternative to find: fd
fd is a simple and fast alternative to the find command. Its purpose is not to replace the functionality of find, but to provide some reasonable defaults that are very useful in certain cases.
For example, when searching for source code files in a directory containing a Git repository, fd automatically excludes hidden files and directories (including the .git directory) and ignores patterns in the .gitignore file. Typically, it can query faster and provide more relevant results.
By default, fd performs a case-insensitive pattern search in the current directory and outputs colored results. When using find for the same search, you need to provide additional command line parameters. For example, to search for all markdown files (i.e., .md or .MD files) in the current directory, the find command is as follows:
$ find . -iname "*.md"$ find . -iname "*.md"
The following fd search can return the same results:
$ fd .md
In some cases, fd also requires other options. For example, if you want to include hidden files and directories in the search, you must use the -H option, while find does not require this.
fd is available on many Linux distributions. To install fd on Fedora, you can use the standard code repository:
$ sudo dnf install fd-find
For more information, please see the fd GitHub repository (https://github.com/sharkdp/fd/).
Excellent alternative tools and time-tested utilities
Although I still prefer using old tools, especially when remotely connecting to servers, new alternative tools offer extra benefits and are helpful in many cases. They especially help me manage and work on Linux desktops and laptops.
Do you use other tools to handle your workflow? Please leave a comment below.
Original article: https://opensource.com/article/20/6/modern-linux-command-line-tools
This article is a translation by CSDN, please indicate the source when reprinting.
-
What is the most tsundere website you’ve ever seen?
-
Various practical uses of virtual device files in Linux systems
-
Don’t upgrade to iOS 14! The bugs are so numerous that it’s heartbreaking, and it’s already a massive failure
-
Windows 10 releases emergency security patches, please update as soon as possible!
*Copyright Statement: Articles and images reproduced are from the public network, and the copyright belongs to the author. Unless we cannot confirm the source, we will indicate the author and source for the articles pushed. If the source is incorrect or infringes the rights of the original author, please contact us for deletion or authorization matters.