Home  >  Article  >  Operation and Maintenance  >  Organize and share fifteen practical Linux tips (summary sharing)

Organize and share fifteen practical Linux tips (summary sharing)

WBOY
WBOYforward
2022-01-20 17:44:062586browse

This article will share with you some very practical tips. Students who are familiar with Linux systems know that its efficiency is mainly reflected in the command line. Through the command line, many simple commands can be freely combined to obtain very powerful functions. I hope everyone has to help.

Organize and share fifteen practical Linux tips (summary sharing)

The command line also means that you canautomate. Automation will make your work more efficient, free up a lot of manual operations, and give you more Time to do more meaningful things.

This article will share some very practical tips. I hope it can help you improve your work efficiency and you will be able to use them after learning them!

Organize and share fifteen practical Linux tips (summary sharing)

1. How to quickly clear a file

There are N ways to quickly clear a file. I prefer the one below because it is the shortest

$ > access.log

but not satisfying? Well, let me also summarize the other most common methods of clearing files

  • : > access.log

  • true > access.log

  • ##cat /dev/null > access.log

  • echo -n "" > access.log

  • ##echo > access.log

  • truncate -s 0 access.log

  • For a brief explanation,
:

is a The built-in command means no-op, which probably means an empty statement, so the usage of : means that after executing the command, nothing will be output and the empty content will be overwritten into the file.

2. Quickly generate large files

Sometimes, on Linux, we need a large file for testing upload or download Speed, you can quickly generate a large file through the

dd

command <pre class="brush:sql;toolbar:false;">$ dd if=/dev/zero of=file.img bs=1M count=1024</pre>The above command generates a file named file.img with a size of 1G.

3. Securely erase hard drive data

Introducing a method of erasing hard drive data that is efficient and safe. It can be easily achieved through the

dd

command: <pre class="brush:sql;toolbar:false;">$ dd if=/dev/urandom of=/dev/sda</pre>Use

/dev/urandom

to generate random data and write the generated data to the sda hard disk , which is equivalent to safely erasing the hard disk data. If Mr. Chen had learned this command back then, there might not have been the Yan Zhaomen Incident.

4. Quickly create a system disk

To create a system disk under Linux, the tools of Laomaotaoshen are too weak, just one command Done:

$ dd if=ubuntu-server-amd64.iso of=/dev/sdb

Haha, isn’t it great?

sdb

It can be a U disk or an ordinary hard disk

5. View The running time of a certain process is

possible. Most students will only use

ps aux

. In fact, you can specify that only specific ones will be displayed through the -o parameter. A certain field will get clearer results. <pre class="brush:sql;toolbar:false;">$ ps -p 10167 -o etimes,etime ELAPSED ELAPSED 1712055 19-19:34:15</pre>Get the running time of the process through

etime

. You can intuitively see that the process has been running for 19 days.Similarly, you can use

-o

Specify rss to obtain only the memory information of the process. <pre class="brush:sql;toolbar:false;">$ ps -p 10167 -o rss RSS 2180</pre>

6. Dynamic real-time viewing of logs


You can dynamically monitor through the

tail

command-f option Changes in log files are very practical <pre class="brush:sql;toolbar:false;">$ tail -f test.log</pre> If you want to stop tail monitoring immediately when information such as

Failed

appears in the log, you can use the following command to achieve this: <pre class="brush:sql;toolbar:false;">$ tail -f test.log | sed &amp;#39;/Failed/ q&amp;#39;</pre>

7. Rapid conversion of timestamps

Time operations are commonplace for programmers. Sometimes you want to convert the timestamp into date and time. You can also quickly convert it on the Linux command line:

$ date -d@1234567890 +"%Y-%m-%d %H:%M:%S"
2009-02-14 07:31:30

Of course, you can also view the current timestamp on the command line

$ date +%s
1617514141

8. Elegant calculation of program running time

Under Linux, you can easily obtain the program running time through the

time

command: <pre class="brush:sql;toolbar:false;">$ time ./test real 0m1.003s user 0m0.000s sys 0m0.000s</pre>You can see that the running time of the program is:

