Home > Article > System Tutorial > Interesting treasure! 6 Unique and Amazing Linux Utilities for You
In this article, we'll dig deeper into the hidden corners of /usr/bin and uncover some more interesting treasures. We'll explore some unique and interesting programs that may seem outdated at first glance, but are actually still very useful today.
Let's get back on the road of adventure and continue to discover more wonderful things that Linux has to offer us.
First of all, we have a very useful little tool that can help you wrap input lines to a specific length. You can define the length by specifying the number of bytes or spaces. Using the fold tool, you can quickly process files with different lengths.
For example, assume we have a line of input that is six characters long. We want to limit each line to only five characters and wrap the remainder. Using fold, we can achieve this using the following command:
┌──(linuxmi㉿linuxmi)-[~/www.linuxmi.com] └─$ echo "12345678" | fold -w 7
The corresponding output should be:
1234567 8
Now we can quickly fit some text into our length limit. This is useful for breaking up long text streams or for enforcing line length limits on code or other configuration files.
For more details on using fold, check out the wiki page.
This is another very useful formatting tool. The column tool can help you create columns in text output or even generate entire tables, all from the command line.
Although the same functionality can be achieved using tools such as awk, the column tool is designed for this specific purpose, so it is very simple to use, and its syntax is easy to remember.
If we want to build a simple table based on a few lines of input, we can execute the following command:
┌──(linuxmi㉿linuxmi)-[~/www.linuxmi.com] └─$ echo -e "one two three\n1 2 3\n93139 777777 999999" | column -t
The output of the command should look like this:
one two three 1 2 3 93139 777777 999999
As you can see, the output is automatically formatted into neatly aligned columns. This forms a small table in the output, which automatically resizes based on the length of each line of input.
This tool will be a big help if you are working with a slightly longer unstructured data set on the command line and want to quickly create some tabular forms.
column's man page provides more usage details and unique ways of handling different inputs.
You may have heard of the newgrp command. This command executes commands as a different group, but there is a simpler utility that achieves the same functionality. The sg utility allows you to directly execute commands with the permissions of another group you specify. You don't need to use pipes or change existing shell groups, just specify a group and a command.
To execute the ls command with the permissions of the sudo group, you can enter the following command:
┌──(linuxmi㉿linuxmi)-[~/www.linuxmi.com] └─$ sg sudo ls
This will switch the ls command to run with the permissions of the sudo group. Once the command has finished executing, you will be returned to the normal group permissions you had before execution.
Thesg command is very helpful for testing new group permissions or quickly switching context to run a program from another group.
The xxd utility is one of many ways to perform a hex dump on Linux. There are many utilities with similar functionality, but the xxd program is slightly different. It has the added advantage that you can use this utility to do hex dumps and restores. There are many configurable flags and you can also perform patching operations on binaries.
Suppose we want to take a hex dump of the following file named linuxmi:
linuxmi
我们只需提供输入,xxd 将自动将文件编码到 stdout(对于较短的输入文件,这是一个很方便的默认功能):
┌──(linuxmi㉿linuxmi)-[~/www.linuxmi.com] └─$ xxd linuxmi 00000000: 6c69 6e75 786d 690a
您还可以通过传递一个额外的文件名参数来直接将输出发送到转储文件:
xxd linuxmi 93139
这将将十六进制转储发送到名为 93139 的文件中。
xxd 的 man 页面可以在此处找到。
这个方便的小实用程序来自于我们都熟悉和喜爱的古老 ps 实用程序家族。pwdx 实用程序可以让您获取运行中进程的当前工作目录。您只需要将进程的 PID 传递给它,它就会告诉您该进程的工作目录在哪里。
假设我们想找出 cron 进程在我们的机器上的工作目录。首先,我们只需要使用 ps 搜索并找到它的 PID,像这样:
┌──(linuxmi㉿linuxmi)-[~/www.linuxmi.com] └─$ ps aux | grep cron
在这里,我们可以看到cron的PID是612。现在,我们只需要将该进程的PID传递给pwdx来确定其工作目录,像这样:
┌──(linuxmi㉿linuxmi)-[~/www.linuxmi.com] └─$ sudo pwdx 691
如下图:
由于cron是系统进程,您需要使用sudo才能获取有关它的信息。命令完成后,我们得到了cron的当前工作目录,即 /var/spool/cron。
这可以是一个非常有价值的故障排除工具,特别是当您追踪目录范围问题时。通过使用 pwdx 进行快速检查,您可以准确地确定一个进程认为它应该从哪个位置运行。
在这里查看 pwdx 的 man 页面。
这个强大的小程序可能不会像您一开始想的那样执行某个特定任务。它在 Linux 中已经存在了几十年,甚至可以追溯到 1975 年 Unix 的第 6版。
write 实用程序实际上允许您向同一系统上的其他用户发送消息。您可以针对任何其他登录的用户发送消息。提供用户名,您将进入一个交互式shell,以向他们写任何您想要的文本。您键入的所有内容(包括换行符)都将出现在目标用户的控制台上。
这里是一个快速示例:
write
这将使您进入一个交互式控制台,以向相应的用户发送消息。请记住,这是一种相当侵入性的与其他用户通信的方式。这将使他们的终端显示您输入的文本,而无需任何警告或提示。对他们来说,这将出现在他们的终端上,就像自动出现的信息一样。由于这是单向通信,他们也无法回复。
尽管现在有更好的方法来处理用户之间的消息传递,但这是计算历史的一部分。我相信今天仍然可以有一些创造性的用途。
The above is the detailed content of Interesting treasure! 6 Unique and Amazing Linux Utilities for You. For more information, please follow other related articles on the PHP Chinese website!