Follow our public account for Java resourcesDelivered promptly
Use new alternative tools to improve old command line tools.
In daily use of Linux/Unix systems, we need to use many command line tools to get work done and to 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 their original ideas, appearance, and feel.
These tools are excellent and essential for many system administrators’ workflows. However, in recent years, the open-source community has developed many more advantageous alternative tools. While some tools are flashy but impractical, others significantly enhance usability and have become excellent choices for modern systems. In this article, I will introduce five alternatives to standard Linux command line tools.
Alternative to du: ncdu
NCurses Disk Usage (ncdu) provides similar results to du, but its curses-based interactive interface can show directories that consume most disk space.
First, ncdu takes some time to analyze the disk and then displays directories or files in the order of usage, 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
You can navigate to each result using the arrow keys. If you press Enter on a 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 taking up the most disk space. Use the left arrow key to return to the previous directory. By default, you can press the d key to delete files, but ncdu will ask for confirmation before deleting files. If you want to prevent deletion 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 repository on Fedora using dnf:
$ sudo dnf install ncdu
For more information about this tool, see: https://dev.yorhel.nl/ncdu. Also, all Linux series interview questions and answers have been compiled. Search for Java Tech Stack on WeChat and send: interview to read online.
Alternative to top: htop
htop is an interactive process viewer similar to top, providing a better user experience. By default, htop shows the same metrics as top, but in a nice 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 viewer.
If you want to learn more about this project, check out the htop homepage (https://hisham.hm/htop/).
Alternative to man: tldr
The tldr command line tool displays simplified command documentation, most of which includes 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 source of information for many tools. However, in some cases, the content of man is too extensive. 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 nearly 3,000 lines long. In contrast, the tldr for curl is only 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
TLDR stands for “too long; didn’t read,” a popular internet phrase indicating an article is too lengthy. This name is very fitting for this summary tool because man pages are indeed very practical but often 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. Recommended: A Spring Boot basic tutorial and practical examples:https://github.com/javastacks/spring-boot-best-practice
Alternative to sed/grep for finding JSON data: jq
jq is a command line processor for JSON. It works like sed or grep, but is primarily for processing JSON data. If you are a developer or system administrator who regularly works with JSON, 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 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"
}
}
If you use grep to directly query for name, the result would be:
$ grep name k8s-pod.json
"name": "myapp",
"namespace": "project1"
"name": "busybox"
"name": "nginx",
grep returns all lines containing the word name. You could add some other options to grep to limit the query results and use regular expressions to find the container names. When using jq to get the desired result, you only need to use an expression that simulates navigating down the data structure, as shown below:
$ 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 result even if the file format changes slightly. However, for grep and sed, even slight format changes can yield completely different results.
jq has many more features, and I would need to write another article to cover all of them. 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 can be 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. Generally, 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 would be as follows:
$ find . -iname "*.md"$ find . -iname "*.md"
While the following fd search can return the same results:
$ fd .md
In some cases, fd may require additional options. For example, if you want the search to include hidden files and directories, you must use the -H option, while find does not require this.
fd is available on many Linux distributions. To install fd in Fedora, you can use the standard 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 to use old tools, especially when connecting remotely to servers, new alternative tools provide additional benefits and are helpful in many cases, especially in helping me manage and work on Linux desktops and laptops.
Do you use other tools to handle your workflow? Please leave a comment below. Also, follow the Java Tech Stack public account and reply: interview to get my compiled Linux series interview questions and answers, which are very comprehensive.
Original: https://opensource.com/article/20/6/modern-linux-command-line-tools Author: Ricardo Gerardi Translator: Wan Yue, Editor: Tu Min Produced by: CSDN (ID: CSDNnews)





Follow Java Tech Stack for more resources
