Home > Operation and Maintenance > Nginx > Eight Shell commands to make you a Linux command line master in no time

Eight Shell commands to make you a Linux command line master in no time

王林
Release: 2023-06-17 09:38:56
forward
1413 people have browsed it

Having a solid programming foundation is obviously an essential quality for a good software engineer. It is very important to master at least one programming language, whether it is an interpreted language such as Python, or a compiled language such as C. However, this is only one aspect of becoming a truly well-rounded engineer. Those basics are of no use if you're lost in the shell environment.

The flexible application of Bash commands allows you to enter areas that traditional programming languages ​​cannot cover. Sometimes, you don't actually need to use a more powerful programming language. Using the Shell, you can accomplish the tasks you need faster and more easily, without the need for additional dependencies.

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

In this article, we will explore some very useful Bash commands. These commands can help you avoid writing more code than you actually need. Next time you encounter a problem, try these commands.

1. Loop Command

linuxmi@linuxmi:~/www.linuxmi.com$ while true; do echo "hello $(date)"; sleep 1; done
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

You don’t have to jump into a huge programming language just to loop something. Getting output at regular intervals or iterating over basic data is easy to do in Bash.

This line of code demonstrates how to build a simple infinite while loop in the Shell. You just splice everything together with a semicolon and you're done. You are free to change the command executed and adjust the sleep timer accordingly.

When you run this command, you should see the date change every second on your terminal.

2. Output redirection

linuxmi@linuxmi:~/www.linuxmi.com$ echo "hello linux迷 www.linuxmi.com" | tee linuxmi.rs | less
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

The tee command can achieve functions that require multiple lines of code in other languages. Using this handy little tool, you can send certain input to a file or other command, which can then be passed to another command. It's actually like installing a "T" valve in your water pipe. You can direct a portion of the output out and it will continue to flow down the pipe.

The above example sends the "hello linux fan www.linuxmi.com" text obtained from the echo command to the linuxmi.rs file, and then proceeds to send it to less. One way to rewrite it is: you will get a file with the output content, and you can view it on the screen using the less command.

3. Compress files

linuxmi@linuxmi:~/www.linuxmi.com$ tar -czvf linuxmi.tar.gz linuxmi.sh
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

Moving files and directories on the command line is an important skill. If you're working on something and need to move it between hosts, or just want to compress a file for offline storage, the tar command is your friend.

Using the above commands and options, you can compress a directory into a new tar.gz compressed package. Now you can bring your files quickly.

4. Counting

linuxmi@linuxmi:~/www.linuxmi.com$ echo -e "linuxmi\n linuxmi.com\n www.linuxmi.com\n www.93139.com" > linuxmi.txt | wc -l
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

Want to know how many lines there are in the file? Very simple. Use wc utility. "Word count" is what it actually means, but it can also be used to count many other things, such as the number of lines.

The above snippet outputs four lines of text to a file and then uses wc to count the number of lines. This tool is useful if you need to manipulate a certain number of lines or confirm whether a process has written new lines to a file.

5. Generate numbers

linuxmi@linuxmi:~/www.linuxmi.com$ seq 95 100
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

So simple, but very helpful. Generating numbers in Bash is very easy, just use the seq utility. This neat little command outputs a sequence of numbers that you can use in loops, text files, or anywhere else you need a list of numbers.

You can also change the delimiter if needed:

seq -s " " 1 10
Copy after login

or

echo {0..10}
Copy after login

This will separate all numbers with spaces instead of the default newline. You can also use the echo command and the .. operator to obtain the same type of results.

6. Manage your SSH keys

linuxmi@linuxmi:~/www.linuxmi.com$ eval $(ssh-agent) && ssh-add && ssh-add -l
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

A basic understanding of SSH keys and how to manage them is absolutely necessary. You may find that understanding the ssh-add and ssh-agent utilities can be more beneficial than you think.

The above command performs several important operations:

  • eval命令将为你执行ssh-agent,并确保它在后台运行。
  • ssh-add命令将添加你的默认SSH密钥。如果你为默认密钥设置了密码,它将提示你输入密码。
  • 最后,ssh-add -l命令显示当前在你的代理中添加的所有密钥。

这个简单的一行命令确保你的代理工作正常,并包含了正确的密钥。当你需要连接到某个服务或获取一些代码时,下一步就可以直接开始了。

7、查找过去的命令

linuxmi@linuxmi:~/www.linuxmi.com$ history | grep "top"
Copy after login
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

或者按下CTRL + R,然后输入top

还记得很久以前运行的那个命令吗?我也不记得了。在历史记录中搜索它吧。

如果你像我一样记忆力不太好,那么history命令非常有用。它会显示当前终端会话中所有已运行的命令列表。反向交互式搜索或grep工具的真正威力在于能够找到之前执行的命令。

如果你只是想查看命令历史记录而不是执行它,你可以使用grep搜索。要统一搜索和执行操作,你可以使用CTRL + R的反向交互式搜索历史记录组合键。一旦按下快捷键,一个交互提示符就会出现,当你开始输入命令时,控制台会显示相应的匹配命令。

8、将多个命令作为一个整体传递

linuxmi@linuxmi:~/www.linuxmi.com$ history | grep "top"
Copy after login
Copy after login

八个 Shell 命令,让你瞬间成为 Linux 命令行大师

有时Bash会变得有点奇怪。变量插值可能出错,嵌套引号可能会混乱且难以跟踪。情况将变得更加复杂,尤其是当你需要在不同的二进制文件或服务中传递命令字符串时。在这种情况下,你可以使用bash命令将一组命令作为单个实体进行评估。

这个指令将接收纯文本字符串,然后按照常规的Bash语法来进行解析。对于外部shell来说,你只是运行一个命令并传递一个参数,但实际上你是在指示Bash解析多个命令的字符串并执行它们。

The above is the detailed content of Eight Shell commands to make you a Linux command line master in no time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template