Home >Operation and Maintenance >Nginx >Eight Shell commands to make you a Linux command line master in no time
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.
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.
linuxmi@linuxmi:~/www.linuxmi.com$ while true; do echo "hello $(date)"; sleep 1; done
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.
linuxmi@linuxmi:~/www.linuxmi.com$ echo "hello linux迷 www.linuxmi.com" | tee linuxmi.rs | less
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.
linuxmi@linuxmi:~/www.linuxmi.com$ tar -czvf linuxmi.tar.gz linuxmi.sh
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.
linuxmi@linuxmi:~/www.linuxmi.com$ echo -e "linuxmi\n linuxmi.com\n www.linuxmi.com\n www.93139.com" > linuxmi.txt | wc -l
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.
linuxmi@linuxmi:~/www.linuxmi.com$ seq 95 100
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
or
echo {0..10}
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.
linuxmi@linuxmi:~/www.linuxmi.com$ eval $(ssh-agent) && ssh-add && ssh-add -l
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:
这个简单的一行命令确保你的代理工作正常,并包含了正确的密钥。当你需要连接到某个服务或获取一些代码时,下一步就可以直接开始了。
linuxmi@linuxmi:~/www.linuxmi.com$ history | grep "top"
或者按下CTRL + R,然后输入top
还记得很久以前运行的那个命令吗?我也不记得了。在历史记录中搜索它吧。
如果你像我一样记忆力不太好,那么history命令非常有用。它会显示当前终端会话中所有已运行的命令列表。反向交互式搜索或grep工具的真正威力在于能够找到之前执行的命令。
如果你只是想查看命令历史记录而不是执行它,你可以使用grep搜索。要统一搜索和执行操作,你可以使用CTRL + R的反向交互式搜索历史记录组合键。一旦按下快捷键,一个交互提示符就会出现,当你开始输入命令时,控制台会显示相应的匹配命令。
linuxmi@linuxmi:~/www.linuxmi.com$ history | grep "top"
有时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!