


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.
1. Loop Command
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.
2. Output redirection
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.
3. Compress files
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.
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
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
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.
6. Manage your SSH keys
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:
- eval命令将为你执行ssh-agent,并确保它在后台运行。
- ssh-add命令将添加你的默认SSH密钥。如果你为默认密钥设置了密码,它将提示你输入密码。
- 最后,ssh-add -l命令显示当前在你的代理中添加的所有密钥。
这个简单的一行命令确保你的代理工作正常,并包含了正确的密钥。当你需要连接到某个服务或获取一些代码时,下一步就可以直接开始了。
7、查找过去的命令
linuxmi@linuxmi:~/www.linuxmi.com$ history | grep "top"
或者按下CTRL + R,然后输入top
还记得很久以前运行的那个命令吗?我也不记得了。在历史记录中搜索它吧。
如果你像我一样记忆力不太好,那么history命令非常有用。它会显示当前终端会话中所有已运行的命令列表。反向交互式搜索或grep工具的真正威力在于能够找到之前执行的命令。
如果你只是想查看命令历史记录而不是执行它,你可以使用grep搜索。要统一搜索和执行操作,你可以使用CTRL + R的反向交互式搜索历史记录组合键。一旦按下快捷键,一个交互提示符就会出现,当你开始输入命令时,控制台会显示相应的匹配命令。
8、将多个命令作为一个整体传递
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

What is the correct way to restart a service in Linux? When using a Linux system, we often encounter situations where we need to restart a certain service, but sometimes we may encounter some problems when restarting the service, such as the service not actually stopping or starting. Therefore, it is very important to master the correct way to restart services. In Linux, you can usually use the systemctl command to manage system services. The systemctl command is part of the systemd system manager

LSOF (ListOpenFiles) is a command line tool mainly used to monitor system resources similar to Linux/Unix operating systems. Through the LSOF command, users can get detailed information about the active files in the system and the processes that are accessing these files. LSOF can help users identify the processes currently occupying file resources, thereby better managing system resources and troubleshooting possible problems. LSOF is powerful and flexible, and can help system administrators quickly locate file-related problems, such as file leaks, unclosed file descriptors, etc. Via LSOF Command The LSOF command line tool allows system administrators and developers to: Determine which processes are currently using a specific file or port, in the event of a port conflict