1.003s

. Attentive students will see that real does not seem to be equal to user sys, and is much larger than real. What is going on?

First explain the meaning of these three parameters:

  • real: represents the clock time, that is, the time it takes from program execution to end time;

  • user:表示运行期间,cpu 在用户空间所消耗的时间;

  • sys:表示运行期间,cpu 在内核空间所消耗的时间;

由于 usersys 只统计 cpu 消耗的时间,程序运行期间会调用 sleep 发生阻塞,也可能会等待网络或磁盘 IO,都会消耗大量时间。因此对于类似情况,real 的值就会大于其它两项之和。

另外,也会遇到 real 远远小于 user + sys 的场景,这是什么鬼情况?

这个更好理解,如果程序在多个 cpu 上并行,那么 usersys 统计时间是多个 cpu 时间,实际消耗时间 real 很可能就比其它两个之和要小了 

9. 命令行查看ascii码

我们在开发过程中,通常需要查看 ascii 码,通过 Linux 命令行就可以轻松查看,而不用去 Google 或 Baidu

$ man ascii

 10. 优雅的删除乱码的文件

在 Linux 系统中,会经常碰到名称乱码的文件。想要删除它,却无法通过键盘输入名字,有时候复制粘贴乱码名称,终端可能识别不了,该怎么办?

不用担心,下边来展示下 find 是如何优雅的解决问题的。

$ ls  -i
138957 a.txt  138959 T.txt  132395.txt

$ find . -inum 132395 -exec rm {} \;

命令中,-inum 指定的是文件的 inode 号,它是系统中每个文件对应的唯一编号,find 通过编号找到后,执行删除操作。 

11. Linux上获取你的公网IP地址

在办公或家庭环境,我们的虚拟机或服务器上配置的通常是内网 IP 地址,我们如何知道,在与外网通信时,我们的公网出口 IP 是神马呢?

这个在 Linux 上非常简单,一条命令搞定

$ curl ip.sb
$ curl ifconfig.me

上述两条命令都可以 

12. 如何批量下载网页资源

有时,同事会通过网页的形式分享文件下载链接,在 Linux 系统,通过 wget 命令可以轻松下载,而不用写脚本或爬虫

$ wget -r -nd -np --accept=pdf http://fast.dpdk.org/doc/pdf-guides/
# --accept:选项指定资源类型格式 pdf

13. 历史命令使用技巧

分享几个历史命令的使用技巧,能够提高你的工作效率。

  • !!:重复执行上条命令;

  • !N:重复执行 history 历史中第 N 条命令,N 可以通过 history 查看;

  • !pw:重复执行最近一次,以pw开头的历史命令,这个非常有用,小编使用非常高频;

  • !$:表示最近一次命令的最后一个参数;

猜测大部分同学没用过 !$,这里简单举个例子,让你感受一下它的高效用法

$ vim /root/sniffer/src/main.c
$ mv !$ !$.bak
# 相当于
$ mv /root/sniffer/src/main.c /root/sniffer/src/main.c.bak

当前工作目录是 root,想把 main.c 改为 main.c.bak。正常情况你可能需要敲 2 遍包含 main.c 的长参数,当然你也可能会选择直接复制粘贴。

而我通过使用 !$ 变量,可以很轻松优雅的实现改名,是不是很 hacker 呢? 

14. 快速搜索历史命令

在 Linux 下经常会敲很多的命令,我们要怎么快速查找并执行历史命令呢?

通过上下键来翻看历史命令,No No No,可以通过执行 Ctrl + r,然后键入要所搜索的命令关键词,进行搜索,回车就可以执行,非常高效。 

15. 真正的黑客不能忽略技巧

最后,再分享一个真正的黑客不能忽略技巧。我们在所要执行的命令前,加一个空格,那这条命令就不会被 history 保存到历史记录

有时候,执行的命令中包含敏感信息,这个小技巧就显得非常实用了,你也不会再因为忘记执行 history -c 而烦恼了。

相关推荐:《Linux视频教程

The above is the detailed content of Organize and share fifteen practical Linux tips (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

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