We use Linux command lines frequently every day. although there are many materials on the internet about their usage skills, many of our friends have not tried it. Therefore, today's article will summarize this, hoping to help you. 1. display the output result as a table
Sometimes, when we look at the output results of a command, a large number of supported strings may be difficult to read. If you organize the Command into a table? It's actually very simple! You only need to enter:
mount | column –t:
In the preceding example, the output result is very clear because it is separated by spaces. So what if the delimiter is a colon or other symbols (such as cat/etc/passwd?
You only need to adjust one-s parameter.
cat /etc/passwd | column -t -s:
2. repeat a command until it is successfully run.
Many of my friends have searched the internet for this issue. Related suggestions include ping the server until it takes effect, checking whether a file with a specific extension has been uploaded from a specific directory, and checking whether a specific URL already exists.
In fact, you can use the while true loop to accomplish this goal:
In this example,>/dev/null 2> & 1 will redirect the output result of the program to/dev/null. This includes Standard Error and Standard Out.
This is one of my favorite Linux command line skills.
3. sort processes by memory usage
ps aux | sort -rnk 4:
4. sort processes by CPU usage
ps aux | sort -nk 3:
To check the architecture, you can use:
getconf LONG_BIT
5. View multiple log files at the same time
You can use the tail command to view logs, but sometimes you may need to view multiple log files at the same time. You can use the multi-tail command to easily implement various text highlighting and filtering requirements:
If this command is not available in the system, you can download it through apt-get install multitail.
6. return to the previous Directory
You only need to enter cd-to return to the previous directory.
7. allow non-interactive Shell sessions to achieve interaction
Set ~ /. Bashrc changed ~ /. Bash_profile.
8. monitoring command output results at regular intervals
With the watch df-h command, you can view the output results of any command. For example, you can view the remaining space and its growth.
With the variable data, the watch command can also play more roles.
9. run the program after the session is closed
If you run a program in the background and close it, shell will completely stop it. How can we continue to ensure shell running after it is closed?
You can use the nohup command, which indicates that there is no hanging up:
nohup wget site.com/file.zip
This command is also a commonly ignored Linux command, because most users use another imperative screen:
It will generate a file named nohup. out in the same directory, which contains the content of the current running program:
Cool, right?
10. automatically reply Yes or No to any command
If you want to automatically reply yes to the request, you can use: yes | apt-get update.
If you want to automatically reply to no, you can use yes no | command.
Summary of the most practical Linux command line usage tips
11. create a file of a specific size
You can use the dd command to create a file of a specific size:
dd if=/dev/zero of=out.txt bs=1M count=10.
The size of the newly generated file is 10 MB, all of which are filled with 0:
12. run the previous command as Root.
Sometimes, you may forget to add sudo when entering a command that requires the root permission. You don't have to enter sudo again. just enter sudo!
13. record command line Sessions
If you want to record your input content on the shell screen, you can use the script command to save the input content as a typescript File: script.
After exit is output, all commands will be written to this file for subsequent review.
14. replace spaces with tabs
You can use the tr command to replace a character with another character:
cat geeks.txt | tr ':[space]:' '\t' > out.txt。
15. convert a file to uppercase or lowercase
Run the following command:
cat myfile | tr a-z A-Z > output.txt。
16. powerful Xargs commands
Xargs commands are one of the most important techniques in Linux. You can use this command to take the output result of a command as a parameter of another command. For example, you can search for a PNGpng file and compress it or perform other operations:
find. -name *.png -type f -print | xargs tar -cvzf images.tar.gz
Alternatively, you may have saved a URL list in the file and want to download or process it:
cat urls.txt | xargs wget
Note that the output result of the first command is passed to the end of the xargs command.
What if we need to pass the command output result to the center? You only need to add the-I parameter to {}, as shown below. In this case, the replace parameter is used to specify the transmission location of the output result of the first command:
ls /etc/*.conf | xargs -i cp {} /home/likegeeks/Desktop/out
Of course, this is just a part of Linux command line tips. If you need it, try awk, sed, and other commands! In addition, we look forward to sharing your practices and experiences in your comments.